skip to Main Content

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

Answers


  1. Try to provide ScrollController on ListView on CustomDrawer.

    ListView(
      controller: ScrollController(),
      children: const <Widget>[
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search