【问题标题】:How do I animate a flutter widget on top of a widget that comes after it in the hierarchy?如何在层次结构中紧随其后的小部件之上为颤振小部件设置动画?
【发布时间】:2020-09-28 15:04:19
【问题描述】:

我有一个简单的屏幕,在 Column 小部件内有两个小部件。当我点击顶部小部件时,我希望它向下移动到第二个小部件。该部分正在工作,但是当它向下滑动时,它会在底部小部件的后面而不是在底部小部件的前面。我假设这是因为底部小部件是在我的代码中创建的第二个。有没有办法将顶部小部件带到前面?这是我的代码:

  AnimationController _controller;
  Animation<Offset> _offsetAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    );
    _offsetAnimation = Tween<Offset>(
      begin: Offset.zero,
      end: const Offset(0.0, 1.0),
    ).animate(CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    ));
  }

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

  @override
  Widget build(BuildContext context) {
    GlobalKey<FlipCardState> cardKey = GlobalKey<FlipCardState>();

    return Scaffold(
      backgroundColor: Colors.grey,
      body: Center(
          child: Column(
              children: <Widget>[
                SizedBox(height: 30),
                Expanded(
                  flex: 1,
                  child: SlideTransition(
                            key: cardKey,
                            position: _offsetAnimation,
                            child: GestureDetector(
                              onTap: () {
                                _controller.addStatusListener((status) {
                                  if (status == AnimationStatus.completed) {
                                    _controller.reset();
                                  }
                                });
                                _controller.forward();
                              },
                            child: EmptyPile(
                              title: "First Card",
                            ),
                            ),
                        ),
                  ),
                Expanded(flex: 1,
                  child: EmptyPile(
                    title: "Second Card",
                  ),
                ),
              ]
          ),
      ),
    );

  }

为了完整性,但我认为这并不重要,这是我的EmptyPile 代码:

class EmptyPile extends StatelessWidget {
  EmptyPile({Key key, this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {

    return Container(
      width: MediaQuery.of(context).size.width * 0.7,
      margin: EdgeInsets.fromLTRB(0, 30, 0, 30),
      decoration: borderDecoration(),
      child: Center(
        child: Text(
          title,
          textAlign: TextAlign.center,
          style: TextStyle(fontSize: 30.0),
        ),
      ),
    );
  }

}

最后是我的边框装饰:

BoxDecoration borderDecoration() {
  return BoxDecoration(
      border: Border.all(
        width: 5,
      ),
      borderRadius: BorderRadius.all(
          Radius.circular(10)
      ),
      color: Colors.white
  );
}

【问题讨论】:

    标签: flutter flutter-layout flutter-animation


    【解决方案1】:

    虽然很简单,但我首先将它们包装在Stack() 中。在这种情况下,您可以访问图层,并且可以使用您的序列定义哪个小部件在前面,哪个不在前面。

    I'm assuming this is because the bottom widget is created second in my code. 
    

    您应该对上述方法有一个有效的解决方法。

    希望对你有帮助!

    【讨论】:

    • 我一直在玩Stack,希望它能修复它,但我没有成功。您能否再分享一下您认为Stack 应该在哪里?
    • 我不能让你的代码基本上运行,我会首先尝试使用堆栈而不是列。与列中一样,您具有从上到下的层次结构。请记住,如果您分层考虑,堆栈中的最后一项就是最前面的一项。
    • 如果我删除堆栈,那么这两个将相互叠放,但在我开始动画之前,我希望它们垂直对齐,就像它们在列中一样。
    猜你喜欢
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 2023-03-06
    • 2022-01-27
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2018-10-18
    相关资源
    最近更新 更多