Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageView PageController initialPage refresh #27020

Closed
shingohu opened this issue Jan 24, 2019 · 12 comments
Closed

PageView PageController initialPage refresh #27020

shingohu opened this issue Jan 24, 2019 · 12 comments

Comments

@shingohu
Copy link

shingohu commented Jan 24, 2019

In MyApp, the children of PageView is dynamic, so I hope that every time the number of children changes, PageView jumps to the initialPage position, and then it doesn't work. I don't know how to update the initialPage position

@robin7331
Copy link

Did you try using the jumpToPage Method of the PageController after inserting a new page?

@shingohu
Copy link
Author

@robin7331 When and where to use this method?

@robin7331
Copy link

Could you share your code of the widget your PageView/Controller is in?

@shingohu
Copy link
Author

shingohu commented Jan 24, 2019

@robin7331 ok

import 'package:flutter/material.dart';

class BannerWidget extends StatefulWidget {
  //item的宽高比例
  final itemRatio;
  final viewportFraction; //page的比例
  final List<Widget> items;
  final EdgeInsetsGeometry margin;
  final virtualCount;
  final PageController pageController;

  BannerWidget({
    Key key,
    @required this.items,
    this.itemRatio: 2 / 2,
    this.viewportFraction: 0.8,
    EdgeInsetsGeometry margin,
    PageController pageController,
    this.virtualCount: 10000,
  })  : assert(items != null, "must set items"),
        margin = margin ?? EdgeInsets.all(10),
        pageController = pageController ??
            PageController(
                viewportFraction: viewportFraction,
                initialPage: items.length < 2 ? 0 : virtualCount,
                keepPage: false),
        
        super(key: key);

  _BannerWidgetState createState() => _BannerWidgetState();
}

class _BannerWidgetState extends State<BannerWidget> {
  void initState() {
    
    
    super.initState();
  }

  @override
  void didUpdateWidget(BannerWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final width = constraints.biggest.width;
        final ratio = widget.itemRatio;
        final viewportFraction = widget.viewportFraction;
        final margin = widget.margin;
        var pageController = widget.pageController;

        final items = widget.items;
        return GestureDetector(
          child: Container(
              width: width,
              height: items.length == 0
                  ? 0
                  : (width * viewportFraction - 2 * (margin.horizontal)) /
                          ratio +
                      margin.vertical,
              child: PageView.builder(
                onPageChanged: (index) {
                  print("index");
                },
                itemBuilder: (context, index) {
                  var realIndex;
                  if (items.length < 2) {
                    realIndex = 0;
                  } else {}

                  return AspectRatio(
                    aspectRatio: ratio,
                    child: Container(
                      color: Colors.blue,
                      margin: margin,
                      width: width * viewportFraction - 2 * (margin.horizontal),
                      child: Center(
                        child: Text("$index"),
                      ),
                    ),
                  );
                },
                controller: pageController,
                itemCount: items.length < 2 ? items.length : null,
              )),
        );
      },
    );
  }
}

how to use

Widget _bannerWidget() {
    return BannerWidget(items: <Widget>[Text("1"),Text("2")],itemRatio: 582/240);
  }

@zoechi
Copy link
Contributor

zoechi commented Jan 24, 2019

Please consider asking support questions in one of the other channels listed at http://flutter.io/support .

@zoechi zoechi added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jan 24, 2019
@dariotrombello
Copy link

Can be closed I guess

@zoechi
Copy link
Contributor

zoechi commented Jan 25, 2019

@dariotrombello Thanks!
I added the waiting for customer response label for that.
If there is no additional response from the original poster, a bot will close the issue after a month (not very reliable) or if not I'll do it manually after a while.

@stefansidler1993
Copy link

check this code in your widget PageController(initialPage: 1, keepPage: false); the magic is setting keepPage

@longxf
Copy link

longxf commented Dec 16, 2019

How to solve it in the end?

@majunchang
Copy link

check this code in your widget PageController(initialPage: 1, keepPage: false); the magic is setting keepPage

It's not going to work

@stefansidler1993
Copy link

check this code in your widget PageController(initialPage: 1, keepPage: false); the magic is setting keepPage

It's not going to work

Why it’s not going to work ?

@pedromassangocode pedromassangocode removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Nov 20, 2020
@github-actions
Copy link

github-actions bot commented Aug 9, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants