simple widget tree with managing data with each

This commit is contained in:
talksik
2019-05-26 00:52:01 -07:00
parent c6de0ce9b2
commit 0d9ee1c1b6
3 changed files with 67 additions and 32 deletions
+4 -32
View File
@@ -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'),
],
),
));
+44
View File
@@ -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),
],
);
}
}
+19
View File
@@ -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(),
);
}
}