mirror of
https://github.com/Iconica-Development/flutter_form_wizard.git
synced 2025-05-19 19:03:47 +02:00
add enums to date picker
This commit is contained in:
parent
564f30b53c
commit
81b2f079c7
3 changed files with 134 additions and 41 deletions
|
@ -30,8 +30,10 @@ class _DatePageState extends State<DatePage> {
|
||||||
flutterFormWidgets: [
|
flutterFormWidgets: [
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.fromLTRB(40, 0, 40, 40),
|
padding: const EdgeInsets.fromLTRB(40, 0, 40, 40),
|
||||||
child: FlutterFormInputDate(
|
child: FlutterFormInputDateTime(
|
||||||
|
inputType: FlutterFormDateTimeType.dateTime,
|
||||||
dateFormat: DateFormat.yMd(),
|
dateFormat: DateFormat.yMd(),
|
||||||
|
firstDate: DateTime.now(),
|
||||||
label: const Text("Date"),
|
label: const Text("Date"),
|
||||||
controller: widget.dateController,
|
controller: widget.dateController,
|
||||||
),
|
),
|
||||||
|
|
|
@ -1,56 +1,122 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_form/utils/translation_service.dart';
|
import 'package:flutter_form/utils/translation_service.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
import '../../../../../flutter_form.dart';
|
import '../../../../../flutter_form.dart';
|
||||||
|
|
||||||
/// Generates a [TextFormField] for passwords. It requires a [FlutterFormInputController]
|
/// Generates a [DateTimeInputField] for DateTimes/Dates/Times/DateRanges.
|
||||||
/// as the [controller] parameter and an optional [Widget] as [label]
|
/// It requires a [FlutterFormInputController], [inputType], [dateFormat], [firstDate], and [lastDate]
|
||||||
class DateInputField extends ConsumerStatefulWidget {
|
class DateTimeInputField extends ConsumerStatefulWidget {
|
||||||
const DateInputField(
|
const DateTimeInputField({
|
||||||
{Key? key,
|
Key? key,
|
||||||
required this.controller,
|
required this.inputType,
|
||||||
this.label,
|
required this.controller,
|
||||||
this.showIcon = true,
|
this.label,
|
||||||
this.icon = Icons.calendar_today,
|
this.showIcon = true,
|
||||||
required this.dateFormat})
|
this.icon = Icons.calendar_today,
|
||||||
: super(
|
required this.dateFormat,
|
||||||
|
required this.firstDate,
|
||||||
|
required this.lastDate,
|
||||||
|
}) : super(
|
||||||
key: key,
|
key: key,
|
||||||
);
|
);
|
||||||
|
final FlutterFormDateTimeType inputType;
|
||||||
final FlutterFormInputController controller;
|
final FlutterFormInputController controller;
|
||||||
final DateFormat dateFormat;
|
final DateFormat dateFormat;
|
||||||
final bool showIcon;
|
final bool showIcon;
|
||||||
|
final DateTime? firstDate;
|
||||||
|
final DateTime? lastDate;
|
||||||
final IconData icon;
|
final IconData icon;
|
||||||
final Widget? label;
|
final Widget? label;
|
||||||
@override
|
@override
|
||||||
ConsumerState<DateInputField> createState() => _DateInputFieldState();
|
ConsumerState<DateTimeInputField> createState() => _DateInputFieldState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _DateInputFieldState extends ConsumerState<DateInputField> {
|
class _DateInputFieldState extends ConsumerState<DateTimeInputField> {
|
||||||
|
late final DateTime firstDate;
|
||||||
|
late final DateTime lastDate;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
firstDate = widget.firstDate ??
|
||||||
|
DateTime.now().subtract(
|
||||||
|
const Duration(days: 1000),
|
||||||
|
);
|
||||||
|
lastDate = widget.lastDate ??
|
||||||
|
DateTime.now().add(
|
||||||
|
const Duration(days: 1000),
|
||||||
|
);
|
||||||
|
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
String Function(String, {List<String>? params}) _ =
|
String Function(String, {List<String>? params}) _ =
|
||||||
getTranslator(context, ref);
|
getTranslator(context, ref);
|
||||||
|
|
||||||
|
Future<String> getInputFromUser(FlutterFormDateTimeType inputType) async {
|
||||||
|
String userInput = '';
|
||||||
|
switch (inputType) {
|
||||||
|
case FlutterFormDateTimeType.date:
|
||||||
|
DateTime? unformatted = await showDatePicker(
|
||||||
|
context: context,
|
||||||
|
initialDate: DateTime.now(),
|
||||||
|
firstDate: firstDate,
|
||||||
|
lastDate: lastDate,
|
||||||
|
);
|
||||||
|
userInput = unformatted != null
|
||||||
|
? widget.dateFormat.format(unformatted)
|
||||||
|
: userInput;
|
||||||
|
break;
|
||||||
|
case FlutterFormDateTimeType.dateTime:
|
||||||
|
await getInputFromUser(FlutterFormDateTimeType.date)
|
||||||
|
.then((value) async {
|
||||||
|
if (value != '') {
|
||||||
|
String secondInput =
|
||||||
|
await getInputFromUser(FlutterFormDateTimeType.time);
|
||||||
|
if (secondInput != '') {
|
||||||
|
userInput = '$value $secondInput';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case FlutterFormDateTimeType.range:
|
||||||
|
userInput = (await showDateRangePicker(
|
||||||
|
context: context,
|
||||||
|
firstDate: firstDate,
|
||||||
|
lastDate: lastDate,
|
||||||
|
).then((value) {
|
||||||
|
return value != null
|
||||||
|
? '${widget.dateFormat.format(value.start)} - ${widget.dateFormat.format(value.end)}'
|
||||||
|
: '';
|
||||||
|
}))
|
||||||
|
.toString();
|
||||||
|
break;
|
||||||
|
case FlutterFormDateTimeType.time:
|
||||||
|
userInput = await showTimePicker(
|
||||||
|
context: context, initialTime: TimeOfDay.now())
|
||||||
|
.then((value) => value == null ? '' : value.format(context));
|
||||||
|
}
|
||||||
|
return userInput;
|
||||||
|
}
|
||||||
|
|
||||||
return TextFormField(
|
return TextFormField(
|
||||||
|
keyboardType: TextInputType.none,
|
||||||
|
readOnly: true,
|
||||||
key: Key(widget.controller.value.toString()),
|
key: Key(widget.controller.value.toString()),
|
||||||
initialValue: widget.controller.value,
|
initialValue: widget.controller.value,
|
||||||
onSaved: (value) {
|
onSaved: (value) {
|
||||||
widget.controller.onSaved(value);
|
widget.controller.onSaved(value);
|
||||||
},
|
},
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
DateTime? pickedDate = await showDatePicker(
|
String userInput = await getInputFromUser(widget.inputType);
|
||||||
context: context,
|
setState(() {
|
||||||
initialDate: DateTime.now(),
|
widget.controller.value =
|
||||||
firstDate: DateTime.now(),
|
userInput != '' ? userInput : widget.controller.value;
|
||||||
lastDate: DateTime.now().add(
|
});
|
||||||
const Duration(days: 2000),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
if (pickedDate != null) {
|
|
||||||
widget.controller.value = widget.dateFormat.format(pickedDate);
|
|
||||||
setState(() {});
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
validator: (value) => widget.controller.onValidate(value, _),
|
validator: (value) => widget.controller.onValidate(value, _),
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
|
|
|
@ -6,24 +6,38 @@ import 'package:intl/intl.dart';
|
||||||
|
|
||||||
import '../../../../../flutter_form.dart';
|
import '../../../../../flutter_form.dart';
|
||||||
|
|
||||||
/// Input for a date used in a [FlutterForm].
|
/// Select Input Types in a [FlutterFormInputDateTime]
|
||||||
|
enum FlutterFormDateTimeType {
|
||||||
|
date,
|
||||||
|
time,
|
||||||
|
dateTime,
|
||||||
|
range,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Input for a dateTime used in a [FlutterForm].
|
||||||
///
|
///
|
||||||
/// Standard controller is [FlutterFormInputDateController].
|
/// Standard controller is [FlutterFormInputDateController].
|
||||||
class FlutterFormInputDate extends FlutterFormInputWidget {
|
class FlutterFormInputDateTime extends FlutterFormInputWidget {
|
||||||
const FlutterFormInputDate(
|
const FlutterFormInputDateTime({
|
||||||
{Key? key,
|
Key? key,
|
||||||
required FlutterFormInputController controller,
|
required FlutterFormInputController controller,
|
||||||
Widget? label,
|
Widget? label,
|
||||||
this.showIcon = true,
|
this.showIcon = true,
|
||||||
this.icon = Icons.calendar_today,
|
required this.inputType,
|
||||||
required this.dateFormat})
|
required this.dateFormat,
|
||||||
: super(
|
this.firstDate,
|
||||||
|
this.lastDate,
|
||||||
|
this.icon = Icons.calendar_today,
|
||||||
|
}) : super(
|
||||||
key: key,
|
key: key,
|
||||||
controller: controller,
|
controller: controller,
|
||||||
label: label,
|
label: label,
|
||||||
);
|
);
|
||||||
final DateFormat dateFormat;
|
|
||||||
final bool showIcon;
|
final bool showIcon;
|
||||||
|
final FlutterFormDateTimeType inputType;
|
||||||
|
final DateFormat dateFormat;
|
||||||
|
final DateTime? firstDate;
|
||||||
|
final DateTime? lastDate;
|
||||||
final IconData icon;
|
final IconData icon;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -32,23 +46,34 @@ class FlutterFormInputDate extends FlutterFormInputWidget {
|
||||||
getTranslator(context, ref);
|
getTranslator(context, ref);
|
||||||
super.registerController(context);
|
super.registerController(context);
|
||||||
|
|
||||||
return DateInputField(controller: controller, dateFormat: dateFormat);
|
return DateTimeInputField(
|
||||||
|
firstDate: firstDate,
|
||||||
|
lastDate: lastDate,
|
||||||
|
inputType: inputType,
|
||||||
|
controller: controller,
|
||||||
|
dateFormat: dateFormat,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Controller for dates used by a [FlutterFormInputWidget] used in a [FlutterForm].
|
/// Controller for dates used by a [FlutterFormInputWidget] used in a [FlutterForm].
|
||||||
///
|
///
|
||||||
/// Mainly used by [FlutterFormInputDate].
|
/// Mainly used by [FlutterFormInputDateTime].
|
||||||
class FlutterFormInputDateController
|
class FlutterFormInputDateTimeController
|
||||||
implements FlutterFormInputController<String> {
|
implements FlutterFormInputController<String> {
|
||||||
FlutterFormInputDateController({
|
FlutterFormInputDateTimeController({
|
||||||
required this.id,
|
required this.id,
|
||||||
this.mandatory = true,
|
this.mandatory = true,
|
||||||
this.value,
|
this.value,
|
||||||
this.checkPageTitle,
|
this.checkPageTitle,
|
||||||
this.checkPageDescription,
|
this.checkPageDescription,
|
||||||
|
required this.dateTimeType,
|
||||||
|
required this.dateFormat,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
final DateFormat dateFormat;
|
||||||
|
final FlutterFormDateTimeType dateTimeType;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String? id;
|
String? id;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue