例如,我有以下伪代码:
if(state1.status==ok&&state2.status==ok) {do something}
MultiBlocListener
在这里不适用,因为我只能分别监听状态,我尝试嵌套BlocListener
,但它并没有帮助我,因为外部BlocListener
不会通知内部状态更改。所以在内部,只有在内部状态改变的那一刻,我才能检查外部的状态。
目前,我通过使用嵌套的BlocBuilder
和WidgetsBinding.instance.addPostFrameCallback
解决了这个问题。在这种情况下,外部BlocBuilder
会重建内部class SplashScreen extends StatefulWidget {
static const routeName = 'splash';
static const logoUrl = 'assets/images/logo.svg';
const SplashScreen({Key key}) : super(key: key);
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
Widget build(BuildContext context) {
void _navigateTo(String routeName) => Navigator.of(context).pushReplacementNamed(routeName);
return Scaffold(
backgroundColor: Theme.of(context).backgroundColor,
body: BlocBuilder<SettingsBloc, SettingsState>(
builder: (context, settingsState) {
return BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, authenticationState) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (authenticationState.status == AuthenticationStatus.UNAUTHENTICATED) {
Timer(Duration(seconds: 1), () => _navigateTo(LoginScreen.routeName));
} else if (authenticationState.status == AuthenticationStatus.AUTHENTICATED) {
switch (settingsState.currentType) {
case FastAccessType.UNKNOWN:
break;
case FastAccessType.NONE:
Timer(Duration(seconds: 1), () => _navigateTo(HomeScreen.routeName));
break;
case FastAccessType.PIN:
Timer(Duration(seconds: 1), () => _navigateTo(PinCodeScreen.routeNameLogin));
break;
case FastAccessType.BIOMETRICS:
Timer(Duration(seconds: 1), () => _navigateTo(BiometricsScreen.routeName));
break;
}
}
});
return Container(
decoration: BoxDecoration(gradient: mainGradient),
child: Center(
child: WebsafeSvg.asset(SplashScreen.logoUrl),
),
);
},
);
},
),
);
}
}
,我可以检查多个状态更改是否正确。但我不确定通常是否可以通过这种方式解决此问题。
注意:状态1来自bloc1,状态2来自bloc2,换句话说,来自多个集团的多个状态
这是我现在使用的代码:
{{1}}
当我在设备导航测试中看到两次调用该动画时,它看起来并不干净。
答案 0 :(得分:0)
尝试条件
BlocBuilder<BlocT, YourState>(
bloc: bloc,
condition: (c, s) => (s is ValidationState ),