单击返回按钮时,在底部导航栏中更改动画状态



我在应用程序中使用了此底部导航栏库,并且可以很好地工作。但是我想维护A Back Navigation 在Android手机中的后退按钮,例如在Google Play商店底部导航中,如果我们在底部导航中导航,动画会更改,并且如果我们返回返回动画之前的后退按钮。

我尝试了这篇文章,这在某种程度上做了我想要的,但是动画不起作用。

这是我的底部导航栏,

的状态小部件
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  int currentIndex;
  final PageStorageBucket bucket = PageStorageBucket();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    currentIndex = 0;
  }
  void changePage(int index) {
    setState(() {
      currentIndex = index;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey.shade300,
      resizeToAvoidBottomInset : false,
      body:
        new Stack(
          children: <Widget>[
            new Offstage(
              offstage: currentIndex != 0,
              child: new TickerMode(
                enabled: currentIndex == 0,
                child: new MaterialApp(home: new HomePage()),
              ),
            ),
            new Offstage(
              offstage: currentIndex != 1,
              child: new TickerMode(
                enabled: currentIndex == 1,
                child: new MaterialApp(home: new StayPage()),
              ),
            ),
            new Offstage(
              offstage: currentIndex != 2,
              child: new TickerMode(
                enabled: currentIndex == 2,
                child: new MaterialApp(home: new TravelPage()),
              ),
            ),
            new Offstage(
              offstage: currentIndex != 3,
              child: new TickerMode(
                enabled: currentIndex == 3,
                child: new MaterialApp(home: new MorePage()),
              ),
            ),
          ],
        ),
      floatingActionButton: Visibility(
        visible: _isVisible,
        child: Container(
          height: 100.0,
          width: 85.0,
//        margin: EdgeInsets.only(bottom: 5.0),
          child: FittedBox(
            child: FloatingActionButton.extended(
              onPressed: () {
                setState(() {});
              },
              elevation: 20.0,
              icon: Icon(Icons.add),
              label: Text(
                "Plan",
                style: TextStyle(
                  fontFamily: "OpenSansBold",
                ),
              ),
              backgroundColor: Colors.red,
            ),
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      bottomNavigationBar: Visibility(
        visible: _isVisible,
        child: BubbleBottomBar(
          hasInk: true,
          fabLocation: BubbleBottomBarFabLocation.end,
          opacity: .2,
          currentIndex: currentIndex,
          onTap: changePage,
          elevation: 15,
          items: <BubbleBottomBarItem>[
            BubbleBottomBarItem(
                backgroundColor: Colors.red,
                icon: Icon(
                  Icons.home,
                  color: Colors.black,
                ),
                activeIcon: Icon(
                  Icons.home,
                  color: Colors.red,
                ),
                title: Text(
                  "Home",
                  style: TextStyle(
                    fontFamily: "OpenSans",
                  ),
                )),
            BubbleBottomBarItem(
              backgroundColor: Colors.deepPurple,
              icon: Icon(
                Icons.hotel,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.hotel,
                color: Colors.deepPurple,
              ),
              title: Text(
                "Stay",
                style: TextStyle(
                  fontFamily: "OpenSans",
                ),
              ),
            ),
            BubbleBottomBarItem(
              backgroundColor: Colors.indigo,
              icon: Icon(
                Icons.card_travel,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.card_travel,
                color: Colors.indigo,
              ),
              title: Text(
                "Travel",
                style: TextStyle(
                  fontFamily: "OpenSans",
                ),
              ),
            ),
            BubbleBottomBarItem(
              backgroundColor: Colors.green,
              icon: Icon(
                Icons.more,
                color: Colors.black,
              ),
              activeIcon: Icon(
                Icons.more,
                color: Colors.green,
              ),
              title: Text(
                "More",
                style: TextStyle(
                  fontFamily: "OpenSans",
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

我不必使用此库,我只想实现一个底部导航栏,并且在使用Android Back Back按钮进行后退时,动画必须工作。

任何类型的帮助将不胜感激!谢谢!

解决了我的问题。我使用了Swav Kulinski的中型帖子,我之前在问题中指出。

代码如下,

class NavBar extends StatefulWidget {
  @override
  _NavBarState createState() => _NavBarState();
}
class _NavBarState extends State<NavBar> {
  final navigatorKey = GlobalKey<NavigatorState>();
  int currentIndex;
  final PageStorageBucket bucket = PageStorageBucket();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    currentIndex = 0;
  }
  void changePage(int index) {
    navigatorKey.currentState.pushNamed(pagesRouteFactories.keys.toList()[index]);
    setState(() {
      currentIndex = index;
    });
  }
  Future<bool> _onWillPop() {
    if(currentIndex == 3){
      changePage(2);
    } else if(currentIndex == 2){
      changePage(1);
    } else if(currentIndex == 1){
      changePage(0);
    } else if(currentIndex == 0){
      return Future.value(false);
    }
    return Future.value(true);
  }
  final pagesRouteFactories = {
    "/": () => MaterialPageRoute(
      builder: (context) => Center(
        child: Text("HomePage",style: Theme.of(context).textTheme.body1,),
      ),
    ),
    "takeOff": () => MaterialPageRoute(
      builder: (context) => Center(
        child: Text("Take Off",style: Theme.of(context).textTheme.body1,),
      ),
    ),
    "landing": () => MaterialPageRoute(
      builder: (context) => Center(
        child: Text("Landing",style: Theme.of(context).textTheme.body1,),
      ),
    ),
    "settings": () => MaterialPageRoute(
      builder: (context) => Center(
        child: Text("Settings",style: Theme.of(context).textTheme.body1,),
      ),
    ),
  };
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WillPopScope(
        onWillPop: _onWillPop,
        child: Scaffold(
          body: _buildBody(),
          bottomNavigationBar: _buildBottomNavigationBar(context),
        ),
      ),
    );
  }
  Widget _buildBody() =>
      MaterialApp(
          navigatorKey: navigatorKey,
          onGenerateRoute: (route) => pagesRouteFactories[route.name]()
      );
  Widget _buildBottomNavigationBar(context) => BottomNavigationBar(
    items: [
      _buildBottomNavigationBarItem("Home", Icons.home),
      _buildBottomNavigationBarItem("Take Off", Icons.flight_takeoff),
      _buildBottomNavigationBarItem("Landing", Icons.flight_land),
      _buildBottomNavigationBarItem("Settings", Icons.settings)
    ],
    currentIndex: currentIndex,
    onTap: changePage,
  );
  _buildBottomNavigationBarItem(name, icon) => BottomNavigationBarItem(
      icon: Icon(icon),
      title: Text(name),
      backgroundColor: Colors.black45,
      activeIcon: Icon(icon)
  );
}

我不知道这是否是正确的方法。如果有人想建议我更好的选择,请告诉我。

最新更新