In Flutter How To Make PageView Scroll Faster? The Animation Seems To Be Slow And The Inner ListView Cant Response My Vertical Gesture
you can see the details in video I don't know how to work out this problem.Compared with Android Native ViewPager,the animation of the PageView in Flutter seems to be slower and us
Solution 1:
You can define your own instance of PageController and use its methods nextPage() and previousPage(). They have "duration" argument that controls how fast the page view scrolls.
final PageController controller = PageController(
viewportFraction: 0.9,
);
void nextPage() async {
await controller.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
void prevPage() async {
await controller.previousPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
Then apply this controller to your Page View:
return PageView.builder(
physics: NeverScrollableScrollPhysics(),
controller: controller,
...
);
The full code of main.dart:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyPageViewExample(),
);
}
}
class MyPageViewExample extends StatelessWidget {
MyPageViewExample({Key key}) : super(key: key);
final PageController controller = PageController(
viewportFraction: 0.9,
);
void nextPage() async {
await controller.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
void prevPage() async {
await controller.previousPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeOut,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Container(
color: Colors.grey[100],
),
Padding(
padding: EdgeInsets.only(top: 20.0 + MediaQuery.of(context).padding.top, bottom: 20.0),
child: _buildCarousel(context),
),
],
),
);
}
Widget _buildCarousel(BuildContext context) {
return PageView.builder(
physics: NeverScrollableScrollPhysics(),
controller: controller,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int itemIndex) {
return _buildItem(context, itemIndex);
},
itemCount: 5,
);
}
Widget _buildItem(BuildContext context, int itemIndex) {
return Padding(
padding: EdgeInsets.only(left: 4.0, top: 10.0, right: 4.0, bottom: 10.0),
child: GestureDetector(
onPanUpdate: (details) {
if (details.delta.dx < 0) {
nextPage();
}
if (details.delta.dx > 0) {
prevPage();
}
},
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(35.0)),
),
child: Center(
child: Text(
'Item ' + (itemIndex + 1).toString(),
),
),
),
),
);
}
}
Solution 2:
You can control the general physics of the scroll animation.
PageView(
physics: CustomPageViewScrollPhysics(),
Control the scroll behavior:
class CustomPageViewScrollPhysics extends ScrollPhysics {
const CustomPageViewScrollPhysics({ScrollPhysics? parent})
: super(parent: parent);
@override
CustomPageViewScrollPhysics applyTo(ScrollPhysics? ancestor) {
return CustomPageViewScrollPhysics(parent: buildParent(ancestor)!);
}
@override
SpringDescription get spring => const SpringDescription(
mass: 50,
stiffness: 100,
damping: 0.8,
);
}
You should also be able to calculate the duration with mass, stiffness, damping, but I believe having those options is even better suited for your problem. Source
Post a Comment for "In Flutter How To Make PageView Scroll Faster? The Animation Seems To Be Slow And The Inner ListView Cant Response My Vertical Gesture"