-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
Comments
Did you try using the jumpToPage Method of the PageController after inserting a new page? |
@robin7331 When and where to use this method? |
Could you share your code of the widget your PageView/Controller is in? |
@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);
} |
Please consider asking support questions in one of the other channels listed at http://flutter.io/support . |
Can be closed I guess |
@dariotrombello Thanks! |
check this code in your widget PageController(initialPage: 1, keepPage: false); the magic is setting keepPage |
How to solve it in the end? |
It's not going to work |
Why it’s not going to work ? |
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 |
In
MyApp
, the children ofPageView
is dynamic, so I hope that every time the number of children changes,PageView
jumps to theinitialPage
position, and then it doesn't work. I don't know how to update theinitialPage
positionThe text was updated successfully, but these errors were encountered: