Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:habit_tracker/components/carousel/carousel_slider/carousel_widget.dart';
- import 'package:habit_tracker/components/my_text_field_section.dart';
- import 'package:habit_tracker/database/habit_database.dart';
- import 'package:habit_tracker/services/providers/edit_habit_provider.dart';
- import 'package:habit_tracker/services/providers/habit_category_provider.dart';
- import 'package:habit_tracker/components/widgets/label_section_container_widget.dart';
- import 'package:provider/provider.dart';
- class HabitCreationPage extends StatefulWidget {
- const HabitCreationPage({super.key});
- @override
- State<HabitCreationPage> createState() => _HabitCreationPageState();
- }
- class _HabitCreationPageState extends State<HabitCreationPage> {
- TextEditingController titleTextController = TextEditingController();
- TextEditingController detailsTextController = TextEditingController();
- void habitCreationFlow() async {
- // initialize all providers needed
- EditHabitProvider editHabitProvider =
- Provider.of<EditHabitProvider>(context, listen: false);
- HabitCategoryProvider habitCategoryProvider =
- Provider.of<HabitCategoryProvider>(context, listen: false);
- HabitDatabase habitDatabase =
- Provider.of<HabitDatabase>(context, listen: false);
- // create an empty list for the key strings
- List<int> newitemDataIntKeys = [];
- // populate the list by adding to content of the selectedItemsList
- newitemDataIntKeys.addAll(
- habitCategoryProvider.selectedItemslist,
- );
- debugPrint("LIST WITH LABELS: ${newitemDataIntKeys.length}");
- if (titleTextController.text == "") {
- // tell user to provide a valid title
- formkey.currentState!.validate();
- await showDialog(
- context: context,
- builder: (context) => AlertDialog(
- title: const Text("TITLE CAN'T BE EMPTY"),
- actions: [
- ElevatedButton(
- onPressed: () {
- Navigator.pop(context);
- },
- child: const Text("close"))
- ],
- ),
- );
- debugPrint("TITLE CAN'T BE EMPTY");
- // don`t procede
- return;
- }
- // create a new habit or edit existing habit
- // provide the title, habitcCategory, and the list of key strings
- editHabitProvider.editingMode
- ? habitDatabase.editHabit(
- editHabitProvider.habitToEdit!.id,
- titleTextController.text,
- detailsTextController.text,
- newitemDataIntKeys,
- habitCategoryProvider.habitCategoryData.category,
- )
- : habitDatabase.addHabit(
- titleTextController.text,
- detailsTextController.text,
- newitemDataIntKeys,
- habitCategoryProvider.habitCategoryData.category,
- );
- // prep to close the page
- prepToClosePage();
- }
- void prepToClosePage() {
- // if editingMode active, end it
- Provider.of<EditHabitProvider>(context, listen: false).editingMode
- ? Provider.of<EditHabitProvider>(context, listen: false)
- .endEditingMode()
- : null;
- // clear the selectedItemsList
- Provider.of<HabitCategoryProvider>(context, listen: false)
- .clearSelectedItemsList();
- // clear the TextEdetingControllers
- titleTextController.clear();
- detailsTextController.clear();
- // return to HomePage
- Navigator.pop(context);
- }
- @override
- Widget build(BuildContext context) {
- TextTheme textTheme = Theme.of(context).textTheme;
- ColorScheme colorScheme = Theme.of(context).colorScheme;
- return Scaffold(
- backgroundColor:
- Provider.of<HabitCategoryProvider>(context).habitCategoryData.color,
- body: Center(
- child: SingleChildScrollView(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- //mainAxisSize: MainAxisSize.max,
- children: [
- // top section of the page, with save and close.
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- mainAxisSize: MainAxisSize.max,
- children: [
- // button to close the page and return to HomePage
- IconButton(
- onPressed: prepToClosePage,
- icon: const Icon(Icons.close_rounded),
- ),
- // page title
- Text(
- Provider.of<EditHabitProvider>(context).editingMode
- ? "New Habit"
- : "Edit Habit",
- style: textTheme.headlineMedium!.copyWith(
- color: colorScheme.onSecondaryContainer,
- ),
- ),
- // button to create a new habit with provided variables
- TextButton(
- onPressed: habitCreationFlow,
- child: Text(
- "Save",
- style: textTheme.titleLarge,
- ),
- ),
- ],
- ),
- // main body of the page
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- const SizedBox(
- height: 20,
- ),
- // section showing selectable Category cards
- // TODO: create a more scalable way of creating the CategoryCards section
- const CarouselWidget(),
- const SizedBox(
- height: 20,
- ),
- // habit titel section
- MyTextFieldSection(
- title: "Title",
- limitation: true,
- textEditingController: titleTextController,
- maxLines: 1,
- maxLength: 40,
- ),
- const SizedBox(
- height: 16,
- ),
- // more details section
- MyTextFieldSection(
- title: "More Details",
- limitation: false,
- textEditingController: detailsTextController,
- maxLines: 4,
- maxLength: 300,
- ),
- const SizedBox(
- height: 16,
- ),
- // label section
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- mainAxisSize: MainAxisSize.max,
- children: [
- Text(
- "Labels",
- style: textTheme.bodyMedium!.copyWith(
- color: colorScheme.onSecondaryContainer,
- ),
- ),
- const SizedBox(
- height: 16,
- ),
- const Center(child: LabelSectionContainerWidget())
- ],
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement