Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Created December 4, 2019 16:17
Show Gist options
  • Select an option

  • Save r3dm1ke/5e6150a9255863fe299f0aa9df469e2d to your computer and use it in GitHub Desktop.

Select an option

Save r3dm1ke/5e6150a9255863fe299f0aa9df469e2d to your computer and use it in GitHub Desktop.
Using Stateful component in Flutter
// The sole reason we keep this extra component is
// because runApp will only take a StatelessWidget as its argument
class TODOApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return TODO();
}
}
// Here we are defining a StatefulWidget
class TODO extends StatefulWidget {
// Every stateful widget must override createState
@override
State<StatefulWidget> createState() {
return TODOState();
}
}
// This is the state for then TODO widget
class TODOState extends State<TODO> {
// We define the properties for the widget in its state
final List<Task> tasks = [
Task('Do homework'),
Task('Laundry'),
Task('Finish this tutorial')
];
// Now state is responsible for building the widget
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TODO app',
initialRoute: '/',
routes: {
'/': (context) => TODOList(tasks: tasks),
'/create': (context) => TODOCreate(),
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment