Skip to content

Instantly share code, notes, and snippets.

@iapicca
Created November 5, 2019 13:25
Show Gist options
  • Select an option

  • Save iapicca/f143ef6c43befa8b1abfb9e37ae0b160 to your computer and use it in GitHub Desktop.

Select an option

Save iapicca/f143ef6c43befa8b1abfb9e37ae0b160 to your computer and use it in GitHub Desktop.
PageView + AnimateToPage
import "package:flutter/material.dart";
void main()=> runApp(new MaterialApp(home: MyApp(),),);
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
PageController _controller;
static final List<Widget> _widgets = [
Container(color: Colors.yellowAccent,),
Container(color: Colors.blueAccent,),
Container(color: Colors.redAccent,),
Container(color: Colors.greenAccent,),
];
static const Duration _duration = Duration(milliseconds: 250);
static const Curve _curve = Curves.easeInOut;
@override
void initState() {
super.initState();
_controller = PageController();
}
@override
void dispose() {
_controller.dispose();
_controller = null;
super.dispose();
}
void _animate2Page() => _controller.animateToPage(
(_controller.page.toInt()-3).abs() ,
curve: _curve,
duration: _duration,
);
@override
Widget build(BuildContext context) => Scaffold(
body: PageView(
controller: _controller,
children: _widgets,
),
floatingActionButton: FloatingActionButton(
onPressed: ()=> _animate2Page(),
child: Icon(Icons.fast_forward),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment