【问题标题】:How to avoid creating a new instance when clicking a page again and again in flutter?如何避免在flutter中一次又一次单击页面时创建新实例?
【发布时间】:2021-07-18 06:11:18
【问题描述】:

我有一个带有导航抽屉的颤振应用程序。它具有三个重定向到相应屏幕的路由。但是,当我单击当前所在的特定屏幕时,会重复创建该屏幕的新实例。我不希望一次又一次地创建当前选定页面的实例。以 Gmail 的抽屉式导航为例。当我单击所选页面时,它不会创建新实例。我想拥有那个功能。谁能帮我解决这个问题?

class NavList extends StatefulWidget {
  @override
  _NavListState createState() => _NavListState();
}

class _NavListState extends State<NavList> {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Container(
        width: MediaQuery.of(context).size.width * 0.70, //70% of the screen
        child: Column(
          // padding: EdgeInsets.zero,
          children: <Widget>[
            Expanded(
              child: Column(
                children: [
                  UserAccountsDrawerHeader(
                    decoration: BoxDecoration(
                        image: DecorationImage(
                      image: AssetImage('images/orange.png'),
                      fit: BoxFit.cover,
                    )),
                    arrowColor: Colors.deepOrangeAccent[700],
                    accountName: Text(''),
                    accountEmail: Text( 
                      'username@gmail.com',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 20.0,
                      ),
                    ),
                    currentAccountPicture: CircleAvatar(
                      radius: 22.0,
                      backgroundColor: Colors.deepOrangeAccent[700],
                      backgroundImage: AssetImage('images/profile.png'),
                    ),
                  ),
                  ListTile(
                    hoverColor: Colors.deepOrange.shade300,
                    title: Text('Sellers'),
                    leading: IconButton(
                      icon: Icon(Icons.people),
                      onPressed: () async {
                        Navigator.pop(context);
                        Navigator.pushNamed(context, SellerScreen.id);
                      },
                    ),
                    onTap: () async {
                      Navigator.pop(context);
                      Navigator.pushNamed(context, SellerScreen.id);
                    },
                  ),
                  ListTile(
                    hoverColor: Colors.deepOrange.shade300,
                    title: Text('Shops'),
                    leading: IconButton(
                      icon: Icon(Icons.shop),
                      onPressed: () async {
                        Navigator.pop(context);
                        Navigator.pushNamed(context, ShopScreen.id);
                      },
                    ),
                    onTap: () {
                      Navigator.pop(context);
                      Navigator.pushNamed(context, ShopScreen.id);
                    },
                  ),
                  ListTile(
                    hoverColor: Colors.deepOrange.shade300,
                    title: Text('Orders'),
                    leading: IconButton(
                      icon: Icon(Icons.monetization_on),
                      onPressed: () async {
                        Navigator.pop(context);
                        Navigator.pushNamed(context, OrderScreen.id);
                      },
                    ),
                    onTap: () async {
                      Navigator.pop(context);
                      Navigator.pushNamed(context, OrderScreen.id);
                    },
                  ),
                  ListTile(
                    hoverColor: Colors.deepOrange.shade300,
                    title: Text('Logout'),
                    leading: IconButton(
                      icon: Icon(Icons.logout),
                      onPressed: () async {
                        Navigator.pop(context);
                        Navigator.pushNamed(context, LoginScreen.id);
                      },
                    ),
                    onTap: () async {
                      Navigator.pop(context);
                      Navigator.pushNamed(context, LoginScreen.id);
                    },
                  ),
                ],
              ),
            ),
            Container(
              child: Padding(
                padding: EdgeInsets.only(bottom: 5.0),
                child: Text(
                  'Version 1.0.1',
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter stack flutter-layout state dart-pub


    【解决方案1】:

    将此代码粘贴到 onTap 部分:

    ModalRoute.of(context).settings.name != '/second'
                    ? Navigator.popAndPushNamed(context, '/second')
                    : print('You can not push this page again');
    

    此代码比较当前页面和新页面的名称。如果它们相同,它会打印错误,如果它们不同,它将推送新页面并弹出上一页。

    【讨论】:

      猜你喜欢
      • 2015-04-15
      • 2023-01-20
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      相关资源
      最近更新 更多