2022-09-20 11:04:00 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '/src/utils/formstate.dart' as fs;
|
|
|
|
|
2022-09-28 12:02:40 +02:00
|
|
|
/// Abstract class for the input widgets used in a [FlutterForm].
|
2022-09-20 11:04:00 +02:00
|
|
|
///
|
2022-09-28 12:02:40 +02:00
|
|
|
/// The controller [FlutterFormInputController] has to be given to the widget.
|
2022-09-20 11:04:00 +02:00
|
|
|
/// Whicht controller is used determines how to value will be handled.
|
|
|
|
///
|
|
|
|
/// label is a standard parameter to normally sets the label of the input.
|
2022-09-28 12:02:40 +02:00
|
|
|
abstract class FlutterFormInputWidget extends ConsumerWidget {
|
|
|
|
const FlutterFormInputWidget({
|
2022-09-20 11:04:00 +02:00
|
|
|
Key? key,
|
|
|
|
required this.controller,
|
|
|
|
this.label,
|
|
|
|
String? hintText,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
2022-09-28 12:02:40 +02:00
|
|
|
final FlutterFormInputController controller;
|
2022-09-20 11:04:00 +02:00
|
|
|
final Widget? label;
|
|
|
|
|
|
|
|
registerController(BuildContext context) {
|
2022-09-28 12:02:40 +02:00
|
|
|
FlutterFormInputController? localController =
|
2022-09-20 11:04:00 +02:00
|
|
|
fs.FormState.of(context).formController.getController(controller.id!);
|
|
|
|
|
|
|
|
if (localController == null) {
|
|
|
|
fs.FormState.of(context).formController.register(controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:02:40 +02:00
|
|
|
/// Abstract class for the controller for inputs used in a [FlutterForm].
|
2022-09-20 11:04:00 +02:00
|
|
|
///
|
2022-09-28 12:02:40 +02:00
|
|
|
/// The [id] determines the key in the [Map] returned by the [FlutterForm].
|
2022-09-20 11:04:00 +02:00
|
|
|
///
|
2022-09-20 11:58:06 +02:00
|
|
|
/// [value] is a way to set a initial value.
|
2022-09-20 11:04:00 +02:00
|
|
|
///
|
2022-09-20 11:58:06 +02:00
|
|
|
/// [mandatory] determines if the input is mandatory.
|
2022-09-20 11:04:00 +02:00
|
|
|
///
|
2022-09-20 11:58:06 +02:00
|
|
|
/// [checkPageTitle] is a function where you can transform the value from the input into something representable.
|
2022-09-20 11:04:00 +02:00
|
|
|
/// This value will be given when defining the check page widgets.
|
|
|
|
/// If this function is not set, the value will be used as is.
|
|
|
|
/// Example:
|
|
|
|
/// ``` dart
|
|
|
|
/// checkPageTitle: (dynamic amount) {
|
|
|
|
/// return "$amount persons";
|
|
|
|
/// },
|
|
|
|
/// ```
|
|
|
|
///
|
2022-09-20 11:58:06 +02:00
|
|
|
/// [checkPageDescription] is the same as checkPageTitle but for the description.
|
2022-09-20 11:04:00 +02:00
|
|
|
/// If null no description will be shown.
|
2022-09-28 12:02:40 +02:00
|
|
|
abstract class FlutterFormInputController<T> {
|
2022-09-20 11:04:00 +02:00
|
|
|
String? id;
|
|
|
|
T? value;
|
|
|
|
bool mandatory = false;
|
|
|
|
String Function(T value)? checkPageTitle;
|
|
|
|
String Function(T value)? checkPageDescription;
|
|
|
|
|
|
|
|
void onSaved(T value);
|
|
|
|
|
|
|
|
String? onValidate(
|
|
|
|
T value, String Function(String, {List<String>? params}) translator);
|
|
|
|
}
|