Last active
August 3, 2019 06:12
-
-
Save extJo/9f39c2afcd50aa1e2354b6ef213f84a7 to your computer and use it in GitHub Desktop.
Flutter count App with setState, StatefulWidget
This file contains 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 { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter State Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Counter(), | |
); | |
} | |
} | |
class Counter extends StatefulWidget { | |
Counter({Key key}) : super(key: key); | |
@override | |
_CounterState createState() => _CounterState(); | |
} | |
class _CounterState extends State<Counter> { | |
int _counter = 0; | |
void _incrementCounter() { | |
setState(() { | |
_counter++; | |
}); | |
} | |
void _decrementCounter() { | |
setState(() { | |
_counter--; | |
}); | |
} | |
Widget _fab(Icon icon, Function action) { | |
return FloatingActionButton( | |
child: icon, | |
onPressed: action, | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Count Sample State Management'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.display1, | |
), | |
], | |
), | |
), | |
floatingActionButton: Column( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
_fab(Icon(Icons.exposure_plus_1), _incrementCounter), | |
_fab(Icon(Icons.exposure_neg_1), _decrementCounter), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment