her*_*ell 4 flutter flutter-animation bloc
因此,我遵循了bloc登录教程,尽管我设法完成了该教程,但对于Flutter&Dart还是很陌生。
代码的一部分,根据状态的不同,代码将返回不同的小部件,而不是新的Scaffold。由于未使用路由,因此页面之间的过渡看起来比较混乱且笨拙。
return BlocProvider<AuthenticationBloc>(
bloc: authenticationBloc,
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: BlocBuilder<AuthenticationEvent, AuthenticationState>(
bloc: authenticationBloc,
builder: (BuildContext context, AuthenticationState state) {
if (state is AuthenticationUninitialized) {
return SplashPage();
}
if (state is AuthenticationAuthenticated) {
return HomePage();
}
if (state is AuthenticationUnauthenticated) {
return LoginPage(userRepository: userRepository);
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
},
),
),
);
Run Code Online (Sandbox Code Playgroud)
我尝试添加添加返回的Navigation.push,如下所示:
if (state is AuthenticationUninitialized) {
Navigation.push(
return SplashPage();
),
}
Run Code Online (Sandbox Code Playgroud)
但是,虽然从语法上看并没有错,但它会使应用程序崩溃。有谁知道在维护BLoC示例的同时实现此目的的方法?谢谢。
您可以使用AnimatedSwitcher包装页面:
return BlocProvider<AuthenticationBloc>(
bloc: authenticationBloc,
child: MaterialApp(
home: BlocBuilder<AuthenticationEvent, AuthenticationState>(
bloc: authenticationBloc,
builder: (BuildContext context, AuthState state) {
return AnimatedSwitcher(
duration: Duration(milliseconds: 250),
child: _buildPage(context, state),
);
},
),
),
);
Run Code Online (Sandbox Code Playgroud)
默认情况下,它使用淡入淡出过渡,并以相反的顺序为旧的和新的小部件设置动画。
要在动画过程中将旧窗口小部件保留在适当的位置,请传递给AnimatedSwitcher
switchOutCurve: Threshold(0),
Run Code Online (Sandbox Code Playgroud)
要模仿Navigator.pushAndroid中的过渡,请将其传递给它
transitionBuilder: (Widget child, Animation<double> animation) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(0, 0.25),
end: Offset.zero,
).animate(animation),
child: child,
);
},
Run Code Online (Sandbox Code Playgroud)
要使用系统转换,请尝试类似
transitionBuilder: (Widget child, Animation<double> animation) {
final theme = Theme.of(context).pageTransitionsTheme;
final prev = MaterialPageRoute(builder: (_) => widget);
return theme.buildTransitions(prev, context, animation, null, child);
},
Run Code Online (Sandbox Code Playgroud)
(最后一个测试得不好)
| 归档时间: |
|
| 查看次数: |
747 次 |
| 最近记录: |