From bce1c9cc06f867b5e452f23e1a9ab88d974a6745 Mon Sep 17 00:00:00 2001 From: nailsonseat Date: Sun, 18 Feb 2024 00:36:21 +0530 Subject: [PATCH] Refactor code to improve performance and readability --- frontend/lib/screens/admin/add_menu.dart | 381 ++++++++++++----------- 1 file changed, 195 insertions(+), 186 deletions(-) diff --git a/frontend/lib/screens/admin/add_menu.dart b/frontend/lib/screens/admin/add_menu.dart index ef40f10..5333dd7 100644 --- a/frontend/lib/screens/admin/add_menu.dart +++ b/frontend/lib/screens/admin/add_menu.dart @@ -17,7 +17,6 @@ class AddMessMenu extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { - WidgetsBinding.instance.addPostFrameCallback((_) { ref.watch(menuProvider.notifier).clearControllers(); if (ref.read(authProvider.notifier).tokenCheckProgress != LoadingState.progress) { @@ -33,179 +32,189 @@ class AddMessMenu extends ConsumerWidget { ), body: SingleChildScrollView( controller: _scrollController, - child: Column( - children: [ - Container( - alignment: Alignment.topLeft, - padding: const EdgeInsets.only(left: 30), - child: const Text( - "Spreadsheet Entry", - style: TextStyle(fontSize: 32, fontFamily: "RobotoFlex"), - ), - ), - const SizedBox(height: 30), - Padding( - padding: const EdgeInsets.only(left: 30), - child: Row( - mainAxisAlignment: MainAxisAlignment.start, + child: Consumer( + builder: (_, ref, __) { + if (ref.watch(menuProvider).loadingState == LoadingState.progress) { + return const Center( + child: CircularProgressIndicator(), + ); + } else { + return Column( children: [ - const Text( - "Upload file here", - style: TextStyle(fontSize: 16), + Container( + alignment: Alignment.topLeft, + padding: const EdgeInsets.only(left: 30), + child: const Text( + "Spreadsheet Entry", + style: TextStyle(fontSize: 32, fontFamily: "RobotoFlex"), + ), ), - const SizedBox(width: 30), - ElevatedButton( - onPressed: () => ref.read(menuProvider.notifier).pickSpreadsheet(), - style: ButtonStyle(minimumSize: MaterialStateProperty.all(const Size(200, 60))), - child: const Text("Upload Spreadsheet"), + const SizedBox(height: 30), + Padding( + padding: const EdgeInsets.only(left: 30), + child: Row( + mainAxisAlignment: MainAxisAlignment.start, + children: [ + const Text( + "Upload file here", + style: TextStyle(fontSize: 16), + ), + const SizedBox(width: 30), + ElevatedButton( + onPressed: () => ref.read(menuProvider.notifier).pickSpreadsheet(), + style: ButtonStyle(minimumSize: MaterialStateProperty.all(const Size(200, 60))), + child: const Text("Upload Spreadsheet"), + ), + ], + ), ), - ], - ), - ), - const SizedBox(height: 30), - const TextDivider(text: "OR"), - const SizedBox(height: 30), - Container( - alignment: Alignment.topLeft, - padding: const EdgeInsets.only(left: 30), - child: const Text( - "Single Entry", - style: TextStyle(fontSize: 32, fontFamily: "RobotoFlex"), - ), - ), - const SizedBox(height: 30), - Container( - margin: const EdgeInsets.symmetric(horizontal: 15), - child: Form( - key: _formKey, - child: MaterialTextFormField( - controller: ref.read(menuProvider).kitchenNameController, - validator: (value) => Validators.nameValidator(value), - hintText: "Enter kitchen name", - onSubmitted: (value) => _scrollController.animateTo( - _scrollController.position.maxScrollExtent, - duration: const Duration(milliseconds: 500), - curve: Curves.ease, + const SizedBox(height: 30), + const TextDivider(text: "OR"), + const SizedBox(height: 30), + Container( + alignment: Alignment.topLeft, + padding: const EdgeInsets.only(left: 30), + child: const Text( + "Single Entry", + style: TextStyle(fontSize: 32, fontFamily: "RobotoFlex"), + ), ), - hintColor: Colors.teal.shade900.withOpacity(0.5), - ), - ), - ), - const SizedBox(height: 30), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 15), - child: SlideSwitcher( - onSelect: (index) => ref.read(menuProvider.notifier).selectMealType(index), - containerHeight: 65, - containerWight: 380, - containerBorderRadius: 20, - slidersColors: [Colors.tealAccent.shade700.withOpacity(0.7)], - containerColor: Colors.tealAccent.shade100, - children: MessMenuConstants.mealTypes, - ), - ), - const SizedBox(height: 30), - Consumer( - builder: (_, ref, ___) { - return Padding( - padding: const EdgeInsets.symmetric(horizontal: 15), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - SlideSwitcher( - onSelect: (index) => ref.read(menuProvider.notifier).selectWeekday(index), - containerHeight: 550, - containerWight: 70, - containerBorderRadius: 20, - direction: Axis.vertical, - slidersColors: [Colors.tealAccent.shade700.withOpacity(0.7)], - containerColor: Colors.tealAccent.shade100, - children: MessMenuConstants.weekdays), - Container( - height: 550, - width: 285, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(20), - border: Border.all(color: Colors.transparent, width: 0), - color: Colors.grey[200], + const SizedBox(height: 30), + Container( + margin: const EdgeInsets.symmetric(horizontal: 15), + child: Form( + key: _formKey, + child: MaterialTextFormField( + controller: ref.read(menuProvider).kitchenNameController, + validator: (value) => Validators.nameValidator(value), + hintText: "Enter kitchen name", + onSubmitted: (value) => _scrollController.animateTo( + _scrollController.position.maxScrollExtent, + duration: const Duration(milliseconds: 500), + curve: Curves.ease, ), - child: Column( + hintColor: Colors.teal.shade900.withOpacity(0.5), + ), + ), + ), + const SizedBox(height: 30), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 15), + child: SlideSwitcher( + onSelect: (index) => ref.read(menuProvider.notifier).selectMealType(index), + containerHeight: 65, + containerWight: 380, + containerBorderRadius: 20, + slidersColors: [Colors.tealAccent.shade700.withOpacity(0.7)], + containerColor: Colors.tealAccent.shade100, + children: MessMenuConstants.mealTypes, + ), + ), + const SizedBox(height: 30), + Consumer( + builder: (_, ref, ___) { + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 15), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Consumer( - builder: (_, ref, __) { - final weekDayIndex = ref.watch(menuProvider).selectedWeekdayIndex; - final mealTypeIndex = ref.watch(menuProvider).selectedMealTypeIndex; - - final weekDay = ref.watch(menuProvider.notifier).getWeekDay(weekDayIndex); - final mealType = ref.watch(menuProvider.notifier).getMealType(mealTypeIndex); - return Container( - padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15), - alignment: Alignment.topLeft, - child: Text( - "$weekDay's $mealType", - style: const TextStyle(fontSize: 23, fontFamily: "GoogleSanaFlex"), - ), - ); - }, - ), - const SizedBox(height: 10), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 20), - child: MaterialTextFormField( - controller: ref.read(menuProvider).itemNameController, - onSubmitted: (value) => ref.read(menuProvider.notifier).addMenuItem(), - validator: (value) => Validators.nameValidator(value), - hintText: 'Enter Menu Item', - hintColor: Colors.teal.shade900.withOpacity(0.5), - ), - ), - const SizedBox(height: 30), + SlideSwitcher( + onSelect: (index) => ref.read(menuProvider.notifier).selectWeekday(index), + containerHeight: 550, + containerWight: 70, + containerBorderRadius: 20, + direction: Axis.vertical, + slidersColors: [Colors.tealAccent.shade700.withOpacity(0.7)], + containerColor: Colors.tealAccent.shade100, + children: MessMenuConstants.weekdays), Container( - width: 250, - height: 360, + height: 550, + width: 285, decoration: BoxDecoration( borderRadius: BorderRadius.circular(20), border: Border.all(color: Colors.transparent, width: 0), - color: Colors.white, + color: Colors.grey[200], ), child: Column( children: [ - Container( - padding: const EdgeInsets.only(left: 20, top: 15), - alignment: Alignment.topLeft, - child: const Text( - "Menu Item", - style: TextStyle(fontSize: 18, fontFamily: "GoogleSanaFlex"), + Consumer( + builder: (_, ref, __) { + final weekDayIndex = ref.watch(menuProvider).selectedWeekdayIndex; + final mealTypeIndex = ref.watch(menuProvider).selectedMealTypeIndex; + + final weekDay = ref.watch(menuProvider.notifier).getWeekDay(weekDayIndex); + final mealType = ref.watch(menuProvider.notifier).getMealType(mealTypeIndex); + return Container( + padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15), + alignment: Alignment.topLeft, + child: Text( + "$weekDay's $mealType", + style: const TextStyle(fontSize: 23, fontFamily: "GoogleSanaFlex"), + ), + ); + }, + ), + const SizedBox(height: 10), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 20), + child: MaterialTextFormField( + controller: ref.read(menuProvider).itemNameController, + onSubmitted: (value) => ref.read(menuProvider.notifier).addMenuItem(), + validator: (value) => Validators.nameValidator(value), + hintText: 'Enter Menu Item', + hintColor: Colors.teal.shade900.withOpacity(0.5), ), ), - const Divider(height: 20), - Expanded( - child: Consumer( - builder: (_, ref, ___) { - final menuState = ref.watch(menuProvider); - final weekDay = ref - .watch(menuProvider.notifier) - .getWeekDay(menuState.selectedWeekdayIndex); - final mealType = ref - .watch(menuProvider.notifier) - .getMealType(menuState.selectedMealTypeIndex); - final currentMenu = ref.watch(menuProvider).currentMenu; - return ListView.builder( - itemCount: currentMenu.messMenu![weekDay]![mealType]!.length, - itemBuilder: (context, index) { - return Container( - padding: const EdgeInsets.symmetric(horizontal: 20), - alignment: Alignment.centerLeft, - child: RoundedChip( - label: currentMenu.messMenu![weekDay]![mealType]![index], - color: Colors.tealAccent.shade100, - onDeleted: () => - ref.watch(menuProvider.notifier).removeMenuItem(index), - ), - ); - }, - ); - }, + const SizedBox(height: 30), + Container( + width: 250, + height: 360, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(20), + border: Border.all(color: Colors.transparent, width: 0), + color: Colors.white, + ), + child: Column( + children: [ + Container( + padding: const EdgeInsets.only(left: 20, top: 15), + alignment: Alignment.topLeft, + child: const Text( + "Menu Item", + style: TextStyle(fontSize: 18, fontFamily: "GoogleSanaFlex"), + ), + ), + const Divider(height: 20), + Expanded( + child: Consumer( + builder: (_, ref, ___) { + final menuState = ref.watch(menuProvider); + final weekDay = ref + .watch(menuProvider.notifier) + .getWeekDay(menuState.selectedWeekdayIndex); + final mealType = ref + .watch(menuProvider.notifier) + .getMealType(menuState.selectedMealTypeIndex); + final currentMenu = ref.watch(menuProvider).currentMenu; + return ListView.builder( + itemCount: currentMenu.messMenu![weekDay]![mealType]!.length, + itemBuilder: (context, index) { + return Container( + padding: const EdgeInsets.symmetric(horizontal: 20), + alignment: Alignment.centerLeft, + child: RoundedChip( + label: currentMenu.messMenu![weekDay]![mealType]![index], + color: Colors.tealAccent.shade100, + onDeleted: () => + ref.watch(menuProvider.notifier).removeMenuItem(index), + ), + ); + }, + ); + }, + ), + ), + ], ), ), ], @@ -213,35 +222,35 @@ class AddMessMenu extends ConsumerWidget { ), ], ), + ); + }, + ), + const SizedBox(height: 30), + Container( + margin: const EdgeInsets.symmetric(horizontal: 20), + child: Align( + alignment: Alignment.centerRight, + child: ElevatedButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + ref.read(menuProvider.notifier).addMenu(); + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text("Menu added successfully"), + ), + ); + } + }, + style: ButtonStyle(minimumSize: MaterialStateProperty.all(const Size(200, 60))), + child: const Text("Add Menu"), ), - ], + ), ), - ); - }, - ), - const SizedBox(height: 30), - Container( - margin: const EdgeInsets.symmetric(horizontal: 20), - child: Align( - alignment: Alignment.centerRight, - child: ElevatedButton( - onPressed: () { - if (_formKey.currentState!.validate()) { - ref.read(menuProvider.notifier).addMenu(); - ScaffoldMessenger.of(context).showSnackBar( - const SnackBar( - content: Text("Menu added successfully"), - ), - ); - } - }, - style: ButtonStyle(minimumSize: MaterialStateProperty.all(const Size(200, 60))), - child: const Text("Add Menu"), - ), - ), - ), - const SizedBox(height: 30), - ], + const SizedBox(height: 30), + ], + ); + } + }, ), ), ),