dart – Flutter:AutomaticKeepAliveClientMixin不能与BottomNavigationBar一起使用

dart – Flutter:AutomaticKeepAliveClientMixin不能与BottomNavigationBar一起使用,第1张

概述我有一个名为AddPatientView的页面,其中BottomNavigationBar包含AddPatientInfo和AddPatient Images页面.所有这三个都是有状态的小部件. 默认情况下,AddPatientInfo打开,其中包含一堆TextFields(用于输入患者信息),在AddPatientImages页面中,用户可以添加Images. 问题是如果我在AddPatient 我有一个名为AddPatIEntVIEw的页面,其中BottomNavigationbar包含AddPatIEntInfo和AddPatIEnt Images页面.所有这三个都是有状态的小部件.

默认情况下,AddPatIEntInfo打开,其中包含一堆TextFIElds(用于输入患者信息),在AddPatIEntimages页面中,用户可以添加Images.

问题是如果我在AddPatIEntInfo上填充TextFIElds然后转到AddPatIEntimages然后返回,所有TextFIElds都是空的.这是正确的,因为整个小部件树得到重建,我放弃了所有填充的数据.

所以我正在实现automaticKeepAliveClIEntMixin,因此即使更改选项卡也会保持状态.但它似乎不起作用:

Working Gif via GIPHY

这是我的代码:

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(...);  }
总结

以上是内存溢出为你收集整理的dart – Flutter:AutomaticKeepAliveClientMixin不能与BottomNavigationBar一起使用全部内容,希望文章能够帮你解决dart – Flutter:AutomaticKeepAliveClientMixin不能与BottomNavigationBar一起使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1001915.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存