繁体   English   中英

Flutter 关闭动画列表中的项目无法正常工作

[英]Flutter dismiss item in Animated List not working correctly

我在 Flutter 应用程序中使用AnimatedList 它正在工作,但dismiss -behavior 并不顺利,因为在转换时被解雇的项目会更改为上面的项目。

是一个屏幕视频,以便更好地理解。 专注于文本,它总是在过渡中更改为第一项的文本(“Hallo Dasist 1”)。

我遵循了本教程,您还可以在 CodePad 上对其进行测试,您可以在其中看到完全相同的行为。 这不是所需的动画...有谁知道我该如何解决这个问题?

以下是我关闭这些项目的方式:

 _removeMemoryAtIndex(int index, Month currentMonth) {
    listKey.currentState!.removeItem(
      index,
      (_, animation) => slideIt(
        context,
        currentMonth,
        0,
        CurvedAnimation(
            parent: animation,
            curve: Curves.easeIn,
            reverseCurve: Curves.easeOut),
      ),
      duration: Duration(
        milliseconds: 500,
      ),
    );

    Provider.of<MemoryImageProvider>(context, listen: false)
        .removeMemoryAt(index: index);
  }

和我的slideIt - 动画功能:

Widget slideIt(
    BuildContext context,
    Month currentMonth,
    int index,
    animation,
  ) {
    Memory memory = currentMonth.memories[index];
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(-1, 0),
        end: Offset(0, 0),
      ).animate(
        CurvedAnimation(
          parent: animation,
          curve: Curves.easeIn,
          reverseCurve: Curves.easeOut,
        ),
      ),
      child: Column(
        children: [
          MemoryTile(
            memory: memory,
            monthName: currentMonth.name,
            onTapped: () {
              _removeMemoryAtIndex(index, currentMonth);
              print('tap on ${memory.description}');
            },
          ),
          SizedBox(
            height: scaleWidth(20),
          ),
        ],
      ),
    );
  }

如果您需要更多信息,请告诉我们! 任何帮助表示赞赏。 我希望这可以以某种方式修复......

我们可以为此创建一个临时模型数据,这就是它的工作方式。


import 'package:flutter/material.dart';

class MemoryTile {
  int index;
  String name;
  MemoryTile({
    required this.index,
    required this.name,
  });
}

final items = List.generate(
  10,
  (index) => MemoryTile(
    index: index,
    name: "name $index",
  ),
);

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

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

class _AnimLtestState extends State<AnimLtest> {
  final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();

  // Remove the selected item from the list model.

  _removeMemoryAtIndex(int index, MemoryTile currentMonth) {
    var tempMOnth = currentMonth;

    listKey.currentState!.removeItem(
      index,
      (_, animation) => slideIt(
        context,
        tempMOnth,
        CurvedAnimation(
            parent: animation,
            curve: Curves.easeIn,
            reverseCurve: Curves.easeOut),
      ),
      duration: Duration(
        milliseconds: 500,
      ),
    );

    items.removeAt(index);
    // setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedList(
      key: listKey,
      initialItemCount: items.length,
      itemBuilder: (context, index, animation) => Container(
        child: InkWell(
          onTap: () {
            _removeMemoryAtIndex(index, items[index]);
          },
          child: slideIt(
            context,
            items[index],
            animation,
          ),
        ),
      ),
    );
  }

  Widget slideIt(
    BuildContext context,
    MemoryTile memory,
    animation,
  ) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(-1, 0),
        end: Offset(0, 0),
      ).animate(
        CurvedAnimation(
          parent: animation,
          curve: Curves.easeIn,
          reverseCurve: Curves.easeOut,
        ),
      ),
      child: Row(
        children: [
          CircleAvatar(
            child: Text(memory.index.toString()),
          ),
          Text("Name+> ${memory.name}")
        ],
      ),
    );
  }
}

愚蠢的错误...如果您查看_removeMemoryAtIndex方法,我总是将0作为索引传递...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM