Flutter Drawer Widget - change Scaffold.body content

前端 未结 4 1456
死守一世寂寞
死守一世寂寞 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条回答
  • 2020-12-05 21:24

    Here is a easy and efficient way to do this with Full example

    class _MyPageState extends State<MyPage> {
      Widget bodyWidget;
    
      Widget drawer() {
        return
      }
    
      Widget bodyFirst() {
        return Container(
            color: Colors.green, child: Center(child: Text("First Page")));
      }
    
      Widget bodySecond() {
        return Container(
            color: Colors.limeAccent, child: Center(child: Text("Second Page")));
      }
    
      Widget bodyThird() {
        return Container(
            color: Colors.lightBlueAccent,
            child: Center(child: Text("Third Page")));
      }
      
      @override
      void initState() {
        super.initState();
        bodyWidget = bodyFirst();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          drawer: drawer(),
          appBar: AppBar(title: Text("My App")),
          body: bodyWidget,
        );
      }
    }
    

    0 讨论(0)
  • 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<HomePage> {
      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?

    0 讨论(0)
  • 2020-12-05 21:34

    That's a very good solution from Aziza! Thank you. It works! I'd just want to add that it can also work for the whole body by replacing it with a widget state:

    ...
    class _TestPageState extends State<TestPage> {
      String text = "Initial Text";
      Widget widgetForBody = SomeWidgetFromClass();
      ...
      ...
            children: <Widget>[
              new Container(child: new DrawerHeader(child: new Container())),
              new Container (
                child: new Column(
                    children: <Widget>[
                      new ListTile(leading: new Icon(Icons.info),
                          onTap:(){
                              setState((){
                                widgetForBody = AnotherWidgetFromClass();
                              });
                          }
                      ),
                      new ListTile(leading: new Icon(Icons.save),
                          onTap:(){
                            setState((){
                              widgetForBody = YetAnotherWidgetFromClass();
                            });
                          }
                      ),
    ...
    
    0 讨论(0)
  • 2020-12-05 21:38

    You can simply change the state of the content of your interest by interacting with your Drawer items like this:

    Here is the relevant code for this example:

    class TestPage extends StatefulWidget {
      @override
      _TestPageState createState() => new _TestPageState();
    }
    
    class _TestPageState extends State<TestPage> {
      String text = "Initial Text";
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            drawer: new Drawer(
              child: new ListView(
                children: <Widget>[
                  new Container(child: new DrawerHeader(child: new Container())),
                  new Container (
                    child: new Column(
                        children: <Widget>[
                          new ListTile(leading: new Icon(Icons.info),
                              onTap:(){
                                  setState((){
                                    text = "info pressed";
                                  });
                              }
                          ),
                          new ListTile(leading: new Icon(Icons.save),
                              onTap:(){
                                setState((){
                                  text = "save pressed";
                                });
                              }
                          ),
                          new ListTile(leading: new Icon(Icons.settings),
                              onTap:(){
                                setState((){
                                  text = "settings pressed";
                                });
                              }
                          ),
    
                        ]
                    ),
                  )
                ],
              ),
            ),
            appBar: new AppBar(title: new Text("Test Page"),),
            body: new Center(child: new Text((text)),
            ));
      }
    }
    
    0 讨论(0)
提交回复
热议问题