Created
June 18, 2021 15:49
-
-
Save imaNNeo/95a5cb882846d9c29d1bdc9b15479a69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter/widgets.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
late PageController _pageController; | |
double page = 0.0; | |
@override | |
void initState() { | |
_pageController = PageController( | |
initialPage: 1, | |
viewportFraction: 0.6, | |
); | |
_pageController.addListener(_onScroll); | |
super.initState(); | |
} | |
void _onScroll() { | |
setState(() { | |
page = _pageController.page!; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
AspectRatio( | |
aspectRatio: 2.0, | |
child: PageView( | |
controller: _pageController, | |
children: [ | |
ColoredBox(color: Colors.red), | |
ColoredBox(color: Colors.green), | |
ColoredBox(color: Colors.blue), | |
], | |
), | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
'_pageController.page: ', | |
style: TextStyle( | |
color: Colors.black, | |
fontSize: 18, | |
), | |
), | |
Text( | |
page.toStringAsFixed(2), | |
style: TextStyle( | |
color: Colors.black, | |
fontWeight: FontWeight.bold, | |
fontSize: 28, | |
), | |
) | |
], | |
), | |
], | |
), | |
), | |
); | |
} | |
@override | |
void dispose() { | |
_pageController.removeListener(_onScroll); | |
_pageController.dispose(); | |
super.dispose(); | |
} | |
} | |
class ColoredBox extends StatelessWidget { | |
final Color color; | |
const ColoredBox({Key? key, required this.color}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: color, | |
margin: EdgeInsets.all(24), | |
); | |
} | |
} |
Author
imaNNeo
commented
Jun 18, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment