forked from firka/flutter
Include widget configuration in the template (#6476)
This should help new users figure out how to set up their constructors and use config data from the build method of a State.
This commit is contained in:
@@ -14,7 +14,6 @@ void main() {
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
// This widget is the root of your application.
|
||||
// It is stateless, meaning that it always looks the same.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new MaterialApp(
|
||||
@@ -30,18 +29,25 @@ class MyApp extends StatelessWidget {
|
||||
// reset back to zero -- the application is not restarted.
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: new MyHomePage(),
|
||||
home: new MyHomePage(title: 'Flutter Demo'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatefulWidget {
|
||||
MyHomePage({Key key}) : super(key: key);
|
||||
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();
|
||||
}
|
||||
@@ -65,7 +71,10 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
Widget build(BuildContext context) {
|
||||
return new Scaffold(
|
||||
appBar: new AppBar(
|
||||
title: new Text('Flutter Demo'),
|
||||
// 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(
|
||||
|
||||
Reference in New Issue
Block a user