From 0d9ee1c1b6e38d7f3d707791be89986c817895ff Mon Sep 17 00:00:00 2001 From: talksik Date: Sun, 26 May 2019 00:52:01 -0700 Subject: [PATCH] simple widget tree with managing data with each --- lib/main.dart | 36 ++++---------------------------- lib/product_manager.dart | 44 ++++++++++++++++++++++++++++++++++++++++ lib/products.dart | 19 +++++++++++++++++ 3 files changed, 67 insertions(+), 32 deletions(-) create mode 100644 lib/product_manager.dart create mode 100644 lib/products.dart diff --git a/lib/main.dart b/lib/main.dart index 2e29e08..ea20a1a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,18 +1,10 @@ import 'package:flutter/material.dart'; +import './product_manager.dart'; + void main() => runApp(MyApp()); -class MyApp extends StatefulWidget { - @override - State createState() { - // TODO: implement createState - return _MyAppState(); - } -} - -class _MyAppState extends State { - List _products = ['Test']; - +class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( @@ -22,27 +14,7 @@ class _MyAppState extends State { ), 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: [ - Image.asset('assets/food.jpg'), - Text(elem) - ], - ))) - .toList(), - ), + ProductManager('Test'), ], ), )); diff --git a/lib/product_manager.dart b/lib/product_manager.dart new file mode 100644 index 0000000..1c64170 --- /dev/null +++ b/lib/product_manager.dart @@ -0,0 +1,44 @@ +import 'package:flutter/material.dart'; + +import './products.dart'; + +class ProductManager extends StatefulWidget { + final String startingProduct; + + ProductManager(this.startingProduct); + + @override + State createState() { + return _ProductManagerState(); + } +} + +class _ProductManagerState extends State { + List _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), + ], + ); + } +} diff --git a/lib/products.dart b/lib/products.dart new file mode 100644 index 0000000..43a1b0a --- /dev/null +++ b/lib/products.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class Products extends StatelessWidget { + final List products; + + Products(this.products); + + @override + Widget build(BuildContext context) { + return Column( + children: products + .map((elem) => Card( + child: Column( + children: [Image.asset('assets/food.jpg'), Text(elem)], + ))) + .toList(), + ); + } +}