Advertisement
harmonyV

Updated Example

Jul 25th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.19 KB | None | 0 0
  1. class HabitCreationPage extends StatefulWidget {
  2.   const HabitCreationPage({super.key});
  3.  
  4.   @override
  5.   State<HabitCreationPage> createState() => _HabitCreationPageState();
  6. }
  7.  
  8. class _HabitCreationPageState extends State<HabitCreationPage> {
  9.   final TextEditingController titleTextController = TextEditingController();
  10.   final TextEditingController detailsTextController = TextEditingController();
  11.  
  12.   void habitCreationFlow() async {
  13.     EditHabitProvider editHabitProvider =
  14.         Provider.of<EditHabitProvider>(context, listen: false);
  15.     HabitCategoryProvider habitCategoryProvider =
  16.         Provider.of<HabitCategoryProvider>(context, listen: false);
  17.     HabitDatabase habitDatabase =
  18.         Provider.of<HabitDatabase>(context, listen: false);
  19.  
  20.     List<int> newitemDataIntKeys =
  21.         habitCategoryProvider.selectedItemslist.toList();
  22.  
  23.     debugPrint("LIST WITH LABELS: ${newitemDataIntKeys.length}");
  24.  
  25.     if (titleTextController.text == "") {
  26.       // tell user to provide a valid title
  27.       formkey.currentState!.validate();
  28.       await showDialog(
  29.         context: context,
  30.         builder: (context) => AlertDialog(
  31.           title: const Text("TITLE CAN'T BE EMPTY"),
  32.           actions: [
  33.             ElevatedButton(
  34.                 onPressed: () {
  35.                   Navigator.pop(context);
  36.                 },
  37.                 child: const Text("close"))
  38.           ],
  39.         ),
  40.       );
  41.       debugPrint("TITLE CAN'T BE EMPTY");
  42.  
  43.       return;
  44.     }
  45.  
  46.     // create a new habit or edit existing habit
  47.     if (editHabitProvider.isEditMode) {
  48.       habitDatabase.editHabit(
  49.         editHabitProvider.habitToEdit!.id,
  50.         titleTextController.text,
  51.         detailsTextController.text,
  52.         newitemDataIntKeys,
  53.         habitCategoryProvider.habitCategoryData.category,
  54.       );
  55.     } else if (!editHabitProvider.isEditMode) {
  56.       habitDatabase.addHabit(
  57.         titleTextController.text,
  58.         detailsTextController.text,
  59.         newitemDataIntKeys,
  60.         habitCategoryProvider.habitCategoryData.category,
  61.       );
  62.     }
  63.  
  64.     prepToClosePage();
  65.   }
  66.  
  67.   void prepToClosePage() {
  68.     if (Provider.of<EditHabitProvider>(context, listen: false).isEditMode) {
  69.       Provider.of<EditHabitProvider>(context, listen: false).endEditingMode();
  70.     }
  71.  
  72.     Provider.of<HabitCategoryProvider>(context, listen: false)
  73.         .clearSelectedItemsList();
  74.  
  75.     titleTextController.clear();
  76.     detailsTextController.clear();
  77.  
  78.     Navigator.pop(context);
  79.   }
  80.  
  81.   @override
  82.   void dispose() {
  83.     titleTextController.dispose();
  84.     detailsTextController.dispose();
  85.     super.dispose();
  86.   }
  87.  
  88.   @override
  89.   Widget build(BuildContext context) {
  90.     TextTheme textTheme = Theme.of(context).textTheme;
  91.     // ignore: unused_local_variable
  92.     ColorScheme colorScheme = Theme.of(context).colorScheme;
  93.  
  94.     HabitCategoryProvider habitCategoryProvider =
  95.         Provider.of<HabitCategoryProvider>(context);
  96.  
  97.     debugPrint("VIEW PADDING: ${MediaQuery.viewPaddingOf(context)}");
  98.  
  99.     return Scaffold(
  100.       backgroundColor: habitCategoryProvider.habitCategoryData.color,
  101.       appBar: AppBar(
  102.         backgroundColor: habitCategoryProvider.habitCategoryData.color,
  103.         leading: IconButton(
  104.           onPressed: prepToClosePage,
  105.           icon: const Icon(Icons.close_rounded),
  106.         ),
  107.         centerTitle: true,
  108.         title: Text(
  109.           Provider.of<EditHabitProvider>(context).isEditMode
  110.               ? "Edit Habit"
  111.               : "New Habit",
  112.           style: textTheme.titleLarge,
  113.         ),
  114.         actions: [
  115.           TextButton(
  116.             onPressed: habitCreationFlow,
  117.             child: Text(
  118.               "Save",
  119.               style: textTheme.bodyLarge,
  120.             ),
  121.           ),
  122.         ],
  123.       ),
  124.       body: SingleChildScrollView(
  125.         child: Column(
  126.           mainAxisAlignment: MainAxisAlignment.center,
  127.           children: [
  128.             Padding(
  129.               padding: const EdgeInsets.symmetric(horizontal: 16.0),
  130.               child: Column(
  131.                 crossAxisAlignment: CrossAxisAlignment.center,
  132.                 children: [
  133.                   const SizedBox(
  134.                     height: 16,
  135.                   ),
  136.                   const CarouselWidget(modifier: 0.22),
  137.                   const SizedBox(
  138.                     height: 24,
  139.                   ),
  140.                   MyTextFieldSection(
  141.                     title: "Title",
  142.                     limitation: true,
  143.                     textEditingController: titleTextController,
  144.                     maxLines: 1,
  145.                     maxLength: 40,
  146.                   ),
  147.                   const SizedBox(
  148.                     height: 16,
  149.                   ),
  150.                   MyTextFieldSection(
  151.                     title: "More Details",
  152.                     limitation: false,
  153.                     textEditingController: detailsTextController,
  154.                     maxLines: 4,
  155.                     maxLength: 300,
  156.                   ),
  157.                   const SizedBox(
  158.                     height: 24,
  159.                   ),
  160.                   Column(
  161.                     crossAxisAlignment: CrossAxisAlignment.start,
  162.                     mainAxisSize: MainAxisSize.max,
  163.                     children: [
  164.                       Row(
  165.                         mainAxisAlignment: MainAxisAlignment.spaceBetween,
  166.                         mainAxisSize: MainAxisSize.max,
  167.                         children: [
  168.                           Text(
  169.                             "Labels",
  170.                             style: textTheme.bodyLarge,
  171.                           ),
  172.                           Text(
  173.                             "${habitCategoryProvider.selectedItemslist.length}/${habitCategoryProvider.maxSelectableChips}",
  174.                             style: textTheme.bodyLarge,
  175.                           )
  176.                         ],
  177.                       ),
  178.                       const SizedBox(
  179.                         height: 8,
  180.                       ),
  181.                       const Center(child: LabelSectionContainerWidget())
  182.                     ],
  183.                   ),
  184.                 ],
  185.               ),
  186.             ),
  187.           ],
  188.         ),
  189.       ),
  190.     );
  191.   }
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement