Created
August 23, 2020 17:50
-
-
Save rubenvereecken/90894d58f2066d00d602bafb8863dcd4 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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
static const Widget _home = HomePage(); | |
@override | |
Widget build(BuildContext context) => MaterialApp( | |
home: _home, | |
); | |
} | |
class HomePage extends StatefulWidget { | |
const HomePage(); | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
PageController _controller; | |
List<int> _lenght = [0, 1, 2]; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = PageController(initialPage: 1, viewportFraction: 0.8); | |
} | |
@override | |
void dispose() { | |
_controller?.dispose(); | |
super.dispose(); | |
} | |
void _add() => setState(() { | |
_lenght.add(_lenght.length); | |
}); | |
void _remove() => setState(() { | |
if (_lenght.length > 1) { | |
_lenght.removeAt(0); | |
_controller.jumpToPage(_controller.page.floor() - 1); | |
} | |
}); | |
void _reset() => setState(() { | |
_lenght = [0, 1, 2]; | |
}); | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
appBar: AppBar( | |
actions: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.only(right: 20.0), | |
child: IconButton( | |
icon: const Icon(Icons.refresh), | |
onPressed: _reset, | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.only(right: 20.0), | |
child: IconButton( | |
icon: const Icon(Icons.remove), | |
onPressed: _remove, | |
), | |
), | |
Padding( | |
padding: const EdgeInsets.only(right: 20.0), | |
child: IconButton( | |
icon: const Icon(Icons.add), | |
onPressed: _add, | |
), | |
), | |
], | |
), | |
body: PageView( | |
controller: _controller, | |
children: <Widget>[ | |
for (int i in _lenght) | |
Padding( | |
padding: const EdgeInsets.all(32.0), | |
child: Container( | |
color: Colors.blueAccent[100], | |
alignment: Alignment.center, | |
child: Text( | |
'$i', | |
style: const TextStyle(fontSize: 32), | |
), | |
), | |
), | |
], | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment