flutter 数据传递与通信
flutter 数据传递与通信
1.页面传值(页面间跳转)
class GoodDetailPage extends StatefulWidget {
final String goodId;
GoodDetailPage({
Key key,
this.goodId,
}) : super(key: key);
@override
CourseDetailPageState createState() => CourseDetailPageState(goodId: goodId);
}
返回时,Future来获取新路由返回数据
路由传值:
Future pushNamed(BuildContext context, String routeName,{Object arguments})
var args=ModalRoute.of(context).settings.arguments
2.监听回调 (flutter dialog 与Widget 通信)
class PhotoPickerDialog extends Dialog {
final OnDialogClickListener onDialogClickListener;
PhotoPickerDialog(this.onDialogClickListener, {Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
//TODO: implement build
}
}
abstract class OnDialogClickListener {
void onPickImage();
void onTakePhoto();
void onCancel();
}
3. event_bus: ^1.0.1(登录后,界面状态的变化)
eventBus.fire(userInfo);
eventBus.on().listen((event) {
getUserInfo();
});
4.StreamController (接口数据成功,控制Widget的绘制)
httpUtil.getOpenClassDetail(openClassId).then((result) {
_streamController.add(result);
});
Widget _courseDetailWidget(BuildContext context) {
return new StreamBuilder(
stream: _streamController.stream,
builder:
(BuildContext context, AsyncSnapshot snapshot) {
if (snapshot != null && snapshot.data != null) {
entity = snapshot.data;
return Container();
} })
}
5.数据共享(InheritedWidget)
6.跨组件状态共享(Provider)