Last active
March 17, 2019 11:01
-
-
Save jindalAnuj/e9fcd414a82decbdd24a96f7ddab4762 to your computer and use it in GitHub Desktop.
Concept of List StateUp in Dart Similar to adding Listiner in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import './product_manager.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
brightness: Brightness.light, | |
primarySwatch: Colors.deepOrange, | |
accentColor: Colors.deepPurple), | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('EasyList'), | |
), | |
body: ProductManager(startingProduct: 'Food Tester'), | |
), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class ProductControl extends StatelessWidget { | |
final Function addProduct; | |
ProductControl(this.addProduct); | |
@override | |
Widget build(BuildContext context) { | |
return RaisedButton( | |
color: Theme.of(context).primaryColor, | |
onPressed: () { | |
//Here in this class we called addProducts without any need of setState() | |
addProduct('Sweets'); | |
}, | |
child: Text('Add Product'), | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import './products.dart'; | |
import './product_control.dart'; | |
class ProductManager extends StatefulWidget { | |
final String startingProduct; | |
ProductManager({this.startingProduct = 'Sweets Tester'}) { | |
print('[ProductManager Widget] Constructor'); | |
} | |
@override | |
State<StatefulWidget> createState() { | |
print('[ProductManager Widget] createState()'); | |
return _ProductManagerState(); | |
} | |
} | |
class _ProductManagerState extends State<ProductManager> { | |
List<String> _products = []; | |
@override | |
void initState() { | |
print('[ProductManager State] initState()'); | |
_products.add(widget.startingProduct); | |
super.initState(); | |
} | |
@override | |
void didUpdateWidget(ProductManager oldWidget) { | |
print('[ProductManager State] didUpdateWidget()'); | |
super.didUpdateWidget(oldWidget); | |
} | |
void _addProduct(String product) { | |
//this method will be called on build method and the moment the constructer of Product class get called we add item on addition | |
setState(() { | |
_products.add(product); | |
}); | |
print(_products); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print('[ProductManager State] build()'); | |
return Column( | |
children: [ | |
Container( | |
margin: EdgeInsets.all(10.0), | |
child: ProductControl(_addProduct), | |
), | |
Products(_products) | |
], | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class Products extends StatelessWidget { | |
final List<String> products; | |
Products([this.products = const []]) { | |
print('[Products Widget] Constructor'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
print('[Products Widget] build()'); | |
return Column( | |
children: products | |
.map( | |
(element) => Card( | |
child: Column( | |
children: <Widget>[ | |
Image.asset('assets/food.jpg'), | |
Text(element) | |
], | |
), | |
), | |
) | |
.toList(), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment