Flutter in InheritedWidget related

2019-09-11 14:21:09

  1 import 'package:flutter/material.dart';
  2 
  3 /// Inherited动画相关
  4 
  5 class Inherited extends StatefulWidget {
  6   @override
  7   State<StatefulWidget> createState() {
  8     return new _InheritedState();
  9   }
 10 }
 11 
 12 class _InheritedState extends State<Inherited> {
 13   _InheritedState() {}
 14 
 15   @override
 16   void initState() {
 17     _counterData = new CounterData(101);
 18     super.initState();
 19   }
 20 
 21   CounterData _counterData;
 22   String _content = '默认值';
 23   int index = 1;
 24 
 25   _increase() {
 26     print('+++');
 27     setState(() {
 28       _counterData = new CounterData(_counterData.count + 1);
 29     });
 30   }
 31 
 32   _reduce() {
 33     print('---');
 34     setState(() {
 35       _counterData = new CounterData(_counterData.count - 1);
 36     });
 37   }
 38 
 39   @override
 40   Widget build(BuildContext context) {
 41     return new MyInheritedWidget(
 42         counterData: _counterData,
 43         reduce: _reduce,
 44         increase: _increase,
 45         content: _content,
 46         child: new Scaffold(
 47           appBar: new AppBar(
 48             title: new Text('Inherited动画'),
 49           ),
 50           body: _body(),
 51           floatingActionButton: new FloatingActionButton(
 52             onPressed: () {
 53               setState(() {
 54                 _content = '${_content} --- ${index}';
 55                 index++;
 56               });
 57             },
 58             child: new Icon(Icons.refresh),
 59           ),
 60         ));
 61   }
 62 
 63   Widget _body() {
 64     return new Column(
 65       mainAxisAlignment: MainAxisAlignment.center,
 66       children: <Widget>[
 67         new IncreaseBtn(),
 68         new CounterText(),
 69         new ReduceBtn(),
 70         new CharmView(),
 71       ],
 72     );
 73   }
 74 }
 75 
 76 class CounterData {
 77   final int count;
 78 
 79   const CounterData(this.count);
 80 }
 81 
 82 class MyInheritedWidget extends InheritedWidget {
 83   final CounterData counterData;
 84   final Function() reduce;
 85   final Function() increase;
 86   final Widget child;
 87   final String content;
 88 
 89   MyInheritedWidget({
 90     Key key,
 91     @required this.counterData,
 92     @required this.reduce,
 93     @required this.increase,
 94     @required this.content,
 95     @required this.child,
 96   }) : super(key: key, child: child);
 97 
 98   static MyInheritedWidget of(BuildContext context) {
 99     return context.inheritFromWidgetOfExactType(MyInheritedWidget);
100   }
101 
102   @override
103   bool updateShouldNotify(MyInheritedWidget oldWidget) {
104     bool needUpdate = (counterData != oldWidget.counterData) || (content != oldWidget.content);
105     print('F1 updateShouldNotify needUpdate = $needUpdate');
106     return needUpdate;
107   }
108 }
109 
110 class IncreaseBtn extends StatelessWidget {
111   @override
112   Widget build(BuildContext context) {
113     return new Padding(
114       padding: EdgeInsets.symmetric(horizontal: 20, vertical: 6),
115       child: new RaisedButton(
116         onPressed: () {
117           print('加加加 F = ${MyInheritedWidget.of(context).increase}');
118           MyInheritedWidget.of(context).increase();
119         },
120         child: new Text('+'),
121       ),
122     );
123   }
124 }
125 
126 class CounterText extends StatelessWidget {
127   @override
128   Widget build(BuildContext context) {
129     return new Padding(
130         padding: EdgeInsets.symmetric(horizontal: 20, vertical: 6),
131         child: new Text(
132             '当前数字是:${MyInheritedWidget.of(context).counterData.count}'));
133   }
134 }
135 
136 class ReduceBtn extends StatelessWidget {
137   @override
138   Widget build(BuildContext context) {
139     return new Padding(
140       padding: EdgeInsets.symmetric(horizontal: 20, vertical: 6),
141       child: new RaisedButton(
142         onPressed: () {
143           MyInheritedWidget.of(context).reduce();
144         },
145         child: new Text('-'),
146       ),
147     );
148   }
149 }
150 
151 class CharmView extends StatefulWidget {
152   @override
153   State<StatefulWidget> createState() {
154     return new _CharmViewState();
155   }
156 }
157 
158 class _CharmViewState extends State<CharmView> {
159   @override
160   void initState() {
161     print('F1 initState');
162     super.initState();
163   }
164 
165   @override
166   void didChangeDependencies() {
167     print('F1 didChangeDependencies');
168     super.didChangeDependencies();
169   }
170 
171   @override
172   void didUpdateWidget(CharmView oldWidget) {
173     print('F1 didUpdateWidget');
174     super.didUpdateWidget(oldWidget);
175   }
176 
177   @override
178   void deactivate() {
179     super.deactivate();
180   }
181 
182   @override
183   void dispose() {
184     super.dispose();
185   }
186 
187   @override
188   Widget build(BuildContext context) {
189     return new Container(
190       width: 200,
191       height: 100,
192       alignment: Alignment.center,
193       child: new Text(MyInheritedWidget.of(context).content),
194     );
195   }
196 }

 

Guess you like

Origin www.cnblogs.com/wlrhnh/p/11506033.html