【问题标题】:Heigh Change Tween Animation Issue in FlutterFlutter 中的高度变化补间动画问题
【发布时间】:2019-04-01 08:41:20
【问题描述】:

我想为容器的高度设置动画

我想做的是:

class _AddVehicleState extends State<AddVehicle>
    with SingleTickerProviderStateMixin {

   AnimationController _otherFieldsAnimationController;
   Animation<double> _heightAnimation;

  override
  void initState() {
    _otherFieldsAnimationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 3000),
    );


    _heightAnimation = new Tween<double>(begin: 400.0, end: 20.0)
        .animate(_otherFieldsAnimationController);

    _otherFieldsAnimationController.forward();

}

 @override
  Widget build(BuildContext context) {
    return new WillPopScope(
        onWillPop: _onWillPop,
        child: new Scaffold(body: Builder(builder: (scaffoldContext1) {
          scaffoldContext = scaffoldContext1;
          return Stack(
            children: <Widget>[
              new Container(
                decoration: getGradientBackground(),
                child: ListView(
                  children: <Widget>[
                    Container(
                       color: Colors.white70,
                       height: _heightAnimation.value,
                       child: Center(
                       child: Text('HEY'),
                    ));
        .
        .
        .
        .
        .

如您所见,容器包含 Animation 的高度。

此页面上的所有其他动画都可以正常运行,但此容器没有动画。

有什么特别的原因吗?还是我错过了什么?

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:

    你可以像这样使用动画容器

        class MyApp extends StatefulWidget {
          @override
          _MyAppState createState() => _MyAppState();
        }
    
        class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
          AnimationController _controller;
          Animation<double> _animation;
          var _duration = const Duration(milliseconds: 3000);
    
          @override
          void initState() {
            super.initState();
            _controller = AnimationController(
              vsync: this,
              duration: _duration,
            )..addListener(() {
                setState(() {});
              });
            _animation =
                new Tween<double>(begin: 20.0, end: 400.0).animate(_controller);
    _controller.forward();
          }
    
          @override
          Widget build(BuildContext context) {
            return Scaffold(
                appBar: AppBar(
                  title: Text('Stackoverflow'),
                ),
                body: Center(
                  child: AnimatedContainer(
                    duration: _duration,
                    height: _animation.value,
                    width: 200,
                    color: Colors.amber,
                  ),
                ));
          }
        }
    

    【讨论】:

    • 还是运气好。根本没有动画
    • 这是一个经过测试的代码,尝试单独运行它,然后弄清楚如何适应您的代码
    • 即使我知道我的代码是正确的,但我想知道是什么停止了动画
    • 然后贴出完整的代码,如果根本不工作,你怎么知道你的代码是正确的
    • 我已经发布了需要的东西。除了我发布的内容之外,它什么都没有
    猜你喜欢
    • 2019-04-21
    • 2019-05-15
    • 2022-01-19
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多