【问题标题】:Flutter : Widget Switch Between AnimationFlutter:动画之间的小部件切换
【发布时间】:2021-01-14 07:22:35
【问题描述】:

我尝试为小部件切换位置制作动画。下图显示了 A 和 B 小部件,我想在单击按钮时在 A 到 B / B 到 A 之间切换。但是一开始我不知道从哪里开始,不知道有没有什么插件可以实现,任何建议都可以帮助我,只是和一些技巧,我愿意研究建议插件。

我尝试使用 AnimatedAlign

Container(
height: 120.0,
color: Colors.black,
child: Column(
  children: [
    AnimatedAlign(
      alignment: _alignment.value = _alignment.value == Alignment.topCenter ? Alignment.bottomCenter : _alignment.value,
      curve: Curves.ease,
      duration: Duration(milliseconds: 500),
      child: Padding(
        padding: EdgeInsets.all(2.5),
          child: Container(
          height: 55,
          color: Colors.red,
        ),
      )
    ),
    AnimatedAlign(
      alignment: _alignment.value = _alignment.value == Alignment.bottomCenter ? Alignment.topCenter : _alignment.value,
      curve: Curves.ease,
      duration: Duration(milliseconds: 500),
      child: Padding(
        padding: EdgeInsets.all(2.5),
          child: Container(
          height: 55,
          color: Colors.blue,
        ),
      ),
    )
  ],
),
),
RaisedButton(
onPressed: () {
  _alignment.value = Alignment.bottomCenter;
},
child: Text("Switch Position"),
)

【问题讨论】:

  • 你看到这个小部件了吗? youtube.com/watch?v=2W7POjFb88g
  • @mohandesR 不,但是我看了之后,这不是那个,因为我需要的是 A 和 B 仍然存在,只是交换它们的位置

标签: flutter dart


【解决方案1】:

我创建了一个示例...这是有效的。我使用了动画定位

import 'package:flutter/material.dart';

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

class MyClass extends StatefulWidget {
  @override
  _MyClassState createState() => _MyClassState();
}

class _MyClassState extends State<MyClass>{

  bool change = true;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(title: Text('Title')),
        body: Stack(
          children: <Widget>[
            AnimatedPositioned(
              top: change ?  50 : 150,
              left: 50,
              right: 50,
              duration: Duration(milliseconds: 300),
              child: red()
            ),
            AnimatedPositioned(
                top: change ?  150 : 50,
                left: 50,
                right: 50,
              duration: Duration(milliseconds: 300),
              child: blue()
            ),
            Positioned(
              top: 210,
              left: 50,
              right: 50,
              child: Center(
                child: InkWell(
                  onTap: (){
                    setState(() {
                      change = !change;
                    });
                  },
                  child: Container(
                    color: Colors.amber,
                    width: 65,
                    height: 50,
                    child: Center(child: Text("click me")),
                  ),
                ),
              ),
            )
          ],
        ),
        ),
    );
  }
}

red(){
  return Container(
    width: 120,
    height: 50,
    color: Colors.red,
  );
}
blue(){
  return Container(
    width: 120,
    height: 50,
    color: Colors.blue,
  );
}

【讨论】:

  • 手动动画定位小部件是核心选项,除非您没有其他实际选择,否则我不会推荐它。有一种方法可以用 slivers 隐式地做到这一点,但我不记得它是如何进行的。
  • @Abion47 你推荐的方法是什么
猜你喜欢
  • 2021-10-07
  • 2019-11-30
  • 2022-12-14
  • 2020-10-20
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
相关资源
最近更新 更多