add some components for settings later
This commit is contained in:
parent
e55d7c4c58
commit
274435b83e
2 changed files with 134 additions and 0 deletions
80
lib/widgets/form_text_input.dart
Normal file
80
lib/widgets/form_text_input.dart
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
import "package:flutter/material.dart";
|
||||||
|
import "package:flutter/services.dart";
|
||||||
|
|
||||||
|
class FormTextInput extends StatelessWidget {
|
||||||
|
final List<String? Function(String value)> 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<TextInputFormatter>? 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;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
54
lib/widgets/radio_dialog.dart
Normal file
54
lib/widgets/radio_dialog.dart
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
import "package:flutter/material.dart";
|
||||||
|
import "package:flutter_hooks/flutter_hooks.dart";
|
||||||
|
|
||||||
|
class RadioDialog<T> extends HookWidget {
|
||||||
|
final T? value;
|
||||||
|
final String title;
|
||||||
|
final List<T> 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<T?>(null);
|
||||||
|
return AlertDialog(
|
||||||
|
title: Text(title),
|
||||||
|
content: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: options
|
||||||
|
.map(
|
||||||
|
(option) => RadioListTile<T>(
|
||||||
|
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"),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue