【问题标题】:Flutter- Animation playing once for each rebuildFlutter-每次重建时播放一次动画
【发布时间】:2021-12-15 16:49:47
【问题描述】:

我正在尝试为我当前项目中的一项功能编写自定义组件。这是通过拖动增加容器的高度,当屏幕上的点击被释放时,它应该回到原来的状态。问题是每当我重新启动时,动画都会播放一次,如果我拖动组件以增加其大小,它会再次播放但不会返回,因此动画不会播放。代码如下;

class _HomeViewState extends State<HomeView> with TickerProviderStateMixin {
  HomeViewController homeController = HomeViewController();

  late Animation<double> translateBetween;
  late AnimationController customAnimationController;

  @override
  void initState() {
    customAnimationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 2000));
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    customAnimationController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Obx(() {
            return Container(
              alignment: Alignment.topCenter,
              child: Column(
                children: [
                  GestureDetector(
                    onVerticalDragUpdate: (dragUpdateDetails) {
                      var tween = Tween(
                          begin: homeController.animatedHeight, end: 20.0);
                      translateBetween = tween.animate(CurvedAnimation(
                        parent: customAnimationController,
                        curve: Curves.bounceOut,
                      ))
                        ..addListener(() {
                          homeController.animatedHeight =
                              translateBetween.value;
                        });

                      if (homeController.animatedHeight >= 20) {
                        homeController.animatedHeight = homeController
                                .animatedHeight -
                            dragUpdateDetails.primaryDelta!.roundToDouble() /
                                6.5;
                      }
                      if (homeController.animatedHeight < 20) {
                        homeController.animatedHeight = 20;
                      }
                      if (homeController.animatedHeight >= 95) {
                        homeController.animatedHeight = 95;
                      }
                    },
                    onVerticalDragEnd: (dragEndDetails) {
                      customAnimationController.forward();
                    },
                    child: Column(
                      children: [
                        const Icon(Icons.arrow_upward_rounded),
                        SizedBox(
                          width: 20.w,
                          child: Divider(
                            color: Colors.black,
                            thickness: 0.3.w,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              width: 100.w,
              height: homeController.animatedHeight.h,
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(5.w),
                      topRight: Radius.circular(5.w))),
            );
          })
        ],
      ),
    );
  }
}

我使用getx 控制器来动态改变容器的大小。

class HomeViewController extends GetxController {
  final RxDouble _animatedHeight = 20.0.obs;
  double get animatedHeight => _animatedHeight.value;
  set animatedHeight(double value) => _animatedHeight.value = value;
}

问题的 GIF

【问题讨论】:

    标签: flutter flutter-layout flutter-animation flutter-getx


    【解决方案1】:

    您需要在动画完成后将其重置为开头。

    试试这个,

     onVerticalDragEnd: (dragEndDetails) {
       customAnimationController.reset();
       customAnimationController.forward();
     },
    

    【讨论】:

    • 您提供的解决方案解决了我的问题。非常感谢!
    • @MehmetKaranlık 欢迎您。
    猜你喜欢
    • 1970-01-01
    • 2018-04-09
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多