Advertisement
harmonyV

Updated Example 2

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