1

Im using the Class PageController to navigate between pages in the app, it was working when i set it up for the first time, so then it stoped to work

This is the base screen, were the pagecontroller is called:

import 'package:flutter/material.dart';
import 'package:loja_virtual_pro/models/page_manager.dart';
import 'package:loja_virtual_pro/screens/login/login_screen.dart';
import 'package:provider/provider.dart';
import '../../commom/commom_drawer/custom_drawer.dart';
import '../../commom/commom_drawer/custom_drawer_header.dart';

final pageController = PageController();

class BaseScreen extends StatelessWidget {
  const BaseScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Provider(
      create: (_) => PageManager(pageController),
      child: PageView(
        controller: pageController,
        physics: const NeverScrollableScrollPhysics(),
        children: <Widget>[
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home'),
            ),
          ),
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home1'),
            ),
          ),
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home2'),
            ),
          ),
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home3'),
            ),
          ),
        ],
      ),
    );
  }
}

This is the page manager:

import 'package:flutter/cupertino.dart';

class PageManager {
  PageManager(this._pageController);

  final PageController _pageController;

  int page = 0;

  void setPage(int value) {
    page = value;
    _pageController.jumpToPage(value);
  }
}

And this is the config of the drawer, where the pages is created:

import 'package:flutter/material.dart';
import 'package:loja_virtual_pro/commom/commom_drawer/custom_drawer_header.dart';
import 'package:loja_virtual_pro/commom/commom_drawer/drawer_tile.dart';

class CustomDrawer extends StatelessWidget {
  const CustomDrawer({super.key});

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: Stack(
        children: <Widget>[
          Container(
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                  colors: [Color.fromARGB(255, 203, 236, 241), Colors.white],
                  begin: Alignment.topRight,
                  end: Alignment.bottomCenter),
            ),
          ),
          ListView(
            children: const <Widget>[
              CustomDrawerHeader(),
              Divider(),
              DrawerTile(
                iconData: Icons.home,
                title: 'Início',
                page: 0,
              ),
              DrawerTile(
                iconData: Icons.list,
                title: 'Produtos',
                page: 1,
              ),
              DrawerTile(
                iconData: Icons.playlist_add_check,
                title: 'Meus Pedidos',
                page: 2,
              ),
              DrawerTile(
                iconData: Icons.location_on,
                title: 'Lojas',
                page: 3,
              ),
            ],
          ),
        ],
      ),
    );
  }
}


when i click to navigate to another page the console shows this error:

The following assertion was thrown while handling a gesture: ScrollController attached to multiple scroll views. 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 109 pos 12: '_positions.length == 1'

how do i make this work again?

2 Answers2

1

You are setting your pageController as global variable, that is not the right way and that is causing your issue.

Simply transform your BaseScreen in a Stateful widget :

class BaseScreen extends StatefulWidget {
  const BaseScreen({super.key});

  @override
  State<BaseScreen> createState() => _BaseScreenState();
}

class _BaseScreenState extends State<BaseScreen> {
  late final pageController;

  @override
  void initState() {
    pageController = PageController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Provider(
      create: (_) => PageManager(pageController),
      child: PageView(
        controller: pageController,
        physics: const NeverScrollableScrollPhysics(),
        children: <Widget>[
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home'),
            ),
          ),
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home1'),
            ),
          ),
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home2'),
            ),
          ),
          Scaffold(
            drawer: const CustomDrawer(),
            appBar: AppBar(
              backgroundColor: const Color.fromARGB(255, 4, 125, 141),
              title: const Text('Home3'),
            ),
          ),
        ],
      ),
    );
  }
}

Now your PageView will be able to change correctly pages, and on your ListView you can omit both the controller and primary properties, they are not needed in this usecase.

AJ-
  • 1,638
  • 1
  • 24
  • 49
  • i tryed this too, and keep the same erros =\ – Vinicius Radé Mar 15 '23 at 17:31
  • @ViniciusRadé if you can provide a runnable code snippet where the error can be reproduced, I'll be happy to help you – AJ- Mar 15 '23 at 17:44
  • i can put on GitHub, there is ok for u? – Vinicius Radé Mar 15 '23 at 17:53
  • im trying to solve this error for 1 week – Vinicius Radé Mar 15 '23 at 17:55
  • https://github.com/radevinicius/loja_virtual – Vinicius Radé Mar 15 '23 at 18:00
  • @ViniciusRadé I quickly debugged your code and found the issue, I updated my post, you were setting your controller as global variable, that is wrong. In flutter try to avoid as much as possible global variable, usually they cause more issues than benefits, you can achieve most things without them by scoping correctly your code – AJ- Mar 15 '23 at 20:19
  • OMG ! it worked man ! thank you so much ! i spend like 8 days trying to solve this issue im so glad you help me ! u make my day so much better god bless u man ! – Vinicius Radé Mar 15 '23 at 20:51
  • @ViniciusRadé you are welcome my friend, if you are satisfied with the answer, you can mark it as chosen one! Have a nice day! – AJ- Mar 15 '23 at 20:57
0

Try to provide ScrollController on ListView on CustomDrawer.

ListView(
  controller: ScrollController(),
  children: const <Widget>[
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56