在所有页面上都持续存在AppBar抽屉 [英] Persisting AppBar Drawer across all Pages Flutter

查看:87
本文介绍了在所有页面上都持续存在AppBar抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个统一的抽屉,可以在我的应用程序的所有页面上进行访问。如何使它在所有这些页面中持久存在而不必在每个dart文件中重新创建自定义抽屉小部件?

I am trying to create a uniform drawer that is accessible across all pages in my app. How do I make it persist throughout all these pages without having to recreate my custom Drawer widget in every single dart file?

推荐答案

有有几种不同的选择。最基本的希望是您已经完成的事情,但是无论如何我都会列出:

There are a few different options for this. The most basic is hopefully something you've already done, but I'll list it anyways:

1:为抽屉创建一个类

您的小部件应该是其自己的有状态或无状态小部件。这样,您只需每次都实例化它。

Your widget should be its own stateful or stateless widget. This way, you just have to instantiate it each time.

class MyDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(...);
  }
}

然后在每个页面中使用时:

And then when using it in each page:

Scaffold(
  drawer: MyDrawer(...),
  ...
)

我希望您已经这样做了;如果不是,你应该是。类的构建函数不应太大,否则会导致性能下降和难以维护的代码;从长远来看,将事物分解成逻辑单元将为您提供帮助。

I hope you're already doing this; if not you should be. A class's build function shouldn't be too large or it can lead to poor performance and harder to maintain code; splitting things into logical units will help you in the long run.

2:为支架创建类

如果必须在每个页面的支架中包含相同的抽屉仍然太多代码,则可以改用封装支架的类。本质上,它将为您实际使用的每个支架输入接受输入。

If having to include the same drawer in a scaffold for each page is still too much code, you can instead use a class that encapsulates your scaffold. It would essentially take inputs for each of the scaffold inputs you actually use.

class MyScaffold extends StatelessWidget {

  final Widget body;

  MyScaffold({this.body});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
     body: body,
     drawer: MyDrawer(...),
    );
  }
}

然后在代码中使用Scaffold而不是MyScaffold(但请给它起个更好的名字= D)。

And then instead of using Scaffold in your code, use MyScaffold (but please name it something better =D).

3:多级脚手架

我仅将这种完成方式包括在内,我不建议这样做。话虽这么说,但在某些情况下,您无法通过flutter的正常工作流程来完成这些工作,例如,当用户在抽屉中的不同项目上轻按时,是否需要自定义动画。

I'm only including this way of doing it to be complete, and I don't recommend it. That being said, there are certain things you can't get to work within flutter's normal workflow that you could do by doing this - for example if you want a custom animation for when the user taps on different items in the drawer.

基本上,在这种情况下,您要做的是在MaterialApp或Navigator之外安装一个Scaffold(我相信这也意味着您必须在该设备之外安装另一个Navigator) ,但我不确定100%)。您将在导航之外的支架上显示抽屉,而另一个支架(在导航的每个页面上)将做您需要做的其他事情。有一些警告-您必须确保获得正确的脚手架(即 Scaffold.of(context)本身不会削减它-您必须以获取第一个脚手架的上下文并使用它来查找较高级别的脚手架),您可能需要将(较低级别脚手架的)GlobalKey传递给Drawer,以便它实际上可以更改其中的页面

Basically, what you'd do in this case is to have a Scaffold outside of your MaterialApp or Navigator (which I believe would also mean you'd have to have another Navigator outside that, but I'm not 100% sure). You would have the scaffold that's outside your navigation show the drawer while the other one (on each page within the navigation) would do whatever else you need it to do. There's a few caveats - you'd have to make sure you get the right scaffold (i.e. Scaffold.of(context) by itself wouldn't cut it - you'd have to get the context of the first scaffold and use it to find the higher-level one), and you'd probably need to pass a GlobalKey (of the lower-level scaffold) to the Drawer so that it could actually change pages within it.

正如我所说,我不建议您使用这种方法,因此,我将不做进一步的详细说明,而只是将其作为练习读者是否想去那个兔子洞!

As I said, I don't recommend this approach, so I'm not going to go into any more detail than that but rather leave it as an exercise for the reader if they want to go down that rabbit hole!

这篇关于在所有页面上都持续存在AppBar抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆