Flutter 列表项通过动画改变位置 [英] Flutter list items change position with animation

查看:77
本文介绍了Flutter 列表项通过动画改变位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码有一个从 1 到 100 的列表,顶部有三个按钮来改变列表的顺序(递增、递减或洗牌).

import 'package:flutter/material.dart';导入 'package:flutter/widgets.dart';类 SamplePage 扩展了 StatefulWidget {SamplePage({Key key}) : super(key: key);@覆盖_SamplePageState createState() =>_SamplePageState();}class _SamplePageState 扩展 State{_SamplePageState();final _list = new List.generate(100, (i) => i + 1);@覆盖小部件构建(BuildContext 上下文){返回脚手架(正文:列(孩子们: [文本按钮(onPressed: () =>设置状态((){_list.sort((a, b) => a.compareTo(b));}),孩子:文本(排序递增",),),文本按钮(onPressed: () =>设置状态((){_list.sort((a, b) => b.compareTo(a));}),孩子:文本(排序递减",),),文本按钮(onPressed: () =>设置状态((){_list.shuffle();}),孩子:文本(洗牌",),),展开(孩子:ListView.builder(itemCount: _list.length,itemBuilder:(上下文,索引)=>文本('${_list[index]}',textAlign: TextAlign.center,样式:文本样式(字体大小:16.0,),),),),],),);}}

一切正常,但由于我是 flutter 新手,我想知道是否可以通过动画更改列表中的项目.

在安卓世界,我们可以通过

解决方案

你需要使用:ReorderableListView和他的属性onReorder

ReorderableListView(儿童:列表.map((m) => ListTile(键:对象键(米),标题:文本(米),)).toList(),onReorder: _onReorder,)

源链接:https://www.programmersought.com/article/4678939801/

The following code has a list from 1 to 100, on top has three buttons to change the order of the list (increasing, decreasing or shuffle).

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,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

Everything works but as I'm new to flutter, I'm wondering if it is possible to make the items of the list change with animation.

On android world, we can do it through the setHasStableIds in a RecyclerView adapter, I imagine the approach on flutter maybe be different, but I guess it is possible to achieve the same result, I would like to know how.


Edited

I'm adding here a gif of how items change position animated when you are using a native recyclerview, this animated change of positions is what I'm looking for.

解决方案

You need to use: ReorderableListView and his property onReorder

ReorderableListView(
            children: list
                .map((m) => ListTile(
                key: ObjectKey(m),
                title: Text(m),
            )).toList(),
            onReorder: _onReorder,
        )

Source link: https://www.programmersought.com/article/4678939801/

这篇关于Flutter 列表项通过动画改变位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆