Skip to content

Instantly share code, notes, and snippets.

@rayliverified
Created April 20, 2020 22:49
Show Gist options
  • Save rayliverified/7b5710b344431457753253625a596158 to your computer and use it in GitHub Desktop.
Save rayliverified/7b5710b344431457753253625a596158 to your computer and use it in GitHub Desktop.
Flutter.dev Example 3 - Counter
import 'package:flutter/material.dart';
class Counter extends StatefulWidget {
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
double val;
void initState() {
super.initState();
val = 0;
}
void change() {
setState(() {
val += 1;
});
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(child: Text('$val'))),
MaterialButton(
color: Theme.of(context).primaryColor,
child: Text(
'Add',
style: TextStyle(color: Colors.white),
),
onPressed: () => change(),
),
],
),
),
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Center(
child: Container(
child: Counter(),
),
),
);
}
}
Future<void> main() async {
runApp(MyApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment