Flutter 列表项通过动画改变位置

ade*_*190 6 android android-recyclerview flutter flutter-animation

下面的代码有一个从 1 到 100 的列表,顶部有三个按钮来更改列表的顺序(递增、递减或随机播放)。

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class SamplePage extends StatefulWidget {
  SamplePage({Key key}) : super(key: key);

  @override
  _SamplePageState createState() => _SamplePageState();
}

class _SamplePageState extends State<SamplePage> {
  _SamplePageState();

  final _list = new List<int>.generate(100, (i) => i + 1);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          TextButton(
            onPressed: () => setState(() {
              _list.sort((a, b) => a.compareTo(b));
            }),
            child: Text(
              "Sort increasing",
            ),
          ),
          TextButton(
            onPressed: () => setState(() {
              _list.sort((a, b) => b.compareTo(a));
            }),
            child: Text(
              "Sort decreasing",
            ),
          ),
          TextButton(
            onPressed: () => setState(() {
              _list.shuffle();
            }),
            child: Text(
              "Shuffle",
            ),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: _list.length,
              itemBuilder: (context, index) => Text(
                '${_list[index]}',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 16.0,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但由于我是颤振的新手,我想知道是否可以使列表中的项目随动画而变化。

在android世界中,我们可以通过RecyclerView适配器中的setHasStableIds来做到这一点,我想flutter上的方法可能会有所不同,但我想有可能达到相同的结果,我想知道如何实现。


已编辑

我在这里添加了一个 gif,展示了当您使用本机 recyclerview 时项目如何以动画方式更改位置,这种位置的动画更改正是我正在寻找的。

动画样本

Yus*_*luc 0

我自己还没有测试过,但也许 AnimatedListView 可以解决这个问题。

https://api.flutter.dev/flutter/widgets/AnimatedList-class.html

但正如我自己所说,我还没有测试过。