From c5aa673963c00f341da39c69fe5f4f91ddf41c31 Mon Sep 17 00:00:00 2001 From: cp-sneha-s Date: Sat, 1 Jun 2024 16:42:11 +0530 Subject: [PATCH 1/4] Investigate issue of item update in the list --- example/lib/home_page.dart | 30 +++++++++++++++++++- lib/src/builder/motion_animated_builder.dart | 4 +-- lib/src/builder/motion_list_base.dart | 3 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/example/lib/home_page.dart b/example/lib/home_page.dart index b6bdbce..a2df7c3 100644 --- a/example/lib/home_page.dart +++ b/example/lib/home_page.dart @@ -11,6 +11,26 @@ class User { final int index; User({required this.name, required this.index}); + + @override + String toString() { + return 'User{name: $name, index: $index}'; + } + + @override + bool operator ==(Object other) { + if (identical(this, other)) return true; + + return other is User && + other.index == index; + } + + @override + // TODO: implement hashCode + int get hashCode => index.hashCode ; + + + } class HomePage extends StatefulWidget { @@ -31,6 +51,13 @@ class _HomePageState extends State { void insert() { addedNumber += 1; + + int index = list.indexWhere((element) => element.index==2); + User user = User(name: "User 3", index: index); + + list[index] = user; + + setState(() { list.insert(1, User(name: "User $addedNumber", index: addedNumber)); }); @@ -159,8 +186,9 @@ class _HomePageState extends State { items: list, scrollDirection: Axis.vertical, itemBuilder: (BuildContext context, int index) { + print(ValueKey(list[index].toString())); return ItemCard( - key: Key(list[index].name), + key: ValueKey(list[index]), index: list[index].index); }, sliverGridDelegate: diff --git a/lib/src/builder/motion_animated_builder.dart b/lib/src/builder/motion_animated_builder.dart index c70ab95..5addf9c 100644 --- a/lib/src/builder/motion_animated_builder.dart +++ b/lib/src/builder/motion_animated_builder.dart @@ -640,7 +640,7 @@ class MotionBuilderState extends State } final Widget child = widget.onReorder != null - ? reordreableItemBuilder(context, _itemIndexToIndex(index)) + ? reordrableItemBuilder(context, _itemIndexToIndex(index)) : widget.itemBuilder(context, _itemIndexToIndex(index)); assert(() { @@ -679,7 +679,7 @@ class MotionBuilderState extends State return SliverChildBuilderDelegate(_itemBuilder, childCount: _itemsCount); } - Widget reordreableItemBuilder(BuildContext context, int index) { + Widget reordrableItemBuilder(BuildContext context, int index) { final Widget item = widget.itemBuilder(context, index); final Widget itemWithSemantics = _wrapWithSemantics(item, index); diff --git a/lib/src/builder/motion_list_base.dart b/lib/src/builder/motion_list_base.dart index b8b5776..cc0dd51 100644 --- a/lib/src/builder/motion_list_base.dart +++ b/lib/src/builder/motion_list_base.dart @@ -127,13 +127,13 @@ abstract class MotionListBaseState< void initState() { super.initState(); oldList = List.from(widget.items); + addEffects(enterTransition, _enterAnimations, enter: true); addEffects(exitTransition, _exitAnimations, enter: false); } @override void didUpdateWidget(covariant B oldWidget) { - super.didUpdateWidget(oldWidget); final newList = widget.items; if (!listEquals(oldWidget.enterTransition, enterTransition)) { _enterAnimations = []; @@ -145,6 +145,7 @@ abstract class MotionListBaseState< } calculateDiff(oldList, newList); oldList = List.from(newList); + super.didUpdateWidget(oldWidget); } void addEffects(List effects, List enteries, From cbaefff5da381b844002171c5f713e18b99d9edb Mon Sep 17 00:00:00 2001 From: cp-sneha-s Date: Sat, 1 Jun 2024 17:02:14 +0530 Subject: [PATCH 2/4] Add equality checks for object in example app --- example/lib/home_page.dart | 23 ++++---------------- lib/src/builder/motion_animated_builder.dart | 4 ++-- pubspec.yaml | 2 +- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/example/lib/home_page.dart b/example/lib/home_page.dart index a2df7c3..4a8378e 100644 --- a/example/lib/home_page.dart +++ b/example/lib/home_page.dart @@ -12,25 +12,17 @@ class User { User({required this.name, required this.index}); - @override - String toString() { - return 'User{name: $name, index: $index}'; - } + // To prevent unnecessary animations when updating items in a list, it's essential to correctly implement the == operator and hashCode for your list item class. + // This allows the list to recognize items with the same data as equal, even if they are different instances. @override bool operator ==(Object other) { if (identical(this, other)) return true; - - return other is User && - other.index == index; + return other is User && other.index == index; } @override - // TODO: implement hashCode - int get hashCode => index.hashCode ; - - - + int get hashCode => index.hashCode; } class HomePage extends StatefulWidget { @@ -52,12 +44,6 @@ class _HomePageState extends State { void insert() { addedNumber += 1; - int index = list.indexWhere((element) => element.index==2); - User user = User(name: "User 3", index: index); - - list[index] = user; - - setState(() { list.insert(1, User(name: "User $addedNumber", index: addedNumber)); }); @@ -186,7 +172,6 @@ class _HomePageState extends State { items: list, scrollDirection: Axis.vertical, itemBuilder: (BuildContext context, int index) { - print(ValueKey(list[index].toString())); return ItemCard( key: ValueKey(list[index]), index: list[index].index); diff --git a/lib/src/builder/motion_animated_builder.dart b/lib/src/builder/motion_animated_builder.dart index 5addf9c..0b94a17 100644 --- a/lib/src/builder/motion_animated_builder.dart +++ b/lib/src/builder/motion_animated_builder.dart @@ -640,7 +640,7 @@ class MotionBuilderState extends State } final Widget child = widget.onReorder != null - ? reordrableItemBuilder(context, _itemIndexToIndex(index)) + ? reorderableItemBuilder(context, _itemIndexToIndex(index)) : widget.itemBuilder(context, _itemIndexToIndex(index)); assert(() { @@ -679,7 +679,7 @@ class MotionBuilderState extends State return SliverChildBuilderDelegate(_itemBuilder, childCount: _itemsCount); } - Widget reordrableItemBuilder(BuildContext context, int index) { + Widget reorderableItemBuilder(BuildContext context, int index) { final Widget item = widget.itemBuilder(context, index); final Widget itemWithSemantics = _wrapWithSemantics(item, index); diff --git a/pubspec.yaml b/pubspec.yaml index 894d1f4..c1459e1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: animated_reorderable_list description: A Flutter Reorderable Animated List with simple implementation and smooth transition. -version: 1.0.5 +version: 1.0.6 repository: https://github.com/canopas/animated_reorderable_list environment: From 6f6e88d24f91043550742c02ccdd7ebf1b311eb1 Mon Sep 17 00:00:00 2001 From: cp-sneha-s Date: Sat, 1 Jun 2024 18:03:13 +0530 Subject: [PATCH 3/4] Update doc --- .github/workflows/publish.yml | 2 +- CHANGELOG.md | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index af19e73..5d00045 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -5,7 +5,7 @@ on: branches: - main tags: - - '[0-9]+.[0-9]+.[0-9]+*' + - 'v[0-9]+.[0-9]+.[0-9]+*' jobs: publish: diff --git a/CHANGELOG.md b/CHANGELOG.md index 2514401..44a0eda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,19 @@ -# 1.0.5 (Apr 30, 2024) +# v1.0.6 + +# Changelog + +## Bug Fixes +- Add equality check in example app to prevent animation on update of item in list. + + +# v1.0.5 # Changelog ## Bug Fixes - Fix `onReorderEnd` callback not being called after reordering is completed. -# 1.0.4 (Apr 14, 2024) +# v1.0.4 # Changelog @@ -17,3 +25,5 @@ + + From 548a80d093ea36e16ee000442365c4e128d3416f Mon Sep 17 00:00:00 2001 From: cp-sneha-s Date: Sat, 1 Jun 2024 18:19:16 +0530 Subject: [PATCH 4/4] Update doc --- .github/workflows/publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 5d00045..09b1d59 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -14,4 +14,4 @@ jobs: uses: dart-lang/setup-dart/.github/workflows/publish.yml@v1 with: environment: 'pub.dev' - working-directory: path/to/package/within/repository +# working-directory: path/to/package/within/repository