Skip to content

Instantly share code, notes, and snippets.

@jediyeti
Created January 29, 2020 12:12
Show Gist options
  • Save jediyeti/c3d058e79df2dc5ba807b123267e752b to your computer and use it in GitHub Desktop.
Save jediyeti/c3d058e79df2dc5ba807b123267e752b to your computer and use it in GitHub Desktop.
Stateful widget
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Flutter Tutorial',
home: TutorialHome(),
));
}
class TutorialHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Counter()),
);
}
}
class Counter extends StatefulWidget {
// Creates State
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _counter = 0;
void _increment() {
// triggers build() after assigning new values
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: _increment,
child: Text('Increment'),
),
SizedBox(width: 16.0),
Text(
'Count: $_counter',
style: TextStyle(fontSize: 16.0),
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment