diff --git a/lib/widgets/form_text_input.dart b/lib/widgets/form_text_input.dart new file mode 100644 index 0000000..492439b --- /dev/null +++ b/lib/widgets/form_text_input.dart @@ -0,0 +1,80 @@ +import "package:flutter/material.dart"; +import "package:flutter/services.dart"; + +class FormTextInput extends StatelessWidget { + final List extraValidators; + final TextEditingController? controller; + final TextInputType keyboardType; + final String? initialValue; + final bool readOnly; + final bool obscure; + final String? title; + final int? minLines; + final int? maxLength; + final bool outlined; + final int? maxLines; + final bool capitalize; + final bool required; + final bool autocorrect; + final void Function()? onTap; + final Widget? trailing; + final InputBorder? border; + final List? formatters; + + const FormTextInput({ + super.key, + this.border, + this.controller, + this.title, + this.obscure = false, + this.readOnly = false, + this.extraValidators = const [], + this.keyboardType = TextInputType.text, + this.initialValue, + this.minLines, + this.capitalize = false, + this.maxLength, + this.formatters, + this.maxLines = 1, + this.outlined = true, + this.trailing, + this.onTap, + this.autocorrect = true, + this.required = true, + }); + + @override + Widget build(BuildContext context) => TextFormField( + controller: controller, + keyboardType: keyboardType, + readOnly: readOnly, + minLines: minLines, + maxLines: maxLines, + maxLength: maxLength, + inputFormatters: formatters, + textCapitalization: capitalize + ? TextCapitalization.sentences + : TextCapitalization.none, + initialValue: initialValue, + autocorrect: autocorrect, + obscureText: obscure, + onTap: onTap, + decoration: InputDecoration( + labelText: title, + border: border ?? (outlined ? null : const UnderlineInputBorder()), + suffixIcon: trailing, + ), + validator: (value) { + if ((value?.isEmpty ?? true) && required) { + return "This field is required"; + } + + for (final validator in extraValidators) { + final reason = validator(value!); + if (reason != null) return reason; + } + + return null; + }, + ); +} diff --git a/lib/widgets/radio_dialog.dart b/lib/widgets/radio_dialog.dart new file mode 100644 index 0000000..f36962f --- /dev/null +++ b/lib/widgets/radio_dialog.dart @@ -0,0 +1,54 @@ +import "package:flutter/material.dart"; +import "package:flutter_hooks/flutter_hooks.dart"; + +class RadioDialog extends HookWidget { + final T? value; + final String title; + final List options; + final void Function(T value)? onChanged; + final String Function(T option) getName; + const RadioDialog({ + super.key, + required this.title, + required this.value, + required this.options, + required this.onChanged, + required this.getName, + }); + + @override + Widget build(BuildContext context) { + final mutValue = useState(null); + return AlertDialog( + title: Text(title), + content: Column( + mainAxisSize: MainAxisSize.min, + children: options + .map( + (option) => RadioListTile( + value: option, + groupValue: mutValue.value ?? value, + onChanged: onChanged == null + ? null + : (value) => + mutValue.value = value ?? mutValue.value ?? value, + title: Text(getName(option)), + dense: true, + ), + ) + .toList(), + ), + actions: [ + TextButton(onPressed: Navigator.of(context).pop, child: Text("Cancel")), + if (onChanged != null) + TextButton( + onPressed: () { + if (mutValue.value != null) onChanged!(mutValue.value as T); + Navigator.of(context).pop(); + }, + child: Text("OK"), + ), + ], + ); + } +}