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 {
- final editHabitProvider =
- Provider.of<EditHabitProvider>(context, listen: false);
- final habitCategoryProvider =
- Provider.of<HabitCategoryProvider>(context, listen: false);
- final habitDatabase = Provider.of<HabitDatabase>(context, listen: false);
- var newitemDataIntKeys = habitCategoryProvider.selectedItemslist.toList();
- debugPrint("LIST WITH LABELS: ${newitemDataIntKeys.length}");
- if (titleTextController.text == "") {
- // tell user to provide a valid title
- formkey.currentState!.validate();
- await showDialog<void>(
- 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");
- return;
- }
- // create a new habit or edit existing habit
- if (editHabitProvider.isEditMode) {
- habitDatabase.editHabit(
- editHabitProvider.habitToEdit!.id,
- titleTextController.text,
- detailsTextController.text,
- newitemDataIntKeys,
- habitCategoryProvider.habitCategoryData.category,
- );
- } else if (!editHabitProvider.isEditMode) {
- habitDatabase.addHabit(
- titleTextController.text,
- detailsTextController.text,
- newitemDataIntKeys,
- habitCategoryProvider.habitCategoryData.category,
- );
- }
- prepToClosePage();
- }
- void prepToClosePage() {
- final editHabitProvider =
- Provider.of<EditHabitProvider>(context, listen: false);
- if (editHabitProvider.isEditMode) {
- editHabitProvider.endEditingMode();
- }
- Provider.of<HabitCategoryProvider>(context, listen: false)
- .clearSelectedItemsList();
- titleTextController.clear();
- detailsTextController.clear();
- 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;
- final habitCategoryProvider = Provider.of<HabitCategoryProvider>(context);
- debugPrint("VIEW PADDING: ${MediaQuery.viewPaddingOf(context)}");
- return Scaffold(
- backgroundColor: habitCategoryProvider.habitCategoryData.color,
- appBar: AppBar(
- backgroundColor: habitCategoryProvider.habitCategoryData.color,
- leading: IconButton(
- onPressed: prepToClosePage,
- icon: const Icon(Icons.close_rounded),
- ),
- centerTitle: true,
- title: Text(
- Provider.of<EditHabitProvider>(context).isEditMode
- ? "Edit Habit"
- : "New Habit",
- style: textTheme.titleLarge,
- ),
- actions: [
- TextButton(
- onPressed: habitCreationFlow,
- child: Text(
- "Save",
- style: textTheme.bodyLarge,
- ),
- ),
- ],
- ),
- body: ScrollViewBody(
- titleTextController: titleTextController,
- detailsTextController: detailsTextController,
- textTheme: textTheme,
- habitCategoryProvider: habitCategoryProvider,
- ),
- );
- }
- }
- class ScrollViewBody extends StatelessWidget {
- const ScrollViewBody({
- super.key,
- required this.titleTextController,
- required this.detailsTextController,
- required this.textTheme,
- required this.habitCategoryProvider,
- });
- final TextEditingController titleTextController;
- final TextEditingController detailsTextController;
- final TextTheme textTheme;
- final HabitCategoryProvider habitCategoryProvider;
- @override
- Widget build(BuildContext context) {
- return SingleChildScrollView(
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- const SizedBox(
- height: 16,
- ),
- const CarouselWidget(modifier: 0.22),
- const SizedBox(
- height: 24,
- ),
- MyTextFieldSection(
- title: "Title",
- limitation: true,
- textEditingController: titleTextController,
- maxLines: 1,
- maxLength: 40,
- ),
- const SizedBox(
- height: 16,
- ),
- MyTextFieldSection(
- title: "More Details",
- limitation: false,
- textEditingController: detailsTextController,
- maxLines: 4,
- maxLength: 300,
- ),
- const SizedBox(
- height: 24,
- ),
- 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