我有一个名为AddPatientView的页面,其中BottomNavigationBar包含AddPatientInfo和AddPatient
Images页面.所有这三个都是有状态的小部件.
默认情况下,AddPatientInfo打开,其中包含一堆TextFields(用于输入患者信息),在AddPatientImages页面中,用户可以添加Images.
问题是如果我在AddPatientInfo上填充TextFields然后转到AddPatientImages然后返回,所有TextFields都是空的.这是正确的,因为整个小部件树得到重建,我放弃了所有填充的数据.
所以我正在实现AutomaticKeepAliveClientMixin,因此即使更改选项卡也会保持状态.但它似乎不起作用:
这是我的代码:
AddPatientView(父)
class AddPatientView extends StatefulWidget { @override State<StatefulWidget> createState() { return _AddPatientViewState(); } } class _AddPatientViewState extends State<AddPatientView> { int _currentIndex = 0; List<Widget> _children; List<File> _imageFileList = new List<File>(); @override void initState() { super.initState(); _children = [ AddPatientInfo(savePatient),AddPatientImages(_imageFileList) ]; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("New Patient Record"),),body: _children[_currentIndex],bottomNavigationBar: BottomNavigationBar( currentIndex: _currentIndex,items: [ BottomNavigationBarItem(icon: new Icon(Icons.create),title: new Text('Info')),BottomNavigationBarItem(icon: new Icon(Icons.camera_alt),title: new Text('Images')),],onTap: (int index) { setState(() { _currentIndex = index; }); },); } }
AddPatientInfo
class AddPatientInfo extends StatefulWidget { final Function savePatient; AddPatientInfo(this.savePatient){ } @override State<StatefulWidget> createState() { return _AddPatientInfoState(); } } class _AddPatientInfoState extends State<AddPatientInfo> with AutomaticKeepAliveClientMixin<AddPatientInfo> { Function _savePatient; String _firstName,_lastName,_gender,_phone,_email,_diabetesMeds,_hypertensionMeds,_others; int _age,_diabetesYears,_hypertensionYears,_smokesPerDay,_smokerYears; bool _diabetes = false,_hypertension = false,_smoker = false,_chestPain = false,_cva = false,_ckd = false,_breathlessness = false,_syncope = false,_sweating = false,_sweatingFeet = false; List<String> _genderList = new List<String>(); List<String> _yesNoList = new List<String>(); List<File> _imageFileList = new List<File>(); @override void initState() { super.initState(); _savePatient = widget.savePatient; _genderList.addAll(['Male','Female','Other']); _yesNoList.addAll(['Yes','No']); _gender = _genderList.elementAt(0); } @override Widget build(BuildContext context) { return Scaffold( resizeToAvoidBottomPadding: false,body: Container( margin: EdgeInsets.all(10.0),child: Form( child: new ListView( children: <Widget>[ TextField( decoration: InputDecoration( labelText: 'Patient First Name',labelStyle: TextStyle( color: Colors.black ),hintText: 'Enter patients first name' ),onChanged: (String value) { setState(() { _firstName = value; }); },TextField( decoration: InputDecoration( labelText: 'Patient Last Name',hintText: 'Enter patients last name' ),onChanged: (String value) { setState(() { _lastName = value; }); },//other textfield widgets below ],) ),); } @override bool get wantKeepAlive => true; }
我在这里错过了什么?是否有更优雅的方式来维护表单中的数据?
解决方法
从
AutomaticKeepAliveClientMixin的文档:
/// A mixin with convenience methods for clients of [AutomaticKeepAlive]. Used /// with [State] subclasses. /// /// Subclasses must implement [wantKeepAlive],and their [build] methods must /// call `super.build` (the return value will always return null,and should be /// ignored).
所以在你的例子中,在你返回Scaffold之前只需要调用super.build:
Widget build(BuildContext context) { super.build(context); return Scaffold(...); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。