Flutter slideTransition没有动画

Pet*_*and 7 mobile user-interface animation slide flutter

因此,我试图在颤动中创建一个琐碎的幻灯片过渡元素,但遇到了一些困难。下面的操作是等待动画时间,然后仅显示Text("hello there sailor")。我不知道为什么这不是动画效果-看起来与以前的帖子非常相似,其中包含一个琐碎的示例(将动画滑动到底部抖动)。

这就是我所谓的以下代码:DeleteCheck(offsetBool: widget.model.deleteNotify, widthSlide: 0.50*width100)where double width100 = MediaQuery.of(context).size.width;

有人看到我在做什么错吗?

class DeleteCheck extends StatefulWidget{

  final offsetBool;
  final double widthSlide;

  DeleteCheck({
    Key key, 
    this.offsetBool, 
    this.widthSlide
  }): super(key: key);

  @override 
  State<StatefulWidget> createState() {
    return new _MyDeleteCheck();
  }
}

class _MyDeleteCheck extends State<DeleteCheck> with TickerProviderStateMixin {
  AnimationController _controller;
  Animation<Offset> _offsetFloat; 

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    );

    _offsetFloat = Tween<Offset>(begin: Offset(widget.widthSlide, 0.0), end: Offset.zero)
        .animate(_controller);

    _offsetFloat.addListener((){
      setState((){});
    });

    _controller.forward();

  }

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

  @override
  Widget build(BuildContext context) {
    double height100 = MediaQuery.of(context).size.height;
    double width100 = MediaQuery.of(context).size.width;
    return new SlideTransition(
      position: _offsetFloat,
      child: Container(
        color: Colors.cyan,
        width: 0.525*width100-3.0,
        child: Text("hello there sailor")
      )
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Nik*_*las 13

我有个好消息要给你!您的代码正在运行!:)动画看起来好像没有发生,因为它移动的距离很大。传递给SlideTransitionOffset与其子项的大小有关。例如,您的孩子有,而您用偏移了,您的孩子将向右移动像素。width: 100.0Offset(2.0, 0.0)200.0

只需尝试更改begin: Offset(widget.widthSlide, 0.0), end: Offset.zero 为即可begin: Offset(2.0, 0.0), end: Offset.zero。您会看到文本从屏幕的右到中心缓慢移动。因此,您只需要调整参数即可。

SlideTransition已更改

无论如何,这里有一些优化代码的其他建议:

  • 如果使用的是诸如SlideTransition之类的预构建AnimatedWidgets,则无需在控制器上调用with 。在本身需要照顾它。因此,您可以删除以下几行:addListenersetStateAnimatedWidget

行数:

_offsetFloat.addListener((){
  setState((){});
});
Run Code Online (Sandbox Code Playgroud)
  • 同样也不必调用const构造函数。您可以像这样忽略此关键字new。每种情况下,编译器都会优化并选择合适的构造函数。