remove futurebuilder from groups view and refactor
This commit is contained in:
@@ -19,15 +19,15 @@ class GroupsView extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _GroupsViewState extends State<GroupsView> {
|
class _GroupsViewState extends State<GroupsView> {
|
||||||
late Future<List<Group>> _allGroupsFuture;
|
|
||||||
late final AppDatabase db;
|
late final AppDatabase db;
|
||||||
|
late List<Group> loadedGroups;
|
||||||
|
bool isLoading = true;
|
||||||
|
|
||||||
final player = Player(name: 'Skeleton Player');
|
List<Group> groups = List.filled(
|
||||||
late final List<Group> skeletonData = List.filled(
|
|
||||||
7,
|
7,
|
||||||
Group(
|
Group(
|
||||||
name: 'Skeleton Match',
|
name: 'Skeleton Group',
|
||||||
members: [player, player, player, player, player, player],
|
members: List.filled(6, Player(name: 'Skeleton Player')),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -35,10 +35,17 @@ class _GroupsViewState extends State<GroupsView> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
db = Provider.of<AppDatabase>(context, listen: false);
|
db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
_allGroupsFuture = Future.wait([
|
Future.wait([
|
||||||
db.groupDao.getAllGroups(),
|
db.groupDao.getAllGroups(),
|
||||||
Future.delayed(minimumSkeletonDuration),
|
Future.delayed(minimumSkeletonDuration),
|
||||||
]).then((results) => results[0] as List<Group>);
|
]).then((results) {
|
||||||
|
loadedGroups = results[0] as List<Group>;
|
||||||
|
setState(() {
|
||||||
|
groups = loadedGroups
|
||||||
|
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||||
|
});
|
||||||
|
isLoading = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -48,50 +55,30 @@ class _GroupsViewState extends State<GroupsView> {
|
|||||||
body: Stack(
|
body: Stack(
|
||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
children: [
|
children: [
|
||||||
FutureBuilder<List<Group>>(
|
AppSkeleton(
|
||||||
future: _allGroupsFuture,
|
enabled: isLoading,
|
||||||
builder:
|
child: Visibility(
|
||||||
(BuildContext context, AsyncSnapshot<List<Group>> snapshot) {
|
visible: groups.isNotEmpty,
|
||||||
if (snapshot.hasError) {
|
replacement: const Center(
|
||||||
return const Center(
|
child: TopCenteredMessage(
|
||||||
child: TopCenteredMessage(
|
icon: Icons.info,
|
||||||
icon: Icons.report,
|
title: 'Info',
|
||||||
title: 'Error',
|
message: 'No groups created yet',
|
||||||
message: 'Group data couldn\'t\nbe loaded',
|
),
|
||||||
),
|
),
|
||||||
|
child: ListView.builder(
|
||||||
|
padding: const EdgeInsets.only(bottom: 85),
|
||||||
|
itemCount: groups.length + 1,
|
||||||
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
if (index == groups.length) {
|
||||||
|
return SizedBox(
|
||||||
|
height: MediaQuery.paddingOf(context).bottom - 20,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (snapshot.connectionState == ConnectionState.done &&
|
return GroupTile(group: groups[index]);
|
||||||
(!snapshot.hasData || snapshot.data!.isEmpty)) {
|
|
||||||
return const Center(
|
|
||||||
child: TopCenteredMessage(
|
|
||||||
icon: Icons.info,
|
|
||||||
title: 'Info',
|
|
||||||
message: 'No groups created yet',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
final bool isLoading =
|
|
||||||
snapshot.connectionState == ConnectionState.waiting;
|
|
||||||
final List<Group> groups =
|
|
||||||
isLoading ? skeletonData : (snapshot.data ?? [])
|
|
||||||
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
|
||||||
return AppSkeleton(
|
|
||||||
enabled: isLoading,
|
|
||||||
child: ListView.builder(
|
|
||||||
padding: const EdgeInsets.only(bottom: 85),
|
|
||||||
itemCount: groups.length + 1,
|
|
||||||
itemBuilder: (BuildContext context, int index) {
|
|
||||||
if (index == groups.length) {
|
|
||||||
return SizedBox(
|
|
||||||
height: MediaQuery.paddingOf(context).bottom - 20,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return GroupTile(group: groups[index]);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
bottom: MediaQuery.paddingOf(context).bottom,
|
bottom: MediaQuery.paddingOf(context).bottom,
|
||||||
@@ -108,7 +95,7 @@ class _GroupsViewState extends State<GroupsView> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
setState(() {
|
setState(() {
|
||||||
_allGroupsFuture = db.groupDao.getAllGroups();
|
//_allGroupsFuture = db.groupDao.getAllGroups();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user