flutter AnimationController animateTo方法,始终返回到开始位置

pcww981p  于 2023-02-05  发布在  Flutter
关注(0)|答案(1)|浏览(365)

我使用AnimationController.animateTo方法将动画立即跳转到特定的点。
将值为0.0的参数“target”传递给animateTo方法时,AnimationController的结果“value”属性为0.0。如果将值为0.5的参数“target”传递给animateTo方法,AnimationController的结果“value”属性仍返回0.0。我希望"value“属性返回0.5。

void _onPlayButtonPressed() {
    final cpm = ScopedModel.of<CpModel>(context);
    if (cpm.started) {
      cpm.stop();
      _animController.stop();

      var targetProgress = cpm.adjustedProgress;
      _animController.animateTo(targetProgress , duration: Duration(seconds: 0));
    } else {
      _ranOnce = true;
      cpm.start();
      _animController.forward();
    }
  }
w8f9ii69

w8f9ii691#

我很晚才回答这个问题,但对其他开发人员会有帮助。
将动画控制器添加到动画对象上并使用该动画对象。
如下所示:下面的示例说明如何使用AnimationController的toAnimate方法:

class Example extends StatefulWidget {
  const Example({Key? key}) : super(key: key);

  @override[![demo][1]][1]
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> with TickerProviderStateMixin {

  late Animation<double> slideAnimation;
  late AnimationController sliderAnimationController;
  @override
  void initState() {
    sliderAnimationController = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 1000));
    slideAnimation = Tween<double>(begin:0, end: 130).animate(CurvedAnimation(
        parent: sliderAnimationController, curve: Curves.ease));
    print(    sliderAnimationController.value);

    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(body: SizedBox(width: 130,height: 160,child: IntrinsicHeight(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  InkWell(
                    onTap: () {
                      sliderAnimationController.animateTo(0.0);

                    },
                    child: Padding(
                      padding: const EdgeInsets.all(6.0),
                      child: Text(
                        "Text1",
                        style: Theme.of(context)
                            .textTheme
                            .bodyText1!
                            .copyWith(
                            color: Colors.black,
                            fontWeight: FontWeight.w600),
                      ),
                    ),
                  ),
                  12.verticalSpace,
                  InkWell(
                    onTap: () {

                      //  sliderAnimationController.forward();
                      sliderAnimationController.animateTo(0.215);
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(6.0),
                      child: Text(
                        "Text2",
                        style: Theme.of(context)
                            .textTheme
                            .subtitle1!
                            .copyWith(color: Colors.black),
                      ),
                    ),
                  ),
                  12.verticalSpace,
                  InkWell(
                    onTap: () {
                      sliderAnimationController.animateTo(0.405);

                    }
                    ,
                    child: Padding(
                      padding: const EdgeInsets.all(6.0),
                      child: Text(
                        "Text3",
                        style: Theme.of(context)
                            .textTheme
                            .subtitle1!
                            .copyWith(color: Colors.black),
                      ),
                    ),
                  ),
                  12.verticalSpace,
                  InkWell(
                    onTap: () {
                      sliderAnimationController.animateTo(1.0);

                    },
                    child: Padding(
                      padding: const EdgeInsets.all(6.0),
                      child: Text(
                        "Text4",
                        style: Theme.of(context)
                            .textTheme
                            .subtitle1!
                            .copyWith(color: Colors.black),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  AnimatedBuilder(

                    animation: sliderAnimationController,
                    builder: (BuildContext context, Widget? child) {
                      return Transform.translate(offset: Offset(0, slideAnimation.value),
                          child: child);
                    },
                    child: Container(
                      width: 3,
                      height: 26,
                      color: Colors.blue,
                    ),
                  ),
                  Container(
                    width: 1,
                    color: Colors.black54,
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    ),));
  }

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

相关问题