Flutter Drawer Widget - change Scaffold.body content

前端 未结 4 1468
死守一世寂寞
死守一世寂寞 2020-12-05 21:09

When using the Flutter Scaffold.Drawer - is it possible to have the links in the Drawer change the content (Scaffold.body) on the page?

Similarly to having links in

4条回答
  •  -上瘾入骨i
    2020-12-05 21:32

    You can use this for body of Scaffold:

            body: _getDrawerItemWidget(_selectedDrawerIndex));
    

    and then in the main or state class do like so:

    class HomePageState extends State {
      int _selectedDrawerIndex = 0;
    
      _getDrawerItemWidget(int pos) {
        switch (pos) {
          case 0:
            return new MainFragment();
          case 1:
              return new CardMgmt();
    }
    }
    }
    

    CardMgmt and other classes in switch case have their own build method that will return a Widget and it will be place in body of your scaffold. Also they can be in the same dart file or even another file.
    But I have another problem:
    What to do if instead of drawer items, we have a button in another class like CardMgmt and we want it to return a Widget and update page?

提交回复
热议问题