Flutter: AutomaticKeepAliveClientMixin is not working with BottomNavigationBar

后端 未结 3 1261
情书的邮戳
情书的邮戳 2020-12-13 10:33

I have page called AddPatientView with BottomNavigationBar containing AddPatientInfo and AddPatientImages pages. All of these three are Stateful widgets.

By default

3条回答
  •  难免孤独
    2020-12-13 10:35

    So I wrapped the tab pages in Stack widget and now it keeps the state of the form intact. I am not sure if this is the right way to go but it does the job.

    Here is the code:

    AddPatientView (parent)

    class AddPatientView extends StatefulWidget {
      @override
      State createState() {
        return _AddPatientViewState();
      }
    }
    
    class _AddPatientViewState extends State {
      int _currentIndex = 0;
      List _imageFileList = new List();
    
      @override
      void initState() {
        super.initState();
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("New Patient Record"),
          ),
          body: new Stack(
            children: [
              new Offstage(
                offstage: _currentIndex != 0,
                child: AddPatientInfo(savePatient),
              ),
              new Offstage(
                offstage: _currentIndex != 1,
                child: AddPatientImages(_imageFileList),
              )
            ],
          ),
          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;
              });
            },
          ),
        );
      }
    }
    

提交回复
热议问题