Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll or jump to position of PageView.builder or PageController in Flutter?

Issue: Unable to scroll to POSITION after loading the Pageviews using PageController *

like ViewPager scroll to specific page in Android

Widget _buildCarousel(BuildContext context, int selectedIndex) {

    PageController controller = PageController(viewportFraction: 1, keepPage: true);
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        SizedBox(
          // you may want to use an aspect ratio here for tablet support
          height: 400.0,
          width: 240,
          child: PageView.builder(
            itemCount: assetImageList.length,
            controller: controller,
            itemBuilder: (BuildContext context, int itemIndex) {
              return _buildCarouselItem(context, selectedIndex, itemIndex);
            },
          ),
        )
      ],
    );
  }
like image 836
jazzbpn Avatar asked May 09 '19 07:05

jazzbpn


People also ask

How do I make my PageView scrollable on flutter?

To make stuff scrollable in general you can wrap the widget (>>right click on the widget you want to make scrollable>>refactor>>wrap with widget) in a SingleChildScrollView() .

What is the default scroll direction for PageView *?

The axis along which the page view scrolls. Defaults to Axis. horizontal.

What is Page snapping in flutter?

A plugin that allows you to make a pageView that scrolls a certain number of pages based on the scroll velocity, and then snaps to the nearest page. Example.


2 Answers

You can use jumpTo() method to scroll position for PageView. I have create one changePageViewPostion() method in below example:

import 'package:flutter/material.dart';

class MyPageView extends StatefulWidget {
  createState() {
    return StateKeeper();
  }
}

class StateKeeper extends State<MyPageView> {

  PageController controller = PageController(viewportFraction: 1, keepPage: true);
  var currentPageValue = 0.0;
  var mItemCount = 10;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller.addListener(() {
      setState(() {
        currentPageValue = controller.page;
      });
    });
  }

  void changePageViewPostion(int whichPage) {
    if(controller != null){
      whichPage = whichPage + 1; // because position will start from 0
      double jumpPosition = MediaQuery.of(context).size.width / 2;
      double orgPosition = MediaQuery.of(context).size.width / 2;
      for(int i=0; i<mItemCount; i++){
        controller.jumpTo(jumpPosition);
        if(i==whichPage){
          break;
        }
        jumpPosition = jumpPosition + orgPosition;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('PageView position change'),
      ),
      body: PageView.builder(
        controller: controller,
        itemBuilder: (context, position) {
          return Container(
            color: position % 2 == 0 ? Colors.blue : Colors.pink,
            child: Column(
              children: <Widget>[

                Center(
                  child: Text(
                    "Page " + (position + 1).toString(),
                    style: TextStyle(color: Colors.white, fontSize: 22.0),
                  ),
                ),

                Align(
                  alignment: FractionalOffset.bottomCenter,
                  child: Padding(padding: EdgeInsets.only(bottom: 20),
                    child: FloatingActionButton(
                        elevation: 0.0,
                        child: new Icon(Icons.check),
                        backgroundColor: new Color(0xFFE57373),
                        onPressed: (){
                          changePageViewPostion(5);
                        }
                    ),),
                ),

              ],
            ),
          );
        },
        itemCount: mItemCount,
      )
    );
  }


}

We can get current position with controller as below:

controller.addListener(() {
      setState(() {
        currentPageValue = controller.page.toInt();
        print((currentPageValue + 1).toString());
      });
    });

Hope it helps :)

like image 105
android Avatar answered Sep 23 '22 19:09

android


Finally found the answer. Just set the initialPage: mSelectedPosition attribute like:

child: PageView.builder(
        itemCount: mTemplateModelList.length,
        controller: PageController(initialPage: mSelectedPosition, keepPage: true, viewportFraction: 1),
        itemBuilder: (BuildContext context, int itemIndex) {
          return _buildCarouselItem(context, selectedIndex, itemIndex);
        },
      ),

OR if you want to scroll the page after the button is clicked then, you can use jumpTo() method using PageController which is clearly mentioned below by another user: @android.

like image 33
jazzbpn Avatar answered Sep 24 '22 19:09

jazzbpn