Advertisement
harmonyV

CreationPage

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