0

I have in my code a TabBar that if login is true it will display certain screens and if it is false it will display others, when I log in and I go to the option to log off to return to the login screen everything is fine but I can no longer navigate between tabbar screens after that because of this error:

ScrollController attached to multiple scroll views. 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 108 pos 12: '_positions.length == 1'

Here is my Tabbar code:

class TabbarCubitView extends StatelessWidget {
  final bool isLoggedIn;

  const TabbarCubitView({Key? key, required this.isLoggedIn}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final tabbarCubit = context.watch<TabbarCubit>();
    return Scaffold(
      extendBody: true,
      body: PageView(
          controller: tabbarCubit.controller,
          onPageChanged: (index) {
            tabbarCubit.changeTab(index);
          },
          children: isLoggedIn
              ? const [
                  ChamadosView(),
                  ArtigosView(),
                  ChatView(),
                  ConfiguracaoDeConta(),
                ]
              : const [
                  TelaLogin(),
                  ArtigosView(),
                  ChatView(),
                ]),
      bottomNavigationBar: BlocBuilder<TabbarCubit, int>(
        builder: (context, state) {
          final bottomNavBarItems = <BottomNavigationBarItem>[
            const BottomNavigationBarItem(
              icon: Icon(Icons.paste),
              label: 'Chamados',
            ),
            const BottomNavigationBarItem(
              icon: Icon(Icons.help),
              label: 'Ajuda',
            ),
            const BottomNavigationBarItem(
              icon: Icon(Icons.chat),
              label: 'Chat',
            ),
          ];

          if (isLoggedIn == true) {
            bottomNavBarItems.add(
              const BottomNavigationBarItem(
                icon: Icon(Icons.settings),
                label: 'Configurações',
              ),
            );
          }

          return BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            currentIndex: state,
            onTap: (value) {
              context.read<TabbarCubit>().jumpToPage(value);
            },
            elevation: 0,
            selectedItemColor: roxoPadrao,
            unselectedItemColor: Colors.grey,
            items: bottomNavBarItems,
          );
        },
      ),
    );
  }
}

Here is the controller I use to move from one screen to another:

class TabbarCubit extends Cubit<int> {
 final PageController controller;

 TabbarCubit(this.controller) : super(0);

 void jumpToPage(int index) {
   controller.animateToPage(
     index,
     duration: const Duration(milliseconds: 300),
     curve: Curves.linear,
   );
   emit(index);
 }

 void changeTab(int newIndex) {
   emit(newIndex);
 }
}

And here where I log off mine sets my isLoggedIn to false. Navigator.pushReplacementNamed(context, Routes.tabbarCubitViewSemLogar);

 logoffEmpresa(BuildContext context) async {
   try {
     var resposta = await CustomEmpresaContaHttp.logoffEmpresa(context);
     if (resposta != null) {
       SharedPreferences sharedPreferences =
           await SharedPreferences.getInstance();
       await sharedPreferences.clear();

       context.read<TabbarCubit>().jumpToPage(0);

       Navigator.pushReplacementNamed(
           context, Routes.tabbarCubitViewSemLogar);
     } else {
       FlushBarComponente.mostrar(
           context, 'erro ao sair da conta', Icons.check, verdePadrao);
     }
   } catch (exception) {
     FlushBarComponente.mostrar(
         context,
         'Ocorreu um erro. Por favor, tente novamente.',
         Icons.warning_amber,
         vermelhoPadrao);
   }
 }
Natan
  • 9
  • 2

1 Answers1

0

You can solve this problem by using two different controllers. Controllers are created once and can be used in one place. In your case, because the page length has changed, you will get a length error even if you solve the multiple error. You can try the isLoggedIn condition in the controller.

Hakkı Alkan
  • 306
  • 1
  • 5