Refactor HomeCubit class for better instance management

Create a private static instance variable and refactor methods for better instance management in the HomeCubit class.
This commit is contained in:
Mohammad Salameh
2024-04-01 12:09:01 +03:00
parent a20dfa3709
commit d1bc973b38
10 changed files with 169 additions and 145 deletions

View File

@ -23,13 +23,13 @@ class AppLayout extends StatelessWidget {
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => HomeCubit(),
create: (context) => HomeCubit.getInstance(),
),
BlocProvider(
create: (context) => DevicesCubit(),
),
],
child: BlocListener<HomeCubit, HomeState>(
child: BlocConsumer<HomeCubit, HomeState>(
listener: (context, state) {
if (state is GetSpacesError) {
ScaffoldMessenger.of(context).showSnackBar(
@ -41,42 +41,41 @@ class AppLayout extends StatelessWidget {
.popUntil((route) => route.settings.name == Routes.authLogin);
}
},
child: BlocBuilder<HomeCubit, HomeState>(
builder: (context, state) {
return AnnotatedRegion(
value: SystemUiOverlayStyle(
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
statusBarIconBrightness: Brightness.light,
builder: (context, state) {
return AnnotatedRegion(
value: SystemUiOverlayStyle(
statusBarColor: ColorsManager.primaryColor.withOpacity(0.5),
statusBarIconBrightness: Brightness.light,
),
child: SafeArea(
child: BlocBuilder<HomeCubit, HomeState>(
builder: (context, state) {
return Scaffold(
backgroundColor: ColorsManager.backgroundColor,
extendBodyBehindAppBar: true,
extendBody: true,
appBar:
state is GetSpacesLoaded ? const DefaultAppBar() : null,
body: const AppBody(),
bottomNavigationBar: const DefaultNavBar(),
// floatingActionButton: FloatingActionButton(
// onPressed: () {
// Navigator.push(
// context,
// CustomPageRoute(
// builder: (context) =>
// const ThreeGangSwitchesView(),
// ),
// );
// },
// child: const Icon(Icons.arrow_forward_ios_sharp),
// ),
);
},
),
child: SafeArea(
child: BlocBuilder<HomeCubit, HomeState>(
builder: (context, state) {
return const Scaffold(
backgroundColor: ColorsManager.backgroundColor,
extendBodyBehindAppBar: true,
extendBody: true,
appBar: DefaultAppBar(),
body: AppBody(),
bottomNavigationBar: DefaultNavBar(),
// floatingActionButton: FloatingActionButton(
// onPressed: () {
// Navigator.push(
// context,
// CustomPageRoute(
// builder: (context) =>
// const ThreeGangSwitchesView(),
// ),
// );
// },
// child: const Icon(Icons.arrow_forward_ios_sharp),
// ),
);
},
),
),
);
},
),
),
);
},
),
);
}