如何避免在 flutter 中一次又一次单击页面时创建新实例?

How to avoid creating a new instance when clicking a page again and again in flutter?

我有一个带有导航抽屉的 flutter 应用程序。它具有三个重定向到相应屏幕的路由。但是,当我单击当前所在的特定屏幕时,会重复创建该屏幕的新实例。我不想一次又一次地创建当前选定页面的实例。以 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),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

将此代码粘贴到 onTap 部分:

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

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