每次按下导航项时,底部导航栏都会重新加载所有小部件 [英] Bottom navigation bar is reloading all the widgets each time I press a navigation item

查看:86
本文介绍了每次按下导航项时,底部导航栏都会重新加载所有小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码几乎是底部导航栏的官方flutter github示例中的复制粘贴.问题是,在包含视图的小部件的打开处,所有内容都已预加载.每次按下底部栏元素时,都会重新加载所有视图.换句话说,当我按第一个项目时,日志显示出其他(第二个和第三个)正在进行网络呼叫.当我按下第二个按钮时,日志显示我什至第一个正在拨打网络电话.调试时,我发现每次都从底部栏中选择一个小部件,该小部件会重新构建(有一个对方法buid的调用).这是正常现象吗?这是包含小部件的代码

The following code is almost a copy-paste from the official flutter github example for the bottom navigation bar. The problem is, at the opening of the widget containing the views, everything is preloaded. And each time I press a bottom bar element I have all of the views reloaded. In other word when I press on the first item, the log shows me that the other ones(second and third) are making network calls. When I press the second, the log shows me that even the first one is making network calls. When I debug I figured out that at each time I select a widget from the bottom bar the widget that contains it rebuilds(there is a call to the method buid). Is it a normal behavior? this is the code of the containing widget

class MainScreen extends StatefulWidget{

    @override
      State<StatefulWidget> createState()=> MainScreenState();

}


class MainScreenState extends State<MainScreen> with TickerProviderStateMixin{
      GlobalKey<ScaffoldState> scaffoldState = new GlobalKey();
      int _currentSelection = 0;
      BottomNavigationBarType _navigationBarType = BottomNavigationBarType.fixed;
      Li

    st<NavigationIconView> _navigationIcons;


    @override
      void initState() {


    super.initState();
        _navigationIcons = <NavigationIconView>[
          new NavigationIconView(


    icon: const Icon(Icons.home),
        title: 'Главная',
        vsync: this

      ),
      new NavigationIconView(
        icon: const Icon(Icons.map),
        title: 'Квесты',
        vsync: this
      ),
      new NavigationIconView(
        icon: const Icon(Icons.dehaze),
        title: 'Профиль',
        vsync: this
      )
    ];

    for(NavigationIconView v in _navigationIcons)
      v.controller.addListener(_rebuild);

    _navigationIcons[_currentSelection].controller.value = 1.0;
      }


    @override
      void dispose() {
        for(NavigationIconView v in _navigationIcons)
          v.controller.dispose();
        super.dispose();
    }

     @override
      Widget build(BuildContext context) {
        final BottomNavigationBar botNavBar = new BottomNavigationBar(
          items: _navigationIcons
              .map((NavigationIconView navigationView) => navigationView.item)
              .toList(),
          currentIndex: _currentSelection,
          fixedColor: Colors.green,
          type: _navigationBarType,
          onTap: (int index) {
            setState(() {
              _navigationIcons[_currentSelection].controller.reverse();
              _currentSelection = index;
              _navigationIcons[_currentSelection].controller.forward();
              print('pressed : $_currentSelection');
            });
          },
        );
        return new Scaffold(
          key: scaffoldState,
          body: new  Center(
            key: new Key('Main view container'),
            child: new FutureBuilder<Widget>(
                future: _buildTransitionsStack(),
                builder: (BuildContext context, AsyncSnapshot<Widget> snapshot){
                  if(!snapshot.hasError) return snapshot.data;
                  else{
                    print('sh3t happened in main : ${snapshot.error}');
                  }
                }
            ),
          ),
      bottomNavigationBar: botNavBar,

    );
  }


    Future<Widget> _buildTransitionsStack() {
        final List<FadeTransition> transitions = <FadeTransition>[];

        return _showMain().then((mainWidget){
          transitions.add(_navigationIcons[0]


    .transition(_navigationBarType,mainWidget, context));
          print('size ${transitions.length}');
        }).then((_){
          transitions.add(_navigationIcons[1].transition(_navigationBarType,
              _showQuest(), context));

          transitions.add(_navigationIcons[2].transition(_navigationBarType,
              _showProfile(), context));

          transitions.sort((FadeTransition a, FadeTransition b) {
          final Animation<double> aAnimation = a.opacity;
          final Animation<double> bAnimation = b.opacity;
          final double aValue = aAnimation.value;
          final double bValue = bAnimation.value;
          return aValue.compareTo(bValue);
        });

          return new Stack(children: transitions);
        });
        }

  Future<Widget> _showMain(){
     return _getToken().then((token){
      return new FeedView(token);
    });
  }

  Widget _showQuest(){
//    return DetailableListScreen(ViewModelType.QUEST);

    return new QuestScreen();

  }

  Widget _showProfile(){
    return new Text('profile');
//    TODO
  }


  void _rebuild() {
    setState(() {

    });
  }

  Future<String> _getToken() async{
    return await SharedPreferences.getInstance()
        .then((SharedPreferences sp)=> sp.getString(TOKEN)
    );
  }
}}

推荐答案

我绝对建议观看 @amrnt 视频.但是对于那些想要直接答案的人,您需要实例化 PageStorageKey 用于底部栏的每一页,然后使每一页通过构造函数接收其PageStorageKey.

I definitively recommend watching @amrnt video. But for those out there who want the straight answer, you need to instantiate a PageStorageKey for each page of your bottom bar sections and then make each page to receive its PageStorageKey via constructor.

这篇关于每次按下导航项时,底部导航栏都会重新加载所有小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆