如何使用“持久性”'抽屉

时间:2018-06-13 22:45:31

标签: dart flutter

我的目标是让抽屉在不同的路线之间导航。 如果我们目前在那条路线上,我想通过更改抽屉中路线名称的颜色让它看起来更好一点。

我的问题是每次按下汉堡包菜单时都会显示我的抽屉的新实例,因为在每条路线中我将其设置为:

return new Scaffold(
      appBar: new AppBar(
        title: new Text('My Route'),
      ),
      drawer: new MyDrawer(),  //  <-----------------------------
      body: new Container()  
);

有没有办法一遍又一遍地调用同一个抽屉,从而保持其状态?

1 个答案:

答案 0 :(得分:0)

我的创建方式是创建side_nav.dart

import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';
import '../pages/login_page.dart';
import '../pages/feedback_page.dart';
import '../pages/upload_page.dart';
import '../pages/search_orders_page.dart';

class SideNav extends StatefulWidget {
  SideNav(
    this.page,
    this.scaffoldKey, {
    Key key,
  }) : super(key: key);
  String page;
  GlobalKey<ScaffoldState> scaffoldKey;
  @override
  _SideNav createState() => new _SideNav(this.page, this.scaffoldKey);
}

class _SideNav extends State<SideNav> {
  _SideNav(this.page, this.scaffoldKey2);
  //final formKey = new GlobalKey<FormState>();
  String page;
  GlobalKey<ScaffoldState> scaffoldKey2;
  static Router router;
  Handler searchHandler;
  Handler settingHandler;
  Handler loginHandler;
  Handler uploadHandler;
  Handler previewHandler;

  void defineRoutes(Router router) {
    router.define("login_page", handler: loginHandler);
    router.define("setting_page", handler: settingHandler);
    router.define("search_page", handler: searchHandler);
    router.define("upload_page", handler: uploadHandler);
    router.define("page_preview", handler: previewHandler);
  }

  @override
  initState() {
    super.initState();

    router = new Router();
    searchHandler = new Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return new SearchOrdersPage();
    });
    loginHandler = new Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return new LoginPage();
    });
    settingHandler = new Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return new FeedbackPage();
    });
    uploadHandler = new Handler(
        handlerFunc: (BuildContext context, Map<String, dynamic> params) {
      return new UploadPage();
    });

    defineRoutes(router);
  }

  @override
  Widget build(BuildContext context) {
    return new Drawer(
        key: scaffoldKey2,
        child: new ListView(
          children: <Widget>[
            new ListTile(
              title:
                  new Text('Data store', style: new TextStyle(fontSize: 14.0)),
              subtitle:
                  new Text('WVDEMO', style: new TextStyle(fontSize: 16.0)),
              onTap: () {},
            ),
            new ListTile(
              title: new Text('Username', style: new TextStyle(fontSize: 14.0)),
              subtitle: new Text('Demo', style: new TextStyle(fontSize: 16.0)),
              onTap: () {},
            ),
            new Divider(height: 1.0, color: Colors.grey[350]),
            new ListTile(
              leading: new Icon(Icons.cloud_upload),
              title: new Text('Upload', style: new TextStyle(fontSize: 14.0)),
              onTap: () {
                print("Page: $page");
                (page == "Upload")
                    ? Navigator.pop(context, true)
                    : router.navigateTo(
                        context,
                        "upload_page",
                        transition: TransitionType.inFromBottom,
                        replace: true,
                      );
              },
            ),
            new ListTile(
              leading: new Icon(Icons.search),
              title: new Text('Search', style: new TextStyle(fontSize: 14.0)),
              onTap: () {
                print("Page: $page");
                (page == "Search")
                    ? Navigator.pop(context, true)
                    : router.navigateTo(
                        context,
                        "search_page",
                        transition: TransitionType.inFromBottom,
                        replace: true,
                      );
              },
            ),
            new ListTile(
              leading: new Icon(Icons.mail),
              title: new Text('Feedback', style: new TextStyle(fontSize: 14.0)),
              onTap: () {
                print("Page: $page");
                (page == "Feedback")
                    ? Navigator.pop(context, true)
                    : router.navigateTo(
                        context,
                        "setting_page",
                        transition: TransitionType.inFromBottom,
                        replace: true,
                      );
              },
            ),
            new Divider(height: 1.0, color: Colors.grey[350]),
            new ListTile(
              title: new Text('Log Out', style: new TextStyle(fontSize: 14.0)),
              onTap: () async {
                router.navigateTo(context, "/login_page",
                    transition: TransitionType.inFromBottom, replace: true);
              },
            ),
          ],
        ));
  }
} 

像这样叫抽屉

drawer: new SideNav(
        "Upload",
        scaffoldKey,
      ),
  

还有很多其他方法可以做到这一点。但是,我发现它更容易实现   希望对您有帮助