【问题标题】:Flutter reverse animation doesn't work after animation complete动画完成后颤振反向动画不起作用
【发布时间】:2019-07-05 07:04:35
【问题描述】:

下面的代码是我实现简单滑动到底部的示例代码,平移到底部的动画效果很好,但是当我再次点击关闭时,这不起作用

我还有另一个问题,在这部分代码中使用容器的大小进行翻译:

Tween<Offset>(begin: Offset.zero, end: Offset(0.0, 0.50))

例如:

Tween<Offset>(begin: Offset.zero, end: Offset(0.0, HEIGHT OF WIDGET ))

完整源代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Stack(
          children: <Widget>[
            TopSlidingLayer(
              context,
              height: 200.0,
              backgroundColor: Colors.indigo,
              child: Container(color: Colors.green),
            )
          ],
        ),
      ),
    );
  }
}

class TopSlidingLayer extends StatefulWidget {
  final BuildContext context;
  final double height;
  final Color backgroundColor;
  final int animationSpeed;
  final Widget child;

  TopSlidingLayer(this.context, {this.height = 100.0, this.backgroundColor, this.animationSpeed = 300, @required this.child});

  @override
  State<TopSlidingLayer> createState() => _TopSlingLayerState();
}

class _TopSlingLayerState extends State<TopSlidingLayer> with TickerProviderStateMixin {
  AnimationController _controller;
  Animation<Offset> _offset;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this, duration: Duration(milliseconds: widget.animationSpeed));
    _offset = Tween<Offset>(begin: Offset.zero, end: Offset(0.0, 0.50)).animate(_controller);
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: _offset,
      child: Container(
        height: widget.height,
        decoration: BoxDecoration(
          color: Colors.indigo,
        ),
        child: Column(
          children: <Widget>[
            Expanded(child: widget.child),
            InkWell(
              onTap: () {
                print('tapped');
                switch (_controller.status) {
                  case AnimationStatus.completed:
                    _controller.reverse();
                    break;
                  case AnimationStatus.dismissed:
                    _controller.forward();
                    break;
                  default:
                }
              },
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  'click me',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • @Niklas 你能帮帮我吗?

标签: flutter flutter-layout flutter-animation


【解决方案1】:

问题来自 SlideTransition 小部件中子容器的高度。 button out the container

当您点击按钮时,它会移出容器,因此您将无法再次点击它。

所以我删除了高度以拥有一个全屏容器,而是在墨水池周围放置了一个尺寸框,以提供与您相同的结果。

class _TopSlingLayerState extends State<TopSlidingLayer>
    with TickerProviderStateMixin {
  AnimationController _controller;
  Animation<Offset> _offset;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        vsync: this, duration: Duration(milliseconds: widget.animationSpeed));
    _offset = Tween<Offset>(begin: Offset.zero, end: Offset(0.0, 0.20))
        .animate(_controller);
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: _offset,
      child: Container(
        child: Column(
          children: <Widget>[
            Container(child: widget.child, height: widget.height),
            InkWell(
              onTap: () {
                print('tapped ${_controller.status}');
                switch (_controller.status) {
                  case AnimationStatus.completed:
                    _controller.reverse();
                    break;
                  case AnimationStatus.dismissed:
                    _controller.forward();
                    break;
                  default:
                }
              },
              child: SizedBox(
                width: double.infinity,
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.indigo,
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Text(
                      'click me',
                      style: TextStyle(color: Colors.white),
                      textAlign: TextAlign.center,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

我不知道它是否很好地回答了你的问题。

【讨论】:

  • 谢谢,如何从屏幕外部为容器设置动画以显示和隐藏到屏幕外部?
  • 我的荣幸。我真的不认为有可能真正离开屏幕,因为您可能会遇到溢出错误。你想要达到的效果对我来说就像一个背景菜单。 codelabs.developers.google.com/codelabs/mdc-104-flutter/#0
猜你喜欢
  • 2018-11-17
  • 2017-06-08
  • 2021-02-21
  • 2011-08-25
  • 2021-05-25
  • 2021-02-15
  • 2014-05-20
  • 1970-01-01
  • 2019-05-01
相关资源
最近更新 更多