颤振应用程序抽屉导航到新页面

flutter app drawer navigation to new page

寻找更好的方法来实现如何从应用程序抽屉导航到下一页,我在其他文件中制作了一个有状态小部件并导入 main.dart,而不是

Navigate.pop(context);

我用什么?

我试过了

Navigator.of(context).push(
    MaterialPageRoute<Null>(builder: (BuildContext context) {
        return new HomePage();

它在上一页上加载页面并使事情变慢。

下面是代码。

  return Scaffold(
      appBar: AppBar(title: Text(title)),
      body: Center(child: Text('some text')),
      drawer: Drawer(
        // Add a ListView to the drawer. This ensures the user can scroll
        // through the options in the drawer if there isn't enough vertical
        // space to fit everything.
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              child: Text('Drawer Header'),
              decoration: BoxDecoration(
                color: Colors.blue,
                image: DecorationImage(image: AssetImage("assets/gold.jpg"),fit: BoxFit.cover)
              ),
            ),
            ListTile(
              title: Text('Item 1'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: Text('Item 2'),
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer // how do i close the drawer after click?
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
    );

我希望当我点击应用程序抽屉时 link 它会将我带到一个新页面并关闭应用程序抽屉本身

如果您正在寻找编辑当前页面的方法,例如选项卡,然后在视图之间切换而不实际启动新的页面路由。

我通常做的是:

enum Section
{
    GUEST,
    HOME,
    PAGE_1,
    PAGE_2
}

您的主要构建函数:

@override
Widget build(BuildContext context)
{
    Widget body;

    /// You can easily control the section for example inside the initState where you check
    /// if the user logged in, or other related logic
    switch (section)
    {
        /// This is for example a login page since the user not logged in
        case Section.GUEST:
            break;

        /// Display the home section, simply by
        case Section.HOME:
            body = HomeSection();
            break;

        case Section.PAGE_1:
            body = Page1Section();
            break;

        case Section.PAGE_2:
            body = Page2Section();
            break;
    }

    return Scaffold(
        body: Container(
            child: body,
        ),
        /// Display the drawer for logged in users only
        drawer: section != Section.GUEST ? Drawer(
            // Your drawer
        ) : null,
    );
}

这甚至会保存这些部分的状态,您可以在它们之间快速移动。

重新分级抽屉,你做得对。您只需在上下文中使用导航器弹出即可。只要确保你有正确的上下文。 (而不是传播的)

当然,更改部分很简单:

setState(() => section = Section.HOME);