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

50 lines
1.3 KiB
Dart
Raw Normal View History

2022-10-31 17:15:05 +01:00
// SPDX-FileCopyrightText: 2022 Iconica
//
// SPDX-License-Identifier: BSD-3-Clause
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;
Widget build(String key, dynamic value, Widget? widget,
2022-11-15 11:20:48 +01:00
Function(String) updateItem, Function(String?) saveItem) {
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;
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 15:34:24 +02:00
keyboardType: options.keyboardType?[key],
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-11-15 11:20:48 +01:00
updateItem(value);
2022-10-25 09:17:08 +02:00
},
onSaved: (newValue) {
2022-11-15 11:20:48 +01:00
saveItem(newValue);
},
2022-10-25 09:17:08 +02:00
validator: (value) {
return options.validators?[key]?.call(value);
},
2022-08-26 15:31:42 +02:00
);
}
return widget;
}
}