Created
January 17, 2020 06:00
-
-
Save libindev/bdbc5e4ea2e8e9a67d5be176efbe7b0b to your computer and use it in GitHub Desktop.
InhertiedWigets
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:collection/collection.dart'; | |
import 'package:flutter/material.dart'; | |
class ShoppingCartInfo { | |
final List<int> productIds; | |
ShoppingCartInfo({this.productIds}); | |
} | |
class ShoppingCart extends InheritedWidget { | |
const ShoppingCart({Key key, @required this.info, @required this.child}); | |
final ShoppingCartInfo info; | |
final Widget child; | |
@override | |
bool updateShouldNotify(ShoppingCart old) => !IterableEquality().equals(info.productIds, old.info.productIds); | |
static ShoppingCartInfo of(BuildContext context) { | |
return (context.inheritFromWidgetOfExactType(ShoppingCart) as ShoppingCart).info; | |
} | |
} |
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_inherted_widgets/inherited_widgets.dart'; | |
import 'package:flutter_inherted_widgets/product_page.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return ShoppingCart(info: ShoppingCartInfo(productIds: <int>[1, 2]),child: MaterialApp( | |
home: ProductFeed(), | |
)); | |
} | |
} | |
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'inherited_widgets.dart'; | |
class ProductDetail extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return ProductDetailState(); | |
} | |
} | |
class ProductDetailState extends State<ProductDetail> { | |
@override | |
Widget build(BuildContext context) { | |
final info = ShoppingCart.of(context); | |
return Scaffold( | |
appBar: AppBar(title: Text('Retail Store')), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('Product A'), | |
Text('${info.productIds.length} selected'), | |
]), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add ), | |
onPressed: (){ | |
setState(() { | |
info.productIds.add(10); | |
}); | |
}, | |
), | |
); | |
} | |
} |
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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_inherted_widgets/product_detail.dart'; | |
import 'inherited_widgets.dart'; | |
class ProductFeed extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return ProductFeedState(); | |
} | |
} | |
class ProductFeedState extends State<ProductFeed> { | |
@override | |
Widget build(BuildContext context) { | |
final info = ShoppingCart.of(context); | |
return Scaffold( | |
appBar: AppBar(title: Text('Product Feed')), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('${info.productIds.length} selected'), | |
RaisedButton( | |
child: Text('Product A'), | |
onPressed: () { | |
Navigator.push( context, MaterialPageRoute(builder: (context) => ProductDetail())); | |
}, | |
), | |
]), | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add ), | |
onPressed: (){ | |
setState(() { | |
info.productIds.add(10); | |
}); | |
}, | |
), | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment