Flutter TextField输入内容后 保持光标在最后
class InputPage extends StatefulWidget {
InputPage({Key key}) : super(key: key);
@override
_InputPageState createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
TextEditingController inputController;
String value;
@override
void initState() {
super.initState();
inputController = TextEditingController.fromValue(
TextEditingValue(
// 设置内容
text: value,
// 保持光标在最后
selection: TextSelection.fromPosition(
TextPosition(
affinity: TextAffinity.downstream,
offset: value.length,
),
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false, // 防止键盘顶起内容
body: Stack(
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(horizontal: 15.0),
margin: EdgeInsets.only(top: 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextInput(
inputController: inputController,
height: 180,
maxLength: 50,
maxLines: 20,
placeholder: 'placeholder',
value: value,
showLength: true,
changeVal: (val) {
value = val;
setState(() {});
}),
],
),
),
],
),
);
}
}