Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Created March 19, 2017 23:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save branflake2267/60f24f6532a3ea54d4680c9967519201 to your computer and use it in GitHub Desktop.
Save branflake2267/60f24f6532a3ea54d4680c9967519201 to your computer and use it in GitHub Desktop.
Flutter - Creating a StatefulWidget
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class SandyShores extends StatelessWidget {
static String routeName = "sandyShores";
@override
Widget build(BuildContext context) {
return new Center(child: new Text("You've landed on the Sandy Shores!"));
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
var routes = <String, WidgetBuilder> {
SandyShores.routeName : (BuildContext context) => new SandyShores(),
};
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting
// the app, try changing the primarySwatch below to Colors.green
// and press "r" in the console where you ran "flutter run".
// We call this a "hot reload". Notice that the counter didn't
// reset back to zero -- the application is not restarted.
primarySwatch: Colors.blue,
),
//home: new MyHomePage(title: 'Flutter Demo Home Page'),
home: new ColorPage(),
routes: routes,
);
}
}
class ColorPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new ColorPageState();
}
class ColorPageState extends State<ColorPage> {
var _color = Colors.blue[400];
@override
Widget build(BuildContext context) {
var item2 = new Container(width: 50.0, height: 50.0, color: _color);
var buttonText = new Text('Click');
var item1 = new RaisedButton(child: buttonText, onPressed: _onButonPressed);
var items = [item1, item2];
var row = new Row(children: items);
var center = new Center(child: row);
Scaffold scaffold = new Scaffold(body: center);
return scaffold;
}
void _onButonPressed() {
setState(() {
if (_color == Colors.blue[400]) {
_color = Colors.red[400];
} else {
_color = Colors.blue[400];
}
});
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful,
// meaning that it has a State object (defined below) that contains
// fields that affect how it looks.
// This class is the configuration for the state. It holds the
// values (in this case the title) provided by the parent (in this
// case the App widget) and used by the build method of the State.
// Fields in a Widget subclass are always marked "final".
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
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 we changed _counter without calling
// setState(), then the build method would not be called again,
// and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
var headerText = new Text('Headers');
var header = new DrawerHeader(child: headerText);
var item1Text = new Text('Sandy Shores');
var item1Icon = new Icon(Icons.settings);
var item1 = new DrawerItem(child: item1Text, icon: item1Icon, onPressed: _onItem1OnPressed);
var children = [header, item1];
var listView = new ListView(children: children);
var drawer = new Drawer(child: listView);
// This method is rerun every time setState is called, for instance
// as done by the _incrementCounter 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 change
// instances of widgets.
return new Scaffold(
drawer: drawer,
appBar: new AppBar(
// Here we take the value from the MyHomePage object that
// was created by the App.build method, and use it to set
// our appbar title.
title: new Text(config.title),
),
body: new Center(
child: new Text(
'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma tells the Dart formatter to use
// a style that looks nicer for build methods.
);
}
void _onItem1OnPressed() {
Navigator.popAndPushNamed(context, SandyShores.routeName);
}
}
@branflake2267
Copy link
Author

https://www.youtube.com/watch?v=bqMnyIFP0dY - Video on Creating a StatefulWidget in Flutter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment