BottomNavigationBar, сохраняющий положение смещения и навигацию к деталям. - PullRequest
0 голосов
/ 03 марта 2019

Я новичок во флаттере.Я использую 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

И в своем маршруте '/' я использую MainPage () с 4 bottomNavigationBarItems и 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 и класс Offstage с bottomNavigationBar, но они не использовали MaterialApp -> маршруты, но я использую, и из-за этого возникают конфликты.. вот ссылка: https://medium.com/coding-with-flutter/flutter-case-study-multiple-navigators-with-bottomnavigationbar-90eb6caa6dbf И я хочу, чтобы приложение уже знало эти страницы и не вызывало его постоянно.Пожалуйста, помогите мне

1 Ответ

0 голосов
/ 03 марта 2019

Я нашел решение моего вопроса .. в теле класса 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";
  }
...