在我的实现Bloc
模式中,我可以像使用name
和email
一样从服务器中获取一些数据,我可以使用布局中的分隔小部件来显示此数据,例如使用{{ 1}},因为我在布局中有一些小部件,例如
Text
从服务器获取数据之前,我想从服务器获取数据后尝试更新此变量
例如:
Text('Hi $_username')
在点击class Login extends StatefulWidget {
final LoginListingBloc loginListingBloc;
Login({this.loginListingBloc});
State<Login> createState() {
return _Login(loginListingBloc: loginListingBloc);
}
}
class _Login extends State<Login> {
var _userPageName='';
_Login({this.loginListingBloc});
final LoginListingBloc loginListingBloc;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 50.0),
child: SingleChildScrollView(
child: Container(
...child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"welcome $_userPageName",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600),
),
...
],
)),
Row(
children: <Widget>[
...
IconButton(
icon: new Icon(Icons.account_circle),
onPressed: () {
loginListingBloc.dispatch(LoginEvent(loginInfoModel: testLogin));
},
iconSize: 40.0,
color: Colors.white,
),
],
),
),
],
),
LoginListing()
],
)),
),
)
],
),
);
}
}
class LoginListing extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder(
bloc: BlocProvider.of<LoginListingBloc>(context),
builder: (context, state) {
if (state is LoginUninitializedState) {
return Message(
message: 'login is not available',
);
} else if (state is LoginFetchingState) {
return Expanded(
child: CircularProgressIndicator(),
);
} else if (state is LoginErrorState) {
return Message(message: 'Error');
} else {
final stateAsLoginFetchedState = state as LoginFetchedState;
final loginInfo = stateAsLoginFetchedState.userInfo;
return buildShowLoginInfoItems(loginInfo);
}
});
}
Widget buildShowLoginInfoItems(UserInfo userInfo) {
return Container(
child: Text(userInfo.name),
);
}
}
之后如何更新$_userPageName
?
我的IconButton
实现的类:
LoginListingBloc