为什么 AnimatedSwitcher() 嵌套在 AnimatedBuilder() 中时不起作用?
Why does AnimatedSwitcher() not work when nested in an AnimatedBuilder()?
当我 运行 下面的代码时,在用户登录时 AnimatedBackground() 动画成功,页面从 LoginScreen() 变为 HomeScreen(),但是过渡中没有动画。
我怀疑是嵌套动画和不合时宜的重建有关,但还没有按键修复。
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final User? firebaseuser = context.watch<User?>();
print("build run with user $firebaseuser");
return SafeArea(
child: AnimatedBackground(
key: Key("salt123value${firebaseuser == null}"),
animate: firebaseuser == null ? false : true,
child: AnimatedSwitcher(
key: Key("animatedswitcher"),
transitionBuilder: AnimatedSwitcher.defaultTransitionBuilder,
duration: const Duration(seconds: 4),
child: firebaseuser == null
? LoginScreen(
key: Key("login"),
)
: HomeScreen(
key: Key("home"),
)),
));
}
}
N.B AnimatedBackground 是一个自定义小部件,returns AnimatedBuilder
中的 CustomPaint(child:child) 或 CustomPaint
难道是因为 AnimatedBckground (animate: bool) returns 不同的部件树深度取决于 animate 的值?
所以最后,将 AnimatedSwticher.defaultLayoutBuilder
包裹在我的动画小部件中成功了
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final User? firebaseuser = context.watch<User?>();
return SafeArea(
child: AnimatedSwitcher(
layoutBuilder: (currentChild, previousChildren) =>
AnimatedBackground(
key: ValueKey<String>(firebaseuser?.uid.toString() ?? "none"),
animate: firebaseuser != null,
child: AnimatedSwitcher.defaultLayoutBuilder(
currentChild, previousChildren),
),
duration: const Duration(seconds: 1),
child: firebaseuser == null ? LoginScreen() : HomeScreen()));
}
}
因此我的动画和 AnimatedSwitcher 动画 运行 不会相互影响。
当我 运行 下面的代码时,在用户登录时 AnimatedBackground() 动画成功,页面从 LoginScreen() 变为 HomeScreen(),但是过渡中没有动画。
我怀疑是嵌套动画和不合时宜的重建有关,但还没有按键修复。
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final User? firebaseuser = context.watch<User?>();
print("build run with user $firebaseuser");
return SafeArea(
child: AnimatedBackground(
key: Key("salt123value${firebaseuser == null}"),
animate: firebaseuser == null ? false : true,
child: AnimatedSwitcher(
key: Key("animatedswitcher"),
transitionBuilder: AnimatedSwitcher.defaultTransitionBuilder,
duration: const Duration(seconds: 4),
child: firebaseuser == null
? LoginScreen(
key: Key("login"),
)
: HomeScreen(
key: Key("home"),
)),
));
}
}
N.B AnimatedBackground 是一个自定义小部件,returns AnimatedBuilder
中的 CustomPaint(child:child) 或 CustomPaint难道是因为 AnimatedBckground (animate: bool) returns 不同的部件树深度取决于 animate 的值?
所以最后,将 AnimatedSwticher.defaultLayoutBuilder
包裹在我的动画小部件中成功了
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final User? firebaseuser = context.watch<User?>();
return SafeArea(
child: AnimatedSwitcher(
layoutBuilder: (currentChild, previousChildren) =>
AnimatedBackground(
key: ValueKey<String>(firebaseuser?.uid.toString() ?? "none"),
animate: firebaseuser != null,
child: AnimatedSwitcher.defaultLayoutBuilder(
currentChild, previousChildren),
),
duration: const Duration(seconds: 1),
child: firebaseuser == null ? LoginScreen() : HomeScreen()));
}
}
因此我的动画和 AnimatedSwitcher 动画 运行 不会相互影响。