为什么AnimatedSwitcher()嵌套在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是一个自定义小部件,它在AnimatedBuilder 中返回CustomPaint(child:child(或CustomPaint

可能是因为AnimatedBckground(animate:bool(根据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动画都在不影响彼此的情况下运行。

最新更新