This commit is contained in:
brausjonas
2024-09-15 19:51:29 +02:00
parent 8740525457
commit 77a411e4b7
8 changed files with 391 additions and 110 deletions
+101 -39
View File
@@ -1,57 +1,119 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:todoapp/elems/todo.dart';
import 'package:todoapp/modals/datepickermodal.dart';
import 'package:todoapp/modals/timepickermodal.dart';
import 'package:todoapp/providers/todoprovider.dart';
class AddTodoModal {
static String tempValue = "";
static void add(BuildContext context) {
showModalBottomSheet(
context: context,
showDragHandle: true,
useSafeArea: true,
scrollControlDisabledMaxHeightRatio: 1,
builder: (context) => Padding(
padding: const EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
autofocus: true,
decoration: const InputDecoration(labelText: "Title"),
onChanged: (value) => tempValue = value,
onFieldSubmitted: (value) {
context.read<TodoProvider>().addTodo(value);
Navigator.pop(context);
},
builder: (context) => const ModalCreateTodo(),
);
}
}
class ModalCreateTodo extends StatefulWidget {
const ModalCreateTodo({super.key});
@override
State<ModalCreateTodo> createState() => _ModalCreateTodoState();
}
class _ModalCreateTodoState extends State<ModalCreateTodo> {
String tempValue = "";
DateTime tempDateSelect = DateTime.now();
TimeOfDay tempTimeSelect = TimeOfDay.now();
String formatNumber(int number) {
if (number < 10) {
return "0$number";
}
return number.toString();
}
DateTime combineTime() {
return DateTime(tempDateSelect.year, tempDateSelect.month,
tempDateSelect.day, tempTimeSelect.hour, tempTimeSelect.minute);
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextFormField(
autofocus: true,
decoration: const InputDecoration(labelText: "Title"),
onChanged: (value) => tempValue = value,
onFieldSubmitted: (value) {
context.read<TodoProvider>().addTodo(value, combineTime());
Navigator.pop(context);
},
),
const SizedBox(height: 20),
FilledButton.tonal(
onPressed: () async {
tempDateSelect =
(await DatePickerModal.add(context, tempDateSelect))!;
setState(() {});
},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Icon(Icons.date_range),
const SizedBox(width: 20),
Text(
"${tempDateSelect.year}-${formatNumber(tempDateSelect.month)}-${formatNumber(tempDateSelect.day)}")
],
),
const SizedBox(height: 20,),
SegmentedButton(
segments: const [
ButtonSegment(
),
const SizedBox(height: 10),
FilledButton.tonal(
onPressed: () async {
tempTimeSelect =
(await TimePickerModal.add(context, tempTimeSelect))!;
setState(() {});
},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Icon(Icons.timer),
const SizedBox(width: 20),
Text(
"${formatNumber(tempTimeSelect.hour)}:${formatNumber(tempTimeSelect.minute)}")
],
),
),
const SizedBox(height: 20),
SegmentedButton(
segments: const [
ButtonSegment(
value: "Cancel",
label: Text("Cancel"),
icon: Icon(Icons.cancel)
),
ButtonSegment(
value: "Save",
label: Text("Save"),
icon: Icon(Icons.save)
),
],
selected: const {"None"},
style: ButtonStyle(iconColor: WidgetStatePropertyAll(Theme.of(context).colorScheme.primary)),
onSelectionChanged: (p0) {
if (p0.last == "Save") {
context.read<TodoProvider>().addTodo(tempValue);
}
Navigator.pop(context);
},
)
],
),
icon: Icon(Icons.cancel)),
ButtonSegment(
value: "Save", label: Text("Save"), icon: Icon(Icons.save)),
],
selected: const {"None"},
style: ButtonStyle(
iconColor: WidgetStatePropertyAll(
Theme.of(context).colorScheme.primary)),
onSelectionChanged: (p0) {
if (p0.last == "Save") {
context.read<TodoProvider>().addTodo(tempValue, combineTime());
}
Navigator.pop(context);
},
)
],
),
);
}
}
}