Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. class MyScopedModelState extends State<MyScopedModel> {
  2. MainModel mainModel;
  3.  
  4. void initState() {
  5. super.initState();
  6. mainModel = MainModel();
  7. }
  8.  
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: "State",
  13. home: Scaffold(
  14. appBar: AppBar(title: Text("Scoped Model")),
  15. body: ScopedModel<MainModel>(
  16. model: mainModel,
  17. child: MyHomePage(),
  18. ),
  19. ),
  20. );
  21. }
  22. }
  23.  
  24. class MyHomePage extends StatefulWidget {
  25. @override
  26. MyHomePageState createState() {
  27. return new MyHomePageState();
  28. }
  29. }
  30.  
  31. class MyHomePageState extends State<MyHomePage> {
  32. @override
  33. void initState() {
  34. super.initState();
  35. ScopedModel.of<MainModel>(context).getItems();
  36. }
  37.  
  38. @override
  39. Widget build(BuildContext context) {
  40. return Column(
  41. children: <Widget>[
  42. Container(
  43. padding: EdgeInsets.only(top: 10),
  44. height: 150,
  45. child: ScopedModelDescendant<MainModel>(
  46. builder: (context, _, mainModel) => mainModel.items == null
  47. ? Center(
  48. child: CircularProgressIndicator(),
  49. )
  50. : ListView.builder(
  51. scrollDirection: Axis.horizontal,
  52. itemCount: mainModel.items.length,
  53. itemBuilder: (context, index) {
  54. Color randColor = RandomColor()
  55. .randomColor(colorBrightness: ColorBrightness.light);
  56. return SizedBox(
  57. width: 100,
  58. child: ItemCard(
  59. item: mainModel.items[index],
  60. color: randColor,
  61. onTap: () => ScopedModel.of<MainModel>(context)
  62. .showDetailed(randColor, index),
  63. ),
  64. );
  65. },
  66. ),
  67. ),
  68. ),
  69. Divider(),
  70. Center(
  71. child: ScopedModelDescendant<MainModel>(
  72. builder: (context, _, mainModel) => mainModel.detailedIndex == null
  73. ? Container()
  74. : SizedBox(
  75. width: 200,
  76. height: 200,
  77. child: ItemCard(
  78. item: mainModel.items[mainModel.detailedIndex],
  79. color: mainModel.detailedColor,
  80. onTap: () => mainModel.increment(),
  81. ),
  82. ),
  83. ),
  84. ),
  85. ],
  86. );
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement