flutter - 如何使用 Getx 构建动画启动画面?

标签 flutter dart splash-screen flutter-animation flutter-getx

我对此感到困惑。我不知道如何更新该值以在启动屏幕中为图像设置动画。对于有状态小部件类,我们在监听器内调用 setState(() {}); 来更新值。但是,我如何使用 Getx 实现它?

带有状态小部件的动画:

animationInitilization() {
    animationController =
        AnimationController(vsync: this, duration: const Duration(seconds: 2));
    animation =
        CurvedAnimation(parent: animationController, curve: Curves.easeOut);
    animation!.addListener(() {
      setState(() {});
    });
    animationController.forward();
  }

使用 Getx 制作动画:

启动画面:

class SplashScreen extends StatelessWidget {
  const SplashScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SplashScreenViewModel splashScreenViewModel =
        Get.put(SplashScreenViewModel());
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Image.asset(
                'assets/images/logo.png',
                width: splashScreenViewModel.animation.value * 200,
                height: splashScreenViewModel.animation.value * 200,
              ),
            ],
          ),
        ],
      ),
    );
  }
}

Getx Controller :

class SplashScreenViewModel extends GetxController
    with GetSingleTickerProviderStateMixin {
  late AnimationController animationController;
  late Animation<double> animation;

  @override
  void onInit() {
    animationInitilization();
    super.onInit();
  }

  animationInitilization() {
    animationController =
        AnimationController(vsync: this, duration: const Duration(seconds: 2));
    animation =
        CurvedAnimation(parent: animationController, curve: Curves.easeOut)
            .obs
            .value;
    animation.addListener(() => update());
    animationController.forward();
  }
}

最佳答案

SplashScreen 兄弟的动画很棒。

我建议使用 GetBuilder() 使其正常工作:

class SplashScreen extends StatelessWidget {
  const SplashScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: GetBuilder<SplashScreenViewModel>(
        init: SplashScreenViewModel(),
        builder: (controller) {
          return Stack(
            fit: StackFit.expand,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset(
                    'assets/images/logo.png',
                    width: controller.animation.value * 200,
                    height: controller.animation.value * 200,
                  ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}

关于flutter - 如何使用 Getx 构建动画启动画面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73097511/

相关文章:

json - 无法从 flutter 中的 jsonDecoded 变量中提取 key

flutter - 如何使用 Flare 动画作为带有淡入动画的图像占位符

Dart VM - 外国浏览器(非谷歌)

ios - Launch/Spash 图像不会随着升级而改变

android - flutter 错误 : Could not resolve com. google.android.gms :play-services-location:16. +,状态码 502

flutter - 如何通过 Flutter web 直接发送电子邮件

Flutter 提升按钮 - 如何使用 MaterialStateProperty.all 移除提升?

flutter - 如何在 Flutter 中从内部和外部存储中获取所有 mp3 文件?

iOS PWA 闪屏?

java - 应用 SplashScreen 而不创建 Thread 和 run()