2022-08-26 15:31:42 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-21 14:37:16 +02:00
|
|
|
import 'package:flutter_profile/src/widgets/item_builder/item_builder_options.dart';
|
2022-08-26 15:31:42 +02:00
|
|
|
|
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-10-28 14:12:40 +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;
|
2022-09-22 09:53:26 +02:00
|
|
|
|
|
|
|
inputDecoration =
|
|
|
|
options.inputDecorationField?[key] ?? options.inputDecoration;
|
2022-10-28 14:12:40 +02:00
|
|
|
var formFieldKey = GlobalKey<FormFieldState>();
|
2022-10-25 09:17:08 +02:00
|
|
|
return TextFormField(
|
2022-10-28 14:12:40 +02:00
|
|
|
key: formFieldKey,
|
2022-10-25 09:17:08 +02:00
|
|
|
controller: controller,
|
|
|
|
decoration: inputDecoration,
|
|
|
|
readOnly: options.readOnly,
|
|
|
|
onFieldSubmitted: (value) {
|
2022-10-28 14:12:40 +02:00
|
|
|
if (formFieldKey.currentState!.validate()) {
|
2022-10-25 09:17:08 +02:00
|
|
|
updateItem(value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
validator: (value) {
|
|
|
|
return options.validators?[key]?.call(value);
|
|
|
|
},
|
2022-08-26 15:31:42 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
}
|