父组件向子组件传值时,在父组件中用setState更新值,子组件会到声明周期的didUpdateWidget部分,改生命周期会刷新build
具体代码:
父组件setState更新值:
FlatButton(
textColor: Colors.white,
onPressed: () {
showBottomSheetTool
.showStringPicker(context, data: _yearList,
clickCallBack: (int index, var str) {
setState(() {
_year = _yearList[index];
print('修改后的年份信息:${_year}');
// 筛选接口
});
});
},
child: Row(
children: [
Text('${_year}'),
SizedBox(width: 4),
Icon(Icons.chevron_right),
],
),
),
父组件传值:
LineTable(year:_year,position:_position,schoolname:widget.arguments['title']),
子组件接受:
class PlanTable extends StatefulWidget {
String schoolname;
String position;
int year;
PlanTable({this.position,this.year,this.schoolname});
@override
_PlanTableState createState() => _PlanTableState();
}
调用生命周期:
@override
void didUpdateWidget(PlanTable oldWidget) {
super.didUpdateWidget(oldWidget);
_dioProgress = getDate();
print("didUpdateWidget");
print("position:${oldWidget.position}:${widget.position}");
print("year:${oldWidget.year}:${widget.year}");
}