flutter - 如何为一行中的 2 个项目的交换设置动画?

标签 flutter dart

我想做一些非常简单的事情。有一个带有 2 个小部件的 Row。当我按下一个按钮时,他们会交换订单。我希望这个订单交换是动画的。

我看过 AnimatedPositioned但它需要一个Stack。做这种事情的最佳方法是什么?

我以为 Animating position across row cells in Flutter回答了这个,但这是另一个不同的问题

最佳答案

您可以使用SlideAnimation 轻松地为一行中的小部件设置动画。 .请看下面的代码或者你可以直接在DartPad上运行代码https://dartpad.dev/e5d9d2c9c6da54b3f76361eac449ce42只需点击彩色框即可通过幻灯片动画交换它们的位置。

SlideAnimation

Animates the position of a widget relative to its normal position.

The translation is expressed as an Offset scaled to the child's size. For example, an Offset with a dx of 0.25 will result in a horizontal translation of one quarter the width of the child.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  List<Animation<Offset>> _offsetAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );
    _offsetAnimation = List.generate(
      2,
      (index) => Tween<Offset>(
        begin: const Offset(0.0, 0.0),
        end: Offset(index == 0 ? 1 : -1, 0.0),
      ).animate(_controller),
    );
  }

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

  void _animate() {
    _controller.status == AnimationStatus.completed
        ? _controller.reverse()
        : _controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Flutter Demo Row Animation")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                BoxWidget(
                  callBack: _animate,
                  text: "1",
                  color: Colors.red,
                  position: _offsetAnimation[0],
                ),
                BoxWidget(
                  callBack: _animate,
                  text: "2",
                  color: Colors.blue,
                  position: _offsetAnimation[1],
                )
              ],
            ),
            RaisedButton(
              onPressed: _animate,
              child: const Text("Swap"),
            )
          ],
        ),
      ),
    );
  }
}

class BoxWidget extends StatelessWidget {
  final Animation<Offset> position;
  final Function callBack;
  final String text;
  final Color color;

  const BoxWidget(
      {Key key, this.position, this.callBack, this.text, this.color})
      : super(key: key);
  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: position,
      child: GestureDetector(
        onTap: () => callBack(),
        child: Container(
          margin: const EdgeInsets.all(10),
          height: 50,
          width: 50,
          color: color,
          child: Center(
            child: Container(
              height: 20,
              width: 20,
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.white,
              ),
              child: Center(child: Text(text)),
            ),
          ),
        ),
      ),
    );
  }
}

关于flutter - 如何为一行中的 2 个项目的交换设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65175065/

相关文章:

ios - Flutter 问题,ios 应用程序无法打开且没有图标

ios - in_app_purchase 如何从 serverVerificationData 获取 android 验证所需的数据

Flutter 仅带有左上角和右上角的上边框

user-interface - StreamBuilder 仅在 Flutter 中的流结束时更新

dart - 如何实现Dart优先级队列(降序排列)

android - 如何在折线图x轴 flutter 中传递字符串参数

android - 在 android 库中添加 Flutter 功能作为依赖项

file-io - Dart文件读取到var

flutter - 如何在Flutter中使用Hive进行库存 list ?

Flutter - 避免 ListView 重建