flutter_profile/lib/src/widgets/item_builder/item_builder.dart

56 lines
1.5 KiB
Dart
Raw Normal View History

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;
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(
text: '${value ?? ''}',
2022-08-26 15:31:42 +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(
2022-09-21 09:20:15 +02:00
key: Key(key),
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;
}
}