2022-08-26 15:31:42 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:profile/src/widgets/item_builder/item_builder_options.dart';
|
|
|
|
|
2022-09-20 14:11:38 +02:00
|
|
|
/// ItemBuilder is used to set the standard textfield for each undefined users data item.
|
|
|
|
///
|
|
|
|
/// Options sets options for the textfield.
|
2022-08-26 15:31:42 +02:00
|
|
|
class ItemBuilder {
|
|
|
|
ItemBuilder({
|
|
|
|
required this.options,
|
|
|
|
});
|
|
|
|
|
|
|
|
final ItemBuilderOptions options;
|
|
|
|
|
2022-09-19 12:11:58 +02:00
|
|
|
Widget build(
|
|
|
|
String key, dynamic value, Widget? widget, Function(String) updateItem) {
|
2022-08-26 15:31:42 +02:00
|
|
|
if (widget == null) {
|
|
|
|
var controller = TextEditingController(
|
2022-09-19 12:11:58 +02:00
|
|
|
text: '${value ?? ''}',
|
2022-08-26 15:31:42 +02:00
|
|
|
);
|
|
|
|
|
2022-09-19 12:11:58 +02:00
|
|
|
late InputDecoration inputDecoration;
|
|
|
|
if (options.inputDecorationField != null &&
|
|
|
|
options.inputDecorationField![key] != null) {
|
|
|
|
inputDecoration = options.inputDecorationField![key]!;
|
|
|
|
} else {
|
|
|
|
inputDecoration = options.inputDecoration;
|
|
|
|
}
|
|
|
|
|
|
|
|
final formKey = GlobalKey<FormState>();
|
|
|
|
|
|
|
|
return Form(
|
|
|
|
key: formKey,
|
|
|
|
child: TextFormField(
|
|
|
|
controller: controller,
|
|
|
|
decoration: inputDecoration,
|
|
|
|
readOnly: options.readOnly,
|
|
|
|
onFieldSubmitted: (value) {
|
|
|
|
if (formKey.currentState!.validate()) {
|
|
|
|
updateItem(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
validator: (value) {
|
|
|
|
if (options.validators != null &&
|
|
|
|
options.validators![key] != null) {
|
|
|
|
return options.validators![key]!(value);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
2022-08-26 15:31:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
}
|