simple widget tree with managing data with each
This commit is contained in:
+4
-32
@@ -1,18 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import './product_manager.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
class MyApp extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
// TODO: implement createState
|
||||
return _MyAppState();
|
||||
}
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
List<String> _products = ['Test'];
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
@@ -22,27 +14,7 @@ class _MyAppState extends State<MyApp> {
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Container(
|
||||
margin: EdgeInsets.all(10),
|
||||
child: RaisedButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_products.add('Advanced Food Tester');
|
||||
});
|
||||
},
|
||||
child: Text('Click Me!'),
|
||||
)),
|
||||
Column(
|
||||
children: _products
|
||||
.map((elem) => Card(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Image.asset('assets/food.jpg'),
|
||||
Text(elem)
|
||||
],
|
||||
)))
|
||||
.toList(),
|
||||
),
|
||||
ProductManager('Test'),
|
||||
],
|
||||
),
|
||||
));
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import './products.dart';
|
||||
|
||||
class ProductManager extends StatefulWidget {
|
||||
final String startingProduct;
|
||||
|
||||
ProductManager(this.startingProduct);
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
return _ProductManagerState();
|
||||
}
|
||||
}
|
||||
|
||||
class _ProductManagerState extends State<ProductManager> {
|
||||
List<String> _products = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_products.add(widget.startingProduct);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
Container(
|
||||
margin: EdgeInsets.all(10),
|
||||
child: RaisedButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_products.add('Advanced Food Tester');
|
||||
});
|
||||
},
|
||||
child: Text('Click Me!'),
|
||||
),
|
||||
),
|
||||
Products(_products),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class Products extends StatelessWidget {
|
||||
final List<String> products;
|
||||
|
||||
Products(this.products);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: products
|
||||
.map((elem) => Card(
|
||||
child: Column(
|
||||
children: <Widget>[Image.asset('assets/food.jpg'), Text(elem)],
|
||||
)))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user