Created
June 18, 2021 16:02
-
-
Save imaNNeo/36a636ea420b29e44d22f518d62d2fd0 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; | |
List<Color> colors = [ | |
Colors.red, | |
Colors.green, | |
Colors.blue, | |
]; | |
@override | |
void initState() { | |
_pageController = PageController( | |
initialPage: 1, | |
viewportFraction: 0.4, | |
); | |
_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.builder( | |
controller: _pageController, | |
itemBuilder: (context, index) { | |
return ColoredBox( | |
color: colors[index], | |
offset: (index - page).clamp(-1, 1).toDouble(), | |
); | |
}, | |
itemCount: colors.length, | |
), | |
), | |
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; | |
final double offset; | |
const ColoredBox({ | |
Key? key, | |
required this.color, | |
required this.offset, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: color, | |
margin: EdgeInsets.all(24), | |
child: Center( | |
child: Text( | |
offset.toStringAsFixed(2), | |
style: TextStyle( | |
color: Colors.white, | |
fontWeight: FontWeight.bold, | |
fontSize: 28, | |
), | |
), | |
), | |
); | |
} | |
} |
Author
imaNNeo
commented
Jun 18, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment