【问题标题】:What is the right way to navigate using a drawer and change only the content of the body?使用抽屉导航并仅更改正文内容的正确方法是什么?
【发布时间】:2018-08-18 17:07:25
【问题描述】:

我正在创建一个基本的 Material 应用程序,并且我有一个用于导航的抽屉。 通过推送路线的简单方法,整个小部件将被替换,这就像打开一个全新的页面,其中包括一个全新的抽屉。 我的目标是营造页面和抽屉的氛围,当用户点击抽屉项目时,抽屉会折叠,只有页面的内容会被替换。

我找到了这两个问题/答案:

  1. Replace initial Route in MaterialApp without animation?
  2. Flutter Drawer Widget - change Scaffold.body content

我的问题是实现我想要做的最好/正确的方法是什么?

第一种方法只是通过删除push/pop 动画来创建错觉,尽管它实际上仍然像我描述的原始方法一样。 第二种方法实际上只是替换了内容,我想到的解决方案是代替更改文本来创建多个Container 小部件并在它们之间进行更改。

由于我还是新手并且正在学习 Flutter,我想知道这样做的正确做法是什么。

编辑: 我创建了this,效果很好。我仍然不知道它的有效性/效率,但现在这正是我想要实现的目标:

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blueGrey,
      ),
      home: new TestPage(),
    );
  }
}

class TestPage extends StatefulWidget {
  @override
  _TestPageState createState() => new _TestPageState();
}

class _TestPageState extends State<TestPage> {
  static final Container info = new Container(
    child: new Center(
        child: new Text('Info')
    ),
  );

  static final Container save = new Container(
    child: new Center(
        child: new Text('Save')
    ),
  );

  static final Container settings = new Container(
    child: new Center(
        child: new Text('Settings')
    ),
  );

  Container activeContainer = info;

  @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), title: new Text('Info'),
                          onTap:(){
                            setState((){
                              activeContainer = info;
                            });
                            Navigator.of(context).pop();
                          }
                      ),
                      new ListTile(leading: new Icon(Icons.save), title: new Text('Save'),
                          onTap:(){
                            setState((){
                              activeContainer = save;
                            });
                            Navigator.of(context).pop();
                          }
                      ),
                      new ListTile(leading: new Icon(Icons.settings), title: new Text('Settings'),
                          onTap:(){
                            setState((){
                              activeContainer = settings;
                            });
                            Navigator.of(context).pop();
                          }
                      ),

                    ]
                ),
              )
            ],
          ),
        ),
        appBar: new AppBar(title: new Text("Test Page"),),
        body: activeContainer,
        );
  }
}

【问题讨论】:

  • This 可能会有所帮助

标签: dart flutter


【解决方案1】:

您想要实现的不是第二个答案中的内容,否? :/

这里我通过切换颜色来说明它,但您可以将它应用到内容本身或基本上任何其他小部件。

class NavDrawer extends StatefulWidget {
  @override
  _NavDrawerState createState() => new _NavDrawerState();
}

class _NavDrawerState extends State<NavDrawer> {
  final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
  Color contentColor = Colors.white;
  @override
  initState(){
    super.initState();
      }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: scaffoldKey,
      appBar: new AppBar(),
      drawer: new Drawer(
        child: new ListView(
          children: <Widget>[
            new DrawerHeader(child: new Container()),
            new ListTile(title: new Text("Blue"),onTap: (){setState((){contentColor=Colors.blue;Navigator.pop(context);});},),
            new ListTile(title: new Text("Red"),onTap: (){setState((){contentColor=Colors.red;Navigator.pop(context);});},),
            new ListTile(title: new Text("Green"),onTap: (){setState((){contentColor=Colors.green;Navigator.pop(context);});},),
            new ListTile(title: new Text("Yellow"),onTap: (){setState((){contentColor=Colors.yellow;Navigator.pop(context);});},)

          ],
        ),
      ),
      body: new Container(
        color: contentColor,
      ),
    );
  }
}

【讨论】:

  • 是的,这正是我想要做的,但我的问题更多是关于它的有效性和“正确性”,而不是如何做到这一点,因为有几种解决方法。
  • 这在 Flutter 中是很正常的,它实际上取决于您交替使用的数据类型和您的用例。
  • 好吧,既然我对颤动很陌生,我认为可能有一种内置的方法可以做到这一点,而不是这样的“解决方法”,但如果你说这很正常,那就太好了,谢谢!
  • 这不是一种解决方法,但是,如果您有更复杂的视图并且在一个类中完成所有操作,则可能会很乏味,状态管理会很痛苦,因此请确保您在做什么将每个视图重构为一个单独的类相当复杂。
猜你喜欢
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多