首页 > 解决方案 > Flutter/Dart - Navigator.pop(context) 在添加 BottomNavigationBar 后停止工作

问题描述

我的堆栈中的第一页有一个BottomNavigationBar. 第一个选项卡调用 a FutureBuilder,它返回 a PageView.builder。滑动它导航到第二页。

以前,点击Navigator.pop(context)第二页让我回到第一页。但是自从向第二页添加了一个 BottomNavigationBar 后,该按钮不再弹回。相反,我得到一个黑屏。

为什么将 a 添加BottomNavigationBar到第二页会阻止它弹回第一页?

奇怪的是,使用我手机上的常规 Android 后退按钮确实有效。使用不应该Navigator.pop(context)与使用 Android 手机上的后退按钮相同吗?如果不是,我怎样才能让我的按钮的行为与 Android 的后退按钮相同?

这是第二页的代码;

class HomeReply extends StatefulWidget {

  @override
  _HomeReplyState createState() => new _HomeReplyState();
}

class _HomeReplyState extends State<HomeReply> {

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

  static List<Widget> _myPages = <Widget>[
    ReplyStage(),
    Account(),
  ];
  
  int _selectedIndex = 0;

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    var socialProvider = Provider.of<SocialProvider>(context);
    return MaterialApp(
      home: FutureBuilder(
      future: Future.wait([socialProvider.loadCurrentName(), socialProvider.loadCurrentAvatar()]),
      builder: (context, snapshot) {
      if (snapshot.hasData) {
      return
      Scaffold(
        appBar: AppBar(
          title: Text(
            "Back to "Original Post",
            ),

          leading: new IconButton(
            icon: new Icon(
              Icons.arrow_back,             
            ),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
          ),
        body: _myPages[_selectedIndex],
        bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
              ),
              title: Text('Home'),
            ),
            BottomNavigationBarItem(
              icon: Consumer<SocialProvider>(
                  builder: (context, socialProvider, child) {
                    return Image.network(snapshot.data[1],
                    );
                  }),
              title: Consumer<SocialProvider>(
                  builder: (context, socialProvider, child) {
                    return Text(snapshot.data[0]);
                  }
              ),
            ),
          ],
          currentIndex: _selectedIndex,
          onTap: _onItemTapped,
         ),
      );
    } else if(snapshot.hasError) {
    return Center(child: CircularProgressIndicator());
    } else{
    return Center(child: CircularProgressIndicator());
    }
    }
      ),
    );
  }
}

标签: flutterdarttabsfuture

解决方案


看来问题BottomNavigationBar毕竟不是问题。问题在这里重复MaterialApp

return MaterialApp(
      home: FutureBuilder

一旦我将其更改return FutureBuilderNavigator.pop(context)恢复正常工作。


推荐阅读