【发布时间】:2020-01-01 01:30:10
【问题描述】:
我使用了 imagePicker。添加后我需要分配个人资料照片。上次我使用 setState() 但没有工作。
我的 bloc 类是这样的,
class ImageBloc {
final _loadingController = StreamController<String>();
Stream<String> get loadingStream => _loadingController.stream;
void setIsLoading(String image) => _loadingController.add(image);
dispose() {
_loadingController.close();
}
}
播放器类 这个类是StatefullWidget
class _PlayersState extends State<Players> {
ImageBloc bloc;
...
drawer: drawer(context, onProfilePictureChanged,bloc),
}
抽屉这是一个普通的抽屉。我在其他课程中使用了这种方法。其他类,我将 null 传递给 bloc 参数
Widget drawer(BuildContext context, onProfilePictureChanged,ImageBloc bloc) {
return Drawer(
child: Container(child:
//I checked bloc null or not because others class I passed null value to bloc
bloc == null ? Text("null value",
style: TextStyle(
color: Colors.white,
fontSize: 13.0,
fontWeight: FontWeight.w500,
)):
//Player class only I passed bloc
StreamBuilder<String>(
stream: bloc.loadingStream,
initialData: "bloc value",
builder: (context,snapshot){
return Text(snapshot.data,
style: TextStyle(
color: Colors.white,
fontSize: 13.0,
fontWeight: FontWeight.w500,
));
}),))
我通过 bloc 设置值来下沉..
await showDialog(
context: context,
builder: (BuildContext context) {
return ProfilePop(bloc:bloc);
});
ProfilePop 类
class ProfilePop extends StatefulWidget {
const ProfilePop({Key key, @required this.bloc}) : super(key: key);
final ImageBloc bloc;
}
class ProfilePopState extends State<ProfilePop>{
upload(File imageFile) async {
...
setState(() {
global.profileImage = uploadedImage["Photo"];
widget.bloc.setIsLoading(uploadedImage["Photo"]);
});
...
@override
Widget build(BuildContext context) {
void onCancelPressed() {
Navigator.pop(context);
}
return new AlertDialog(
title:global.image == null
? profile
: new Container(
...
),
),
),);}
});}}
【问题讨论】: