Advertisement
harmonyV

Example of comments

Jul 25th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 7.05 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.     // initialize all providers needed
  14.     EditHabitProvider editHabitProvider =
  15.         Provider.of<EditHabitProvider>(context, listen: false);
  16.     HabitCategoryProvider habitCategoryProvider =
  17.         Provider.of<HabitCategoryProvider>(context, listen: false);
  18.     HabitDatabase habitDatabase =
  19.         Provider.of<HabitDatabase>(context, listen: false);
  20.  
  21.     //  create an empty list for the key strings
  22.     List<int> newitemDataIntKeys = [];
  23.  
  24.     // populate the list by adding the content of the selectedItemsList
  25.     newitemDataIntKeys.addAll(
  26.       habitCategoryProvider.selectedItemslist,
  27.     );
  28.     debugPrint("LIST WITH LABELS: ${newitemDataIntKeys.length}");
  29.  
  30.     if (titleTextController.text == "") {
  31.       // tell user to provide a valid title
  32.       formkey.currentState!.validate();
  33.       await showDialog(
  34.         context: context,
  35.         builder: (context) => AlertDialog(
  36.           title: const Text("TITLE CAN'T BE EMPTY"),
  37.           actions: [
  38.             ElevatedButton(
  39.                 onPressed: () {
  40.                   Navigator.pop(context);
  41.                 },
  42.                 child: const Text("close"))
  43.           ],
  44.         ),
  45.       );
  46.       debugPrint("TITLE CAN'T BE EMPTY");
  47.  
  48.       // don`t procede
  49.       return;
  50.     }
  51.  
  52.     // create a new habit or edit existing habit
  53.     // provide the title, habitcCategory, and the list of key strings
  54.     editHabitProvider.editingMode
  55.         ? habitDatabase.editHabit(
  56.             editHabitProvider.habitToEdit!.id,
  57.             titleTextController.text,
  58.             detailsTextController.text,
  59.             newitemDataIntKeys,
  60.             habitCategoryProvider.habitCategoryData.category,
  61.           )
  62.         : habitDatabase.addHabit(
  63.             titleTextController.text,
  64.             detailsTextController.text,
  65.             newitemDataIntKeys,
  66.             habitCategoryProvider.habitCategoryData.category,
  67.           );
  68.  
  69.    
  70.     prepToClosePage();
  71.   }
  72.  
  73.   void prepToClosePage() {
  74.     // if editingMode active, end it
  75.     Provider.of<EditHabitProvider>(context, listen: false).editingMode
  76.         ? Provider.of<EditHabitProvider>(context, listen: false)
  77.             .endEditingMode()
  78.         : null;
  79.  
  80.     // clear the selectedItemsList
  81.     Provider.of<HabitCategoryProvider>(context, listen: false)
  82.         .clearSelectedItemsList();
  83.  
  84.     // clear the TextEdetingControllers
  85.     titleTextController.clear();
  86.     detailsTextController.clear();
  87.  
  88.     // return to HomePage
  89.     Navigator.pop(context);
  90.   }
  91.  
  92.   @override
  93.   void dispose() {
  94.     titleTextController.dispose();
  95.     detailsTextController.dispose();
  96.     super.dispose();
  97.   }
  98.  
  99.   @override
  100.   Widget build(BuildContext context) {
  101.     TextTheme textTheme = Theme.of(context).textTheme;
  102.     // ignore: unused_local_variable
  103.     ColorScheme colorScheme = Theme.of(context).colorScheme;
  104.  
  105.     HabitCategoryProvider habitCategoryProvider =
  106.         Provider.of<HabitCategoryProvider>(context);
  107.  
  108.     debugPrint("VIEW PADDING: ${MediaQuery.viewPaddingOf(context)}");
  109.  
  110.     return Scaffold(
  111.       backgroundColor: habitCategoryProvider.habitCategoryData.color,
  112.       appBar: AppBar(
  113.         backgroundColor: habitCategoryProvider.habitCategoryData.color,
  114.         leading:
  115.             // button to close the page and return to HomePage
  116.             IconButton(
  117.           onPressed: prepToClosePage,
  118.           icon: const Icon(Icons.close_rounded),
  119.         ),
  120.         centerTitle: true,
  121.         title: Text(
  122.           Provider.of<EditHabitProvider>(context).editingMode
  123.               ? "Edit Habit"
  124.               : "New Habit",
  125.           style: textTheme.titleLarge,
  126.         ),
  127.         actions: [
  128.           // button to create a new habit with provided variables
  129.           TextButton(
  130.             onPressed: habitCreationFlow,
  131.             child: Text(
  132.               "Save",
  133.               style: textTheme.bodyLarge,
  134.             ),
  135.           ),
  136.         ],
  137.       ),
  138.       body: SingleChildScrollView(
  139.         child: Column(
  140.           mainAxisAlignment: MainAxisAlignment.center,
  141.           children: [
  142.             // top section of the page, with save and close.
  143.  
  144.             // main body of the page
  145.             Padding(
  146.               padding: const EdgeInsets.symmetric(horizontal: 16.0),
  147.               child: Column(
  148.                 crossAxisAlignment: CrossAxisAlignment.center,
  149.                 children: [
  150.                   const SizedBox(
  151.                     height: 16,
  152.                   ),
  153.  
  154.                   // section showing selectable Category cards
  155.                   const CarouselWidget(modifier: 0.22),
  156.                   const SizedBox(
  157.                     height: 24,
  158.                   ),
  159.  
  160.                   // habit titel section
  161.                   MyTextFieldSection(
  162.                     title: "Title",
  163.                     limitation: true,
  164.                     textEditingController: titleTextController,
  165.                     maxLines: 1,
  166.                     maxLength: 40,
  167.                   ),
  168.                   const SizedBox(
  169.                     height: 16,
  170.                   ),
  171.  
  172.                   // more details section
  173.                   MyTextFieldSection(
  174.                     title: "More Details",
  175.                     limitation: false,
  176.                     textEditingController: detailsTextController,
  177.                     maxLines: 4,
  178.                     maxLength: 300,
  179.                   ),
  180.                   const SizedBox(
  181.                     height: 24,
  182.                   ),
  183.  
  184.                   // label section
  185.                   Column(
  186.                     crossAxisAlignment: CrossAxisAlignment.start,
  187.                     mainAxisSize: MainAxisSize.max,
  188.                     children: [
  189.                       Row(
  190.                         mainAxisAlignment: MainAxisAlignment.spaceBetween,
  191.                         mainAxisSize: MainAxisSize.max,
  192.                         children: [
  193.                           Text(
  194.                             "Labels",
  195.                             style: textTheme.bodyLarge,
  196.                           ),
  197.                           Text(
  198.                             "${habitCategoryProvider.selectedItemslist.length}/${habitCategoryProvider.maxSelectableChips}",
  199.                             style: textTheme.bodyLarge,
  200.                           )
  201.                         ],
  202.                       ),
  203.                       const SizedBox(
  204.                         height: 8,
  205.                       ),
  206.                       const Center(child: LabelSectionContainerWidget())
  207.                     ],
  208.                   ),
  209.                 ],
  210.               ),
  211.             ),
  212.           ],
  213.         ),
  214.       ),
  215.     );
  216.   }
  217. }
  218.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement