Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display more than one page with PageView?

I have an app that use PageView and I want to make it responsive – such that on a tablet, it shows 3 pages and with a smartphone, it shows only one:

enter image description here

How can I change the number of pages shown in a PageView?

like image 347
Dominik Roszkowski Avatar asked Mar 26 '19 10:03

Dominik Roszkowski


People also ask

How do I get index of PageView in flutter?

You can use PageView. builder, it will give you page index.

What is PageController in flutter?

A page controller lets you manipulate which page is visible in a PageView. In addition to being able to control the pixel offset of the content inside the PageView, a PageController also lets you control the offset in terms of pages, which are increments of the viewport size.


2 Answers

You can control how many pages are displayed in a PageView through a PageController

var controller = PageController(viewportFraction: 1 / 3);

Then pass it to your PageView:

PageView(
  controller: controller,
  ...
), 
like image 159
Rémi Rousselet Avatar answered Sep 24 '22 15:09

Rémi Rousselet


You have to use LayoutBuilder to check the max width and then you can set the PageController(viewportFraction: ); accordingly.

Here is an example:

  PageController pageController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: LayoutBuilder(builder: (context, constrains) {
        if (constrains.maxWidth < 600) {
          pageController = PageController(viewportFraction: 1.0);
        } else {
          pageController = PageController(viewportFraction: 0.3);
        }
        return PageView.builder(
          controller: pageController,
          itemCount: places.length,
          itemBuilder: (context, index) {
            // return your view for pageview
          },
        );
      }),
    );
  }
like image 43
divyanshu bhargava Avatar answered Sep 25 '22 15:09

divyanshu bhargava