我尝试过这种不同的方法,但它根本不起作用。我喜欢用户在提交/上传后留在文本字段中的输入文本,所以当用户回到页面时,文本应该在那里,用户可以手动清除它,不确定这里出了什么问题。提前谢谢。
class MondayScreen extends StatefulWidget {
static final String id = 'monday_screen';
@override
_MondayScreenState createState() => _MondayScreenState();
}
TextEditingController _oneController = TextEditingController();
class _MondayScreenState extends State<MondayScreen> {
String _alOne = '';
_submit() {
Post post = Post(
authorId: Provider.of<UserData>(context ,listen: false).currentUserId,
timestamp: Timestamp.fromDate(DateTime.now()),
alOne: _alOne,
);
DatabaseService.createPostMonday(post);
setState(() {
_oneController.text;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text(
'Monday Lunch',
style: TextStyle(color: Colors.white),
),
actions: <Widget>[
FlatButton(
child: Text(
'Spara',
style:
TextStyle(color: Colors.white, fontWeight: FontWeight.w600),
),
onPressed: _submit,
color: Colors.black,
)
],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.only(top: 60.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 30.0),
child: TextField(
controller: TextEditingController(),
style: TextStyle(fontSize: 18.0),
decoration: InputDecoration(
labelText: 'Alternativ',
),
onSubmitted: (input) => _alOne = input,
),
),
我在使用StatelessWidget构建表单时遇到了类似的问题。
请确保使用StatefulWidget,并且_oneController
是状态的字段。参考https://flutter.dev/docs/cookbook/forms/validation
将oneController传递到TextFormField (_O(
控制器:_oneController