2

AnimatedList在我的 Flutter 应用程序中使用。它正在工作,但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),
          ),
        ],
      ),
    );
  }

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

4

2 回答 2

0

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

于 2021-07-21T10:49:54.120 回答
0

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


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

于 2021-07-13T12:21:49.867 回答