fix: use own grouped list
the grouped list from sticky_grouped_list has some problemsmain
parent
6961c744a7
commit
642f5b70a2
|
|
@ -2,12 +2,14 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:release_schedule/model/dates.dart';
|
import 'package:release_schedule/model/dates.dart';
|
||||||
import 'package:release_schedule/model/movie.dart';
|
import 'package:release_schedule/model/movie.dart';
|
||||||
import 'package:release_schedule/view/movie_item.dart';
|
import 'package:release_schedule/view/movie_item.dart';
|
||||||
import 'package:sticky_grouped_list/sticky_grouped_list.dart';
|
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
|
||||||
|
|
||||||
class MovieList extends StatelessWidget {
|
class MovieList extends StatelessWidget {
|
||||||
final List<MovieData> movies;
|
final List<MovieData> movies;
|
||||||
final bool Function(MovieData)? filter;
|
final bool Function(MovieData)? filter;
|
||||||
const MovieList(this.movies, {this.filter, super.key});
|
MovieList(List<MovieData> movies, {this.filter, super.key})
|
||||||
|
: movies = movies.toList(),
|
||||||
|
super();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
|
@ -80,14 +82,13 @@ class MovieList extends StatelessWidget {
|
||||||
}
|
}
|
||||||
return max;
|
return max;
|
||||||
}();
|
}();
|
||||||
return StickyGroupedListView<int, DateWithPrecision>(
|
return GroupedList<DateWithPrecision>(
|
||||||
elements: indexMap,
|
itemCount: indexMap.length,
|
||||||
floatingHeader: true,
|
groupBy: (index) =>
|
||||||
groupBy: (index) => movies[index].releaseDate.dateWithPrecision,
|
movies[indexMap[index]].releaseDate.dateWithPrecision,
|
||||||
groupSeparatorBuilder: (index) => buildGroupSeparator(
|
groupSeparatorBuilder: (date) => buildGroupSeparator(context, date),
|
||||||
context, movies[index].releaseDate.dateWithPrecision),
|
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
return MovieItem(movies[index]);
|
return MovieItem(movies[indexMap[index]]);
|
||||||
},
|
},
|
||||||
initialScrollIndex: firstMovieTodayOrAfterIndex,
|
initialScrollIndex: firstMovieTodayOrAfterIndex,
|
||||||
);
|
);
|
||||||
|
|
@ -107,16 +108,71 @@ class MovieList extends StatelessWidget {
|
||||||
}
|
}
|
||||||
return max;
|
return max;
|
||||||
}();
|
}();
|
||||||
return StickyGroupedListView<MovieData, DateWithPrecision>(
|
return GroupedList<DateWithPrecision>(
|
||||||
elements: movies,
|
itemCount: movies.length,
|
||||||
floatingHeader: true,
|
groupBy: (index) => movies[index].releaseDate.dateWithPrecision,
|
||||||
groupBy: (movie) => movie.releaseDate.dateWithPrecision,
|
groupSeparatorBuilder: (date) => buildGroupSeparator(context, date),
|
||||||
groupSeparatorBuilder: (movie) =>
|
itemBuilder: (context, index) {
|
||||||
buildGroupSeparator(context, movie.releaseDate.dateWithPrecision),
|
return MovieItem(movies[index]);
|
||||||
itemBuilder: (context, movie) {
|
|
||||||
return MovieItem(movie);
|
|
||||||
},
|
},
|
||||||
initialScrollIndex: firstMovieTodayOrAfterIndex,
|
initialScrollIndex: firstMovieTodayOrAfterIndex,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class GroupedList<GroupType> extends StatelessWidget {
|
||||||
|
final int itemCount;
|
||||||
|
final int initialScrollIndex;
|
||||||
|
final Widget Function(BuildContext, int) itemBuilder;
|
||||||
|
final Widget Function(GroupType) groupSeparatorBuilder;
|
||||||
|
final GroupType Function(int) groupBy;
|
||||||
|
|
||||||
|
const GroupedList(
|
||||||
|
{required this.itemCount,
|
||||||
|
required this.itemBuilder,
|
||||||
|
required this.groupSeparatorBuilder,
|
||||||
|
required this.groupBy,
|
||||||
|
this.initialScrollIndex = 0,
|
||||||
|
super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
if (itemCount == 0) {
|
||||||
|
return Container();
|
||||||
|
}
|
||||||
|
List<({int index, GroupType group})> newGroupStarts = [
|
||||||
|
(index: 0, group: groupBy(0))
|
||||||
|
];
|
||||||
|
int internalInitialScrollIndex = initialScrollIndex + 1;
|
||||||
|
GroupType last = newGroupStarts[0].group;
|
||||||
|
for (int i = 1; i < itemCount; i++) {
|
||||||
|
final GroupType current = groupBy(i);
|
||||||
|
if (current != last) {
|
||||||
|
newGroupStarts.add((index: i, group: current));
|
||||||
|
if (initialScrollIndex > i) {
|
||||||
|
internalInitialScrollIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
last = current;
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget itemAndSeparatorBuilder(BuildContext context, int index) {
|
||||||
|
int itemIndex = index;
|
||||||
|
for (int i = 0; i < newGroupStarts.length; i++) {
|
||||||
|
if (newGroupStarts[i].index > itemIndex) {
|
||||||
|
break;
|
||||||
|
} else if (newGroupStarts[i].index == itemIndex) {
|
||||||
|
return groupSeparatorBuilder(groupBy(itemIndex));
|
||||||
|
}
|
||||||
|
itemIndex--;
|
||||||
|
}
|
||||||
|
return itemBuilder(context, itemIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ScrollablePositionedList.builder(
|
||||||
|
itemCount: itemCount + newGroupStarts.length,
|
||||||
|
itemBuilder: itemAndSeparatorBuilder,
|
||||||
|
initialScrollIndex: internalInitialScrollIndex,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
10
pubspec.lock
10
pubspec.lock
|
|
@ -220,7 +220,7 @@ packages:
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.6"
|
version: "2.1.6"
|
||||||
scrollable_positioned_list:
|
scrollable_positioned_list:
|
||||||
dependency: transitive
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: scrollable_positioned_list
|
name: scrollable_positioned_list
|
||||||
sha256: "1b54d5f1329a1e263269abc9e2543d90806131aa14fe7c6062a8054d57249287"
|
sha256: "1b54d5f1329a1e263269abc9e2543d90806131aa14fe7c6062a8054d57249287"
|
||||||
|
|
@ -248,14 +248,6 @@ packages:
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.11.0"
|
version: "1.11.0"
|
||||||
sticky_grouped_list:
|
|
||||||
dependency: "direct main"
|
|
||||||
description:
|
|
||||||
name: sticky_grouped_list
|
|
||||||
sha256: "40398cb90321f07cbdbdd3049c27a5f048bd75a13abb19453b07a956f53a0eda"
|
|
||||||
url: "https://pub.dev"
|
|
||||||
source: hosted
|
|
||||||
version: "3.1.0"
|
|
||||||
stream_channel:
|
stream_channel:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ dependencies:
|
||||||
http: ^1.1.0
|
http: ^1.1.0
|
||||||
intl: ^0.18.1
|
intl: ^0.18.1
|
||||||
get_storage: ^2.1.1
|
get_storage: ^2.1.1
|
||||||
sticky_grouped_list: ^3.1.0
|
scrollable_positioned_list: ^0.3.8
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue