BottomNavigationBar保存偏移位置并导航到细节颤动

问题描述 投票:0回答:1

我是个新人。我在项目(和自动身份验证)中使用Scoped Model,我在MaterialApp类上使用路由

@override
    void initState() {

      _model.autoAuthenticate();
      _model.userSubject.listen((bool isAuthenticated){
        setState(() {
          _isAuthenticated = isAuthenticated;
        });
      });
      print('is auth $_isAuthenticated');
      super.initState();
    }

    @override
    Widget build(BuildContext context) {
      return ScopedModel<MainModel>(
        model: _model,
        child: MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: getAdaptiveThemeData(context),
          routes: {
            '/': (BuildContext context) => !_isAuthenticated ? WelcomePage() : MainPage(_model),
            '/info': (BuildContext context) => InfoPage(),
            // another code

在我的'/'路线中,我使用带有4个bottomNavigationBarItem的Main Page()和带有代码的4个不同页面:

    int _currentTab = 0;
  final List<Widget> _children = [
    ScopedModelDescendant<MainModel>(
        builder: (BuildContext context, Widget child, MainModel model){
          return ProfilePage(model);
        },
    ),
    OnStockPage(),
    SendPage(),
    PppPage()
  ];

并在构建小部件中:

@override
  Widget build(BuildContext context) {

    if(widget.model.isUserChanged){
      widget.model.getUserProfile().then((model){

      }).catchError((error){
        print('error is $error');
      });
    }

    print('build first');
    return StreamBuilder<UserModel>(
          stream: _bloc.user,
          builder: (BuildContext context, AsyncSnapshot<UserModel> snapshot){

              return Scaffold(
                appBar: AppBar(
                  title: ScopedModelDescendant<MainModel>(
                    builder: (BuildContext context, Widget child, MainModel model){
                      return ListTile(
                        title: model.isLoading ? Text('name surname', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)) : Text('${model.profile.firstName} ${model.profile.lastName}', style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),),
                        subtitle: Text('Баланс \$0', style: TextStyle(color: Colors.white),),
                      );
                    },
                  ),
                  elevation: 0,
                ),
                drawer: DrawerSettings(),
                body:_children[_currentTab],

                bottomNavigationBar: BottomNavigationBar(
                  currentIndex: _currentTab,
                  items: [
                    BottomNavigationBarItem(
                        icon: Icon(Icons.person, color: Colors.black,),
                        title: Text('Профиль', style: TextStyle(color: Colors.black),)
                    ),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.check_box_outline_blank, color: Colors.black),
                        title: Text('На складе', style: TextStyle(color: Colors.black),)
                    ),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.drive_eta, color: Colors.black),
                        title: Text('Отправленные', style: TextStyle(color: Colors.black),)
                    ),
                    BottomNavigationBarItem(
                        icon: Icon(Icons.shopping_cart, color: Colors.black),
                        title: Text('ПпП', style: TextStyle(color: Colors.black))
                    ),
                  ],
                  onTap: _onTabTapped,
                ),
              );
           },
    );
  }

但当我在第一个底部项目时,我加载所有数据,一切都很好,但当我转到第二个底部项目并返回所有数据再次加载,而不保存,等我发现一些代码与Navigator类和使用bottomNavigationBar的offstage类,但是他们没有使用MaterialApp - > routes,但是我使用并且因为这有冲突..这里是链接:https://medium.com/coding-with-flutter/flutter-case-study-multiple-navigators-with-bottomnavigationbar-90eb6caa6dbf我希望应用程序已经知道这些页面而不是不断调用它。请帮我

routes flutter key bottomnavigationview navigator
1个回答
0
投票

我找到了我的问题的解决方案..在Scaffold类的主体 - >

body:_body(),

Widget _body(){

    return Stack(
      children: List<Widget>.generate(_pageCount, (int index){
        return IgnorePointer(
          ignoring: index != _currentTab,
          child: Opacity(
            opacity: _currentTab == index ? 1 : 0,
            child: Navigator(
              onGenerateRoute: (RouteSettings settings){
                print('settings ${settings.name}');
                if(settings.name == '/'){
                  return MaterialPageRoute(
                      builder: (_) => _page(index),
                      settings: settings
                  );
                }
              },
            ),
          ),
        );
      })
    );
  }

  Widget _page(int index){

    switch (index){
      case 0:
        return ScopedModelDescendant<MainModel>(
          builder: (BuildContext context, Widget child, MainModel model){
            return /*(model.isLoading) ? Center(
            child: CircularProgressIndicator(),
          ) : */ProfilePage(model);
          },
        );
      case 1:
        return OnStockPage();
      case 2:
        return SendPage();
      case 3:
        return PppPage();
    }
    throw "Invalid index $index";
  }
© www.soinside.com 2019 - 2024. All rights reserved.