Skip to content

Instantly share code, notes, and snippets.

@vladnicula
Created May 29, 2021 06:41
Show Gist options
  • Save vladnicula/b097c062b2ad57537d228a6441ae1d25 to your computer and use it in GitHub Desktop.
Save vladnicula/b097c062b2ad57537d228a6441ae1d25 to your computer and use it in GitHub Desktop.
Counter App
// from https://flutter.dev/docs/development/ui/widgets-intro
import 'package:flutter/material.dart';
class Counter extends StatefulWidget {
// This class is the configuration for the state.
// It holds the values (in this case nothing) provided
// by the parent and used by the build method of the
// State. Fields in a Widget subclass are always marked
// "final".
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _counter = 0;
void _increment() {
setState(() {
// This call to setState tells the Flutter framework
// that something has changed in this State, which
// causes it to rerun the build method below so that
// the display can reflect the updated values. If you
// change _counter without calling setState(), then
// the build method won't be called again, and so
// nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called,
// for instance, as done by the _increment method above.
// The Flutter framework has been optimized to make
// rerunning build methods fast, so that you can just
// rebuild anything that needs updating rather than
// having to individually changes instances of widgets.
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _increment,
child: Text('Increment'),
),
SizedBox(width: 16),
Text('Count: $_counter'),
],
);
}
}
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: Center(
child: Counter(),
),
),
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment