加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dart – Flutter:AutomaticKeepAliveClientMixin不能与BottomNa

发布时间:2020-12-14 14:52:30 所属栏目:百科 来源:网络整理
导读:我有一个名为AddPatientView的页面,其中BottomNavigationBar包含AddPatientInfo和AddPatient Images页面.所有这三个都是有状态的小部件. 默认情况下,AddPatientInfo打开,其中包含一堆TextFields(用于输入患者信息),在AddPatientImages页面中,用户可以添加Ima
我有一个名为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(...);
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读