chore(group): fix delay show group when switch and convert to relative import
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'groups/groups_screen.dart';
|
||||
import 'groups/groups_model.dart';
|
||||
import '../../bloc/inter_family_bloc.dart';
|
||||
import 'inter_family_widget.dart';
|
||||
import '../../product/base/bloc/base_bloc.dart';
|
||||
@@ -25,22 +26,31 @@ class _InterFamilyScreenState extends State<InterFamilyScreen> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
interFamilyBloc = BlocProvider.of(context);
|
||||
// fetch initial groups for the default selected tab
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
interFamilyBloc.getAllGroup(
|
||||
context,
|
||||
_selectedIndex == 0
|
||||
? ApplicationConstants.OWNER_GROUP
|
||||
: ApplicationConstants.PARTICIPANT_GROUP);
|
||||
});
|
||||
}
|
||||
|
||||
final _widgetOptions = <Widget>[
|
||||
BlocProvider(
|
||||
blocBuilder: () => InterFamilyBloc(),
|
||||
child: const GroupsScreen(
|
||||
role: ApplicationConstants.OWNER_GROUP,
|
||||
),
|
||||
),
|
||||
BlocProvider(
|
||||
blocBuilder: () => InterFamilyBloc(),
|
||||
child: const GroupsScreen(
|
||||
role: ApplicationConstants.PARTICIPANT_GROUP,
|
||||
),
|
||||
),
|
||||
];
|
||||
List<Group> ownerGroups = [];
|
||||
List<Group> participantGroups = [];
|
||||
|
||||
List<Widget> get _widgetOptions => [
|
||||
GroupsScreen(
|
||||
role: ApplicationConstants.OWNER_GROUP,
|
||||
groups: ownerGroups,
|
||||
interFamilyBloc: interFamilyBloc,
|
||||
),
|
||||
GroupsScreen(
|
||||
role: ApplicationConstants.PARTICIPANT_GROUP,
|
||||
groups: participantGroups,
|
||||
interFamilyBloc: interFamilyBloc,
|
||||
),
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -49,96 +59,109 @@ class _InterFamilyScreenState extends State<InterFamilyScreen> {
|
||||
stream: interFamilyBloc.streamSelectedScreen,
|
||||
initialData: _selectedIndex,
|
||||
builder: (context, selectSnapshot) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
actions: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
if (selectSnapshot.data == 0) {
|
||||
createOrJoinGroupDialog(
|
||||
context,
|
||||
interFamilyBloc,
|
||||
selectSnapshot.data! == 0
|
||||
? ApplicationConstants.OWNER_GROUP
|
||||
: ApplicationConstants.PARTICIPANT_GROUP,
|
||||
appLocalization(context).add_new_group,
|
||||
appLocalization(context).group_name_title,
|
||||
"",
|
||||
false,
|
||||
"",
|
||||
"",
|
||||
"");
|
||||
} else {
|
||||
createOrJoinGroupDialog(
|
||||
context,
|
||||
interFamilyBloc,
|
||||
selectSnapshot.data! == 0
|
||||
? ApplicationConstants.OWNER_GROUP
|
||||
: ApplicationConstants.PARTICIPANT_GROUP,
|
||||
appLocalization(context).join_group,
|
||||
appLocalization(context).group_id_title,
|
||||
'',
|
||||
true,
|
||||
"",
|
||||
appLocalization(context).group_name_title,
|
||||
"");
|
||||
}
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: const CircleBorder(),
|
||||
// subscribe to groups stream and update local lists so child widgets render instantly
|
||||
return StreamBuilder<List<Group>>(
|
||||
stream: interFamilyBloc.streamCurrentGroups,
|
||||
builder: (context, groupsSnapshot) {
|
||||
if (groupsSnapshot.hasData) {
|
||||
final all = groupsSnapshot.data!;
|
||||
ownerGroups = all
|
||||
.where((g) => g.isOwner == true && g.visibility == 'PUBLIC')
|
||||
.toList();
|
||||
participantGroups = all
|
||||
.where((g) => g.isOwner == null && g.visibility == 'PUBLIC')
|
||||
.toList();
|
||||
}
|
||||
// build UI below
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
actions: [
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
if (selectSnapshot.data == 0) {
|
||||
createOrJoinGroupDialog(
|
||||
context,
|
||||
interFamilyBloc,
|
||||
ApplicationConstants.OWNER_GROUP,
|
||||
appLocalization(context).add_new_group,
|
||||
appLocalization(context).group_name_title,
|
||||
"",
|
||||
false,
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
);
|
||||
} else {
|
||||
createOrJoinGroupDialog(
|
||||
context,
|
||||
interFamilyBloc,
|
||||
ApplicationConstants.PARTICIPANT_GROUP,
|
||||
appLocalization(context).join_group,
|
||||
appLocalization(context).group_id_title,
|
||||
'',
|
||||
true,
|
||||
"",
|
||||
appLocalization(context).group_name_title,
|
||||
"",
|
||||
);
|
||||
}
|
||||
},
|
||||
style:
|
||||
ElevatedButton.styleFrom(shape: const CircleBorder()),
|
||||
child: IconConstants.instance.getMaterialIcon(Icons.add),
|
||||
),
|
||||
],
|
||||
leading: Builder(
|
||||
builder: (context) {
|
||||
return IconButton(
|
||||
onPressed: () {
|
||||
Scaffold.of(context).openDrawer();
|
||||
},
|
||||
icon: const Icon(Icons.menu),
|
||||
);
|
||||
},
|
||||
),
|
||||
title: StreamBuilder<String>(
|
||||
stream: interFamilyBloc.streamTitleScreen,
|
||||
initialData: title,
|
||||
builder: (context, titleSnapshot) {
|
||||
return Center(
|
||||
child: Text(titleSnapshot.data ?? title),
|
||||
);
|
||||
},
|
||||
),
|
||||
child: IconConstants.instance.getMaterialIcon(Icons.add),
|
||||
),
|
||||
],
|
||||
leading: Builder(
|
||||
builder: (context) {
|
||||
return IconButton(
|
||||
onPressed: () {
|
||||
Scaffold.of(context).openDrawer();
|
||||
},
|
||||
icon: const Icon(Icons.menu),
|
||||
);
|
||||
},
|
||||
),
|
||||
title: StreamBuilder<String>(
|
||||
stream: interFamilyBloc.streamTitleScreen,
|
||||
initialData: title,
|
||||
builder: (context, titleSnapshot) {
|
||||
return Center(
|
||||
child: Text(titleSnapshot.data ?? title),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
drawer: Drawer(
|
||||
width: context.dynamicWidth(0.6),
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(appLocalization(context).my_group_title),
|
||||
selected: _selectedIndex == 0,
|
||||
onTap: () {
|
||||
_onItemTapped(0);
|
||||
title = appLocalization(context).my_group_title;
|
||||
interFamilyBloc.sinkTitleScreen.add(title);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
drawer: Drawer(
|
||||
width: context.dynamicWidth(0.6),
|
||||
child: ListView(
|
||||
padding: EdgeInsets.zero,
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(appLocalization(context).my_group_title),
|
||||
selected: _selectedIndex == 0,
|
||||
onTap: () {
|
||||
_onItemTapped(0);
|
||||
title = appLocalization(context).my_group_title;
|
||||
interFamilyBloc.sinkTitleScreen.add(title);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: Text(appLocalization(context).invite_group),
|
||||
selected: _selectedIndex == 1,
|
||||
onTap: () {
|
||||
_onItemTapped(1);
|
||||
title = appLocalization(context).invite_group;
|
||||
interFamilyBloc.sinkTitleScreen.add(title);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
ListTile(
|
||||
title: Text(appLocalization(context).invite_group),
|
||||
selected: _selectedIndex == 1,
|
||||
onTap: () {
|
||||
_onItemTapped(1);
|
||||
title = appLocalization(context).invite_group;
|
||||
interFamilyBloc.sinkTitleScreen.add(title);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: _widgetOptions[selectSnapshot.data ?? _selectedIndex],
|
||||
),
|
||||
body: _widgetOptions[selectSnapshot.data ?? _selectedIndex],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -159,5 +182,11 @@ class _InterFamilyScreenState extends State<InterFamilyScreen> {
|
||||
interFamilyBloc.sinkSelectedScreen.add(_selectedIndex);
|
||||
isLoading = false;
|
||||
interFamilyBloc.sinkIsLoading.add(isLoading);
|
||||
// fetch groups for the selected tab immediately
|
||||
interFamilyBloc.getAllGroup(
|
||||
context,
|
||||
_selectedIndex == 0
|
||||
? ApplicationConstants.OWNER_GROUP
|
||||
: ApplicationConstants.PARTICIPANT_GROUP);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user