Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HabitCreationPage extends StatefulWidget {
- const HabitCreationPage({super.key});
- @override
- State<HabitCreationPage> createState() => _HabitCreationPageState();
- }
- class _HabitCreationPageState extends State<HabitCreationPage> {
- final TextEditingController titleTextController = TextEditingController();
- final 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 the 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,
- );
- 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
- void dispose() {
- titleTextController.dispose();
- detailsTextController.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- TextTheme textTheme = Theme.of(context).textTheme;
- // ignore: unused_local_variable
- ColorScheme colorScheme = Theme.of(context).colorScheme;
- HabitCategoryProvider habitCategoryProvider =
- Provider.of<HabitCategoryProvider>(context);
- debugPrint("VIEW PADDING: ${MediaQuery.viewPaddingOf(context)}");
- return Scaffold(
- backgroundColor: habitCategoryProvider.habitCategoryData.color,
- appBar: AppBar(
- backgroundColor: habitCategoryProvider.habitCategoryData.color,
- leading:
- // button to close the page and return to HomePage
- IconButton(
- onPressed: prepToClosePage,
- icon: const Icon(Icons.close_rounded),
- ),
- centerTitle: true,
- title: Text(
- Provider.of<EditHabitProvider>(context).editingMode
- ? "Edit Habit"
- : "New Habit",
- style: textTheme.titleLarge,
- ),
- actions: [
- // button to create a new habit with provided variables
- TextButton(
- onPressed: habitCreationFlow,
- child: Text(
- "Save",
- style: textTheme.bodyLarge,
- ),
- ),
- ],
- ),
- body: SingleChildScrollView(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- // top section of the page, with save and close.
- // main body of the page
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- const SizedBox(
- height: 16,
- ),
- // section showing selectable Category cards
- const CarouselWidget(modifier: 0.22),
- const SizedBox(
- height: 24,
- ),
- // 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: 24,
- ),
- // label section
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- mainAxisSize: MainAxisSize.max,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- mainAxisSize: MainAxisSize.max,
- children: [
- Text(
- "Labels",
- style: textTheme.bodyLarge,
- ),
- Text(
- "${habitCategoryProvider.selectedItemslist.length}/${habitCategoryProvider.maxSelectableChips}",
- style: textTheme.bodyLarge,
- )
- ],
- ),
- const SizedBox(
- height: 8,
- ),
- const Center(child: LabelSectionContainerWidget())
- ],
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement