mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-07-23 01:29:40 +02:00
Lint all files
This commit is contained in:
@@ -16,11 +16,13 @@ abstract class GeneratedFormItem {
|
||||
dynamic ensureType(dynamic val);
|
||||
GeneratedFormItem clone();
|
||||
|
||||
GeneratedFormItem(this.key,
|
||||
{this.label = 'Input',
|
||||
this.belowWidgets = const [],
|
||||
this.defaultValue,
|
||||
this.additionalValidators = const []});
|
||||
GeneratedFormItem(
|
||||
this.key, {
|
||||
this.label = 'Input',
|
||||
this.belowWidgets = const [],
|
||||
this.defaultValue,
|
||||
this.additionalValidators = const [],
|
||||
});
|
||||
}
|
||||
|
||||
class GeneratedFormTextField extends GeneratedFormItem {
|
||||
@@ -31,18 +33,19 @@ class GeneratedFormTextField extends GeneratedFormItem {
|
||||
late TextInputType? textInputType;
|
||||
late List<String>? autoCompleteOptions;
|
||||
|
||||
GeneratedFormTextField(super.key,
|
||||
{super.label,
|
||||
super.belowWidgets,
|
||||
String super.defaultValue = '',
|
||||
List<String? Function(String? value)> super.additionalValidators =
|
||||
const [],
|
||||
this.required = true,
|
||||
this.max = 1,
|
||||
this.hint,
|
||||
this.password = false,
|
||||
this.textInputType,
|
||||
this.autoCompleteOptions});
|
||||
GeneratedFormTextField(
|
||||
super.key, {
|
||||
super.label,
|
||||
super.belowWidgets,
|
||||
String super.defaultValue = '',
|
||||
List<String? Function(String? value)> super.additionalValidators = const [],
|
||||
this.required = true,
|
||||
this.max = 1,
|
||||
this.hint,
|
||||
this.password = false,
|
||||
this.textInputType,
|
||||
this.autoCompleteOptions,
|
||||
});
|
||||
|
||||
@override
|
||||
String ensureType(val) {
|
||||
@@ -51,16 +54,18 @@ class GeneratedFormTextField extends GeneratedFormItem {
|
||||
|
||||
@override
|
||||
GeneratedFormTextField clone() {
|
||||
return GeneratedFormTextField(key,
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
additionalValidators: List.from(additionalValidators),
|
||||
required: required,
|
||||
max: max,
|
||||
hint: hint,
|
||||
password: password,
|
||||
textInputType: textInputType);
|
||||
return GeneratedFormTextField(
|
||||
key,
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
additionalValidators: List.from(additionalValidators),
|
||||
required: required,
|
||||
max: max,
|
||||
hint: hint,
|
||||
password: password,
|
||||
textInputType: textInputType,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,8 +96,9 @@ class GeneratedFormDropdown extends GeneratedFormItem {
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
disabledOptKeys:
|
||||
disabledOptKeys != null ? List.from(disabledOptKeys!) : null,
|
||||
disabledOptKeys: disabledOptKeys != null
|
||||
? List.from(disabledOptKeys!)
|
||||
: null,
|
||||
additionalValidators: List.from(additionalValidators),
|
||||
);
|
||||
}
|
||||
@@ -117,12 +123,14 @@ class GeneratedFormSwitch extends GeneratedFormItem {
|
||||
|
||||
@override
|
||||
GeneratedFormSwitch clone() {
|
||||
return GeneratedFormSwitch(key,
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
disabled: false,
|
||||
additionalValidators: List.from(additionalValidators));
|
||||
return GeneratedFormSwitch(
|
||||
key,
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
disabled: false,
|
||||
additionalValidators: List.from(additionalValidators),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,17 +140,20 @@ class GeneratedFormTagInput extends GeneratedFormItem {
|
||||
late WrapAlignment alignment;
|
||||
late String emptyMessage;
|
||||
late bool showLabelWhenNotEmpty;
|
||||
GeneratedFormTagInput(super.key,
|
||||
{super.label,
|
||||
super.belowWidgets,
|
||||
Map<String, MapEntry<int, bool>> super.defaultValue = const {},
|
||||
List<String? Function(Map<String, MapEntry<int, bool>> value)>
|
||||
super.additionalValidators = const [],
|
||||
this.deleteConfirmationMessage,
|
||||
this.singleSelect = false,
|
||||
this.alignment = WrapAlignment.start,
|
||||
this.emptyMessage = 'Input',
|
||||
this.showLabelWhenNotEmpty = true});
|
||||
GeneratedFormTagInput(
|
||||
super.key, {
|
||||
super.label,
|
||||
super.belowWidgets,
|
||||
Map<String, MapEntry<int, bool>> super.defaultValue = const {},
|
||||
List<String? Function(Map<String, MapEntry<int, bool>> value)>
|
||||
super.additionalValidators =
|
||||
const [],
|
||||
this.deleteConfirmationMessage,
|
||||
this.singleSelect = false,
|
||||
this.alignment = WrapAlignment.start,
|
||||
this.emptyMessage = 'Input',
|
||||
this.showLabelWhenNotEmpty = true,
|
||||
});
|
||||
|
||||
@override
|
||||
Map<String, MapEntry<int, bool>> ensureType(val) {
|
||||
@@ -151,25 +162,30 @@ class GeneratedFormTagInput extends GeneratedFormItem {
|
||||
|
||||
@override
|
||||
GeneratedFormTagInput clone() {
|
||||
return GeneratedFormTagInput(key,
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
additionalValidators: List.from(additionalValidators),
|
||||
deleteConfirmationMessage: deleteConfirmationMessage,
|
||||
singleSelect: singleSelect,
|
||||
alignment: alignment,
|
||||
emptyMessage: emptyMessage,
|
||||
showLabelWhenNotEmpty: showLabelWhenNotEmpty);
|
||||
return GeneratedFormTagInput(
|
||||
key,
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
additionalValidators: List.from(additionalValidators),
|
||||
deleteConfirmationMessage: deleteConfirmationMessage,
|
||||
singleSelect: singleSelect,
|
||||
alignment: alignment,
|
||||
emptyMessage: emptyMessage,
|
||||
showLabelWhenNotEmpty: showLabelWhenNotEmpty,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
typedef OnValueChanges = void Function(
|
||||
Map<String, dynamic> values, bool valid, bool isBuilding);
|
||||
typedef OnValueChanges =
|
||||
void Function(Map<String, dynamic> values, bool valid, bool isBuilding);
|
||||
|
||||
class GeneratedForm extends StatefulWidget {
|
||||
const GeneratedForm(
|
||||
{super.key, required this.items, required this.onValueChanges});
|
||||
const GeneratedForm({
|
||||
super.key,
|
||||
required this.items,
|
||||
required this.onValueChanges,
|
||||
});
|
||||
|
||||
final List<List<GeneratedFormItem>> items;
|
||||
final OnValueChanges onValueChanges;
|
||||
@@ -179,7 +195,8 @@ class GeneratedForm extends StatefulWidget {
|
||||
}
|
||||
|
||||
List<List<GeneratedFormItem>> cloneFormItems(
|
||||
List<List<GeneratedFormItem>> items) {
|
||||
List<List<GeneratedFormItem>> items,
|
||||
) {
|
||||
List<List<GeneratedFormItem>> clonedItems = [];
|
||||
for (var row in items) {
|
||||
List<GeneratedFormItem> clonedRow = [];
|
||||
@@ -194,8 +211,13 @@ List<List<GeneratedFormItem>> cloneFormItems(
|
||||
class GeneratedFormSubForm extends GeneratedFormItem {
|
||||
final List<List<GeneratedFormItem>> items;
|
||||
|
||||
GeneratedFormSubForm(super.key, this.items,
|
||||
{super.label, super.belowWidgets, super.defaultValue = const []});
|
||||
GeneratedFormSubForm(
|
||||
super.key,
|
||||
this.items, {
|
||||
super.label,
|
||||
super.belowWidgets,
|
||||
super.defaultValue = const [],
|
||||
});
|
||||
|
||||
@override
|
||||
ensureType(val) {
|
||||
@@ -204,8 +226,13 @@ class GeneratedFormSubForm extends GeneratedFormItem {
|
||||
|
||||
@override
|
||||
GeneratedFormSubForm clone() {
|
||||
return GeneratedFormSubForm(key, cloneFormItems(items),
|
||||
label: label, belowWidgets: belowWidgets, defaultValue: defaultValue);
|
||||
return GeneratedFormSubForm(
|
||||
key,
|
||||
cloneFormItems(items),
|
||||
label: label,
|
||||
belowWidgets: belowWidgets,
|
||||
defaultValue: defaultValue,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,13 +247,18 @@ Color generateRandomLightColor() {
|
||||
// Map from HPLuv color space to RGB, use constant saturation=100, lightness=70
|
||||
final List<double> rgbValuesDbl = Hsluv.hpluvToRgb([hue, 100, 70]);
|
||||
// Map RBG values from 0-1 to 0-255:
|
||||
final List<int> rgbValues =
|
||||
rgbValuesDbl.map((rgb) => (rgb * 255).toInt()).toList();
|
||||
final List<int> rgbValues = rgbValuesDbl
|
||||
.map((rgb) => (rgb * 255).toInt())
|
||||
.toList();
|
||||
return Color.fromARGB(255, rgbValues[0], rgbValues[1], rgbValues[2]);
|
||||
}
|
||||
|
||||
int generateRandomNumber(int seed1,
|
||||
{int seed2 = 0, int seed3 = 0, max = 10000}) {
|
||||
int generateRandomNumber(
|
||||
int seed1, {
|
||||
int seed2 = 0,
|
||||
int seed3 = 0,
|
||||
max = 10000,
|
||||
}) {
|
||||
int combinedSeed = seed1.hashCode ^ seed2.hashCode ^ seed3.hashCode;
|
||||
Random random = Random(combinedSeed);
|
||||
int randomNumber = random.nextInt(max);
|
||||
@@ -297,9 +329,9 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
||||
});
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
helperText:
|
||||
formItem.label + (formItem.required ? ' *' : ''),
|
||||
hintText: formItem.hint),
|
||||
helperText: formItem.label + (formItem.required ? ' *' : ''),
|
||||
hintText: formItem.hint,
|
||||
),
|
||||
minLines: formItem.max <= 1 ? null : formItem.max,
|
||||
maxLines: formItem.max <= 1 ? 1 : formItem.max,
|
||||
validator: (value) {
|
||||
@@ -339,23 +371,26 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
||||
return Text(tr('dropdownNoOptsError'));
|
||||
}
|
||||
return DropdownButtonFormField(
|
||||
decoration: InputDecoration(labelText: formItem.label),
|
||||
value: values[formItem.key],
|
||||
items: formItem.opts!.map((e2) {
|
||||
var enabled =
|
||||
formItem.disabledOptKeys?.contains(e2.key) != true;
|
||||
return DropdownMenuItem(
|
||||
value: e2.key,
|
||||
enabled: enabled,
|
||||
child: Opacity(
|
||||
opacity: enabled ? 1 : 0.5, child: Text(e2.value)));
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
values[formItem.key] = value ?? formItem.opts!.first.key;
|
||||
someValueChanged();
|
||||
});
|
||||
decoration: InputDecoration(labelText: formItem.label),
|
||||
value: values[formItem.key],
|
||||
items: formItem.opts!.map((e2) {
|
||||
var enabled = formItem.disabledOptKeys?.contains(e2.key) != true;
|
||||
return DropdownMenuItem(
|
||||
value: e2.key,
|
||||
enabled: enabled,
|
||||
child: Opacity(
|
||||
opacity: enabled ? 1 : 0.5,
|
||||
child: Text(e2.value),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
values[formItem.key] = value ?? formItem.opts!.first.key;
|
||||
someValueChanged();
|
||||
});
|
||||
},
|
||||
);
|
||||
} else if (formItem is GeneratedFormSubForm) {
|
||||
values[formItem.key] = [];
|
||||
for (Map<String, dynamic> v
|
||||
@@ -394,33 +429,33 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Flexible(child: Text(widget.items[r][e].label)),
|
||||
const SizedBox(
|
||||
width: 8,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Switch(
|
||||
value: values[fieldKey],
|
||||
onChanged:
|
||||
(widget.items[r][e] as GeneratedFormSwitch).disabled
|
||||
? null
|
||||
: (value) {
|
||||
setState(() {
|
||||
values[fieldKey] = value;
|
||||
someValueChanged();
|
||||
});
|
||||
})
|
||||
value: values[fieldKey],
|
||||
onChanged: (widget.items[r][e] as GeneratedFormSwitch).disabled
|
||||
? null
|
||||
: (value) {
|
||||
setState(() {
|
||||
values[fieldKey] = value;
|
||||
someValueChanged();
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
} else if (widget.items[r][e] is GeneratedFormTagInput) {
|
||||
onAddPressed() {
|
||||
showDialog<Map<String, dynamic>?>(
|
||||
context: context,
|
||||
builder: (BuildContext ctx) {
|
||||
return GeneratedFormModal(
|
||||
title: widget.items[r][e].label,
|
||||
items: [
|
||||
[GeneratedFormTextField('label', label: tr('label'))]
|
||||
]);
|
||||
}).then((value) {
|
||||
context: context,
|
||||
builder: (BuildContext ctx) {
|
||||
return GeneratedFormModal(
|
||||
title: widget.items[r][e].label,
|
||||
items: [
|
||||
[GeneratedFormTextField('label', label: tr('label'))],
|
||||
],
|
||||
);
|
||||
},
|
||||
).then((value) {
|
||||
String? label = value?['label'];
|
||||
if (label != null) {
|
||||
setState(() {
|
||||
@@ -434,8 +469,10 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
||||
var someSelected = temp.entries
|
||||
.where((element) => element.value.value)
|
||||
.isNotEmpty;
|
||||
temp[label] = MapEntry(generateRandomLightColor().value,
|
||||
!(someSelected && singleSelect));
|
||||
temp[label] = MapEntry(
|
||||
generateRandomLightColor().value,
|
||||
!(someSelected && singleSelect),
|
||||
);
|
||||
values[fieldKey] = temp;
|
||||
someValueChanged();
|
||||
}
|
||||
@@ -444,236 +481,274 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
||||
});
|
||||
}
|
||||
|
||||
formInputs[r][e] =
|
||||
Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
||||
if ((values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.isNotEmpty ==
|
||||
true &&
|
||||
(widget.items[r][e] as GeneratedFormTagInput)
|
||||
.showLabelWhenNotEmpty)
|
||||
Column(
|
||||
crossAxisAlignment:
|
||||
(widget.items[r][e] as GeneratedFormTagInput).alignment ==
|
||||
WrapAlignment.center
|
||||
? CrossAxisAlignment.center
|
||||
: CrossAxisAlignment.stretch,
|
||||
formInputs[r][e] = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if ((values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.isNotEmpty ==
|
||||
true &&
|
||||
(widget.items[r][e] as GeneratedFormTagInput)
|
||||
.showLabelWhenNotEmpty)
|
||||
Column(
|
||||
crossAxisAlignment:
|
||||
(widget.items[r][e] as GeneratedFormTagInput).alignment ==
|
||||
WrapAlignment.center
|
||||
? CrossAxisAlignment.center
|
||||
: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(widget.items[r][e].label),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
),
|
||||
Wrap(
|
||||
alignment:
|
||||
(widget.items[r][e] as GeneratedFormTagInput).alignment,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
Text(widget.items[r][e].label),
|
||||
const SizedBox(
|
||||
height: 8,
|
||||
),
|
||||
],
|
||||
),
|
||||
Wrap(
|
||||
alignment:
|
||||
(widget.items[r][e] as GeneratedFormTagInput).alignment,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
// (values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
// ?.isEmpty ==
|
||||
// true
|
||||
// ? Text(
|
||||
// (widget.items[r][e] as GeneratedFormTagInput)
|
||||
// .emptyMessage,
|
||||
// )
|
||||
// : const SizedBox.shrink(),
|
||||
...(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.entries
|
||||
.map((e2) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: ChoiceChip(
|
||||
label: Text(e2.key),
|
||||
backgroundColor: Color(e2.value.key).withAlpha(50),
|
||||
selectedColor: Color(e2.value.key),
|
||||
visualDensity: VisualDensity.compact,
|
||||
selected: e2.value.value,
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
(values[fieldKey] as Map<String,
|
||||
MapEntry<int, bool>>)[e2.key] =
|
||||
MapEntry(
|
||||
(values[fieldKey] as Map<String,
|
||||
MapEntry<int, bool>>)[e2.key]!
|
||||
.key,
|
||||
value);
|
||||
if ((widget.items[r][e]
|
||||
as GeneratedFormTagInput)
|
||||
.singleSelect &&
|
||||
value == true) {
|
||||
for (var key in (values[fieldKey]
|
||||
as Map<String, MapEntry<int, bool>>)
|
||||
.keys) {
|
||||
if (key != e2.key) {
|
||||
(values[fieldKey] as Map<
|
||||
String,
|
||||
MapEntry<int,
|
||||
bool>>)[key] = MapEntry(
|
||||
(values[fieldKey] as Map<String,
|
||||
MapEntry<int, bool>>)[key]!
|
||||
.key,
|
||||
false);
|
||||
// (values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
// ?.isEmpty ==
|
||||
// true
|
||||
// ? Text(
|
||||
// (widget.items[r][e] as GeneratedFormTagInput)
|
||||
// .emptyMessage,
|
||||
// )
|
||||
// : const SizedBox.shrink(),
|
||||
...(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.entries
|
||||
.map((e2) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 4,
|
||||
),
|
||||
child: ChoiceChip(
|
||||
label: Text(e2.key),
|
||||
backgroundColor: Color(
|
||||
e2.value.key,
|
||||
).withAlpha(50),
|
||||
selectedColor: Color(e2.value.key),
|
||||
visualDensity: VisualDensity.compact,
|
||||
selected: e2.value.value,
|
||||
onSelected: (value) {
|
||||
setState(() {
|
||||
(values[fieldKey]
|
||||
as Map<String, MapEntry<int, bool>>)[e2
|
||||
.key] = MapEntry(
|
||||
(values[fieldKey]
|
||||
as Map<
|
||||
String,
|
||||
MapEntry<int, bool>
|
||||
>)[e2.key]!
|
||||
.key,
|
||||
value,
|
||||
);
|
||||
if ((widget.items[r][e]
|
||||
as GeneratedFormTagInput)
|
||||
.singleSelect &&
|
||||
value == true) {
|
||||
for (var key
|
||||
in (values[fieldKey]
|
||||
as Map<
|
||||
String,
|
||||
MapEntry<int, bool>
|
||||
>)
|
||||
.keys) {
|
||||
if (key != e2.key) {
|
||||
(values[fieldKey]
|
||||
as Map<
|
||||
String,
|
||||
MapEntry<int, bool>
|
||||
>)[key] = MapEntry(
|
||||
(values[fieldKey]
|
||||
as Map<
|
||||
String,
|
||||
MapEntry<int, bool>
|
||||
>)[key]!
|
||||
.key,
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
someValueChanged();
|
||||
});
|
||||
},
|
||||
));
|
||||
}) ??
|
||||
[const SizedBox.shrink()],
|
||||
(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.values
|
||||
.where((e) => e.value)
|
||||
.length ==
|
||||
1
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
var temp = values[fieldKey]
|
||||
as Map<String, MapEntry<int, bool>>;
|
||||
// get selected category str where bool is true
|
||||
final oldEntry = temp.entries
|
||||
.firstWhere((entry) => entry.value.value);
|
||||
// generate new color, ensure it is not the same
|
||||
int newColor = oldEntry.value.key;
|
||||
while (oldEntry.value.key == newColor) {
|
||||
newColor = generateRandomLightColor().value;
|
||||
}
|
||||
// Update entry with new color, remain selected
|
||||
temp.update(oldEntry.key,
|
||||
(old) => MapEntry(newColor, old.value));
|
||||
values[fieldKey] = temp;
|
||||
someValueChanged();
|
||||
});
|
||||
},
|
||||
icon: const Icon(Icons.format_color_fill_rounded),
|
||||
visualDensity: VisualDensity.compact,
|
||||
tooltip: tr('colour'),
|
||||
))
|
||||
: const SizedBox.shrink(),
|
||||
(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.values
|
||||
.where((e) => e.value)
|
||||
.isNotEmpty ==
|
||||
true
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
fn() {
|
||||
someValueChanged();
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
}) ??
|
||||
[const SizedBox.shrink()],
|
||||
(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.values
|
||||
.where((e) => e.value)
|
||||
.length ==
|
||||
1
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
var temp = values[fieldKey]
|
||||
as Map<String, MapEntry<int, bool>>;
|
||||
temp.removeWhere((key, value) => value.value);
|
||||
var temp =
|
||||
values[fieldKey]
|
||||
as Map<String, MapEntry<int, bool>>;
|
||||
// get selected category str where bool is true
|
||||
final oldEntry = temp.entries.firstWhere(
|
||||
(entry) => entry.value.value,
|
||||
);
|
||||
// generate new color, ensure it is not the same
|
||||
int newColor = oldEntry.value.key;
|
||||
while (oldEntry.value.key == newColor) {
|
||||
newColor = generateRandomLightColor().value;
|
||||
}
|
||||
// Update entry with new color, remain selected
|
||||
temp.update(
|
||||
oldEntry.key,
|
||||
(old) => MapEntry(newColor, old.value),
|
||||
);
|
||||
values[fieldKey] = temp;
|
||||
someValueChanged();
|
||||
});
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.format_color_fill_rounded),
|
||||
visualDensity: VisualDensity.compact,
|
||||
tooltip: tr('colour'),
|
||||
),
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.values
|
||||
.where((e) => e.value)
|
||||
.isNotEmpty ==
|
||||
true
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: IconButton(
|
||||
onPressed: () {
|
||||
fn() {
|
||||
setState(() {
|
||||
var temp =
|
||||
values[fieldKey]
|
||||
as Map<String, MapEntry<int, bool>>;
|
||||
temp.removeWhere((key, value) => value.value);
|
||||
values[fieldKey] = temp;
|
||||
someValueChanged();
|
||||
});
|
||||
}
|
||||
|
||||
if ((widget.items[r][e] as GeneratedFormTagInput)
|
||||
.deleteConfirmationMessage !=
|
||||
null) {
|
||||
var message =
|
||||
(widget.items[r][e] as GeneratedFormTagInput)
|
||||
.deleteConfirmationMessage!;
|
||||
showDialog<Map<String, dynamic>?>(
|
||||
if ((widget.items[r][e] as GeneratedFormTagInput)
|
||||
.deleteConfirmationMessage !=
|
||||
null) {
|
||||
var message =
|
||||
(widget.items[r][e]
|
||||
as GeneratedFormTagInput)
|
||||
.deleteConfirmationMessage!;
|
||||
showDialog<Map<String, dynamic>?>(
|
||||
context: context,
|
||||
builder: (BuildContext ctx) {
|
||||
return GeneratedFormModal(
|
||||
title: message.key,
|
||||
message: message.value,
|
||||
items: const []);
|
||||
}).then((value) {
|
||||
if (value != null) {
|
||||
fn();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
fn();
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.remove),
|
||||
visualDensity: VisualDensity.compact,
|
||||
tooltip: tr('remove'),
|
||||
))
|
||||
: const SizedBox.shrink(),
|
||||
(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.isEmpty ==
|
||||
true
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: TextButton.icon(
|
||||
onPressed: onAddPressed,
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text(
|
||||
title: message.key,
|
||||
message: message.value,
|
||||
items: const [],
|
||||
);
|
||||
},
|
||||
).then((value) {
|
||||
if (value != null) {
|
||||
fn();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
fn();
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.remove),
|
||||
visualDensity: VisualDensity.compact,
|
||||
tooltip: tr('remove'),
|
||||
),
|
||||
)
|
||||
: const SizedBox.shrink(),
|
||||
(values[fieldKey] as Map<String, MapEntry<int, bool>>?)
|
||||
?.isEmpty ==
|
||||
true
|
||||
? Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: TextButton.icon(
|
||||
onPressed: onAddPressed,
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text(
|
||||
(widget.items[r][e] as GeneratedFormTagInput)
|
||||
.label),
|
||||
))
|
||||
: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: IconButton(
|
||||
onPressed: onAddPressed,
|
||||
icon: const Icon(Icons.add),
|
||||
visualDensity: VisualDensity.compact,
|
||||
tooltip: tr('add'),
|
||||
)),
|
||||
],
|
||||
)
|
||||
]);
|
||||
.label,
|
||||
),
|
||||
),
|
||||
)
|
||||
: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 4),
|
||||
child: IconButton(
|
||||
onPressed: onAddPressed,
|
||||
icon: const Icon(Icons.add),
|
||||
visualDensity: VisualDensity.compact,
|
||||
tooltip: tr('add'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
} else if (widget.items[r][e] is GeneratedFormSubForm) {
|
||||
List<Widget> subformColumn = [];
|
||||
var compact = (widget.items[r][e] as GeneratedFormSubForm)
|
||||
.items
|
||||
.length ==
|
||||
1 &&
|
||||
var compact =
|
||||
(widget.items[r][e] as GeneratedFormSubForm).items.length == 1 &&
|
||||
(widget.items[r][e] as GeneratedFormSubForm).items[0].length == 1;
|
||||
for (int i = 0; i < values[fieldKey].length; i++) {
|
||||
var internalFormKey = ValueKey(generateRandomNumber(
|
||||
var internalFormKey = ValueKey(
|
||||
generateRandomNumber(
|
||||
values[fieldKey].length,
|
||||
seed2: i,
|
||||
seed3: forceUpdateKeyCount));
|
||||
subformColumn.add(Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (!compact)
|
||||
const SizedBox(
|
||||
height: 16,
|
||||
seed3: forceUpdateKeyCount,
|
||||
),
|
||||
);
|
||||
subformColumn.add(
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (!compact) const SizedBox(height: 16),
|
||||
if (!compact)
|
||||
Text(
|
||||
'${(widget.items[r][e] as GeneratedFormSubForm).label} (${i + 1})',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
GeneratedForm(
|
||||
key: internalFormKey,
|
||||
items:
|
||||
cloneFormItems(
|
||||
(widget.items[r][e] as GeneratedFormSubForm)
|
||||
.items,
|
||||
)
|
||||
.map(
|
||||
(x) => x.map((y) {
|
||||
y.defaultValue = values[fieldKey]?[i]?[y.key];
|
||||
y.key = '${y.key.toString()},$internalFormKey';
|
||||
return y;
|
||||
}).toList(),
|
||||
)
|
||||
.toList(),
|
||||
onValueChanges: (values, valid, isBuilding) {
|
||||
values = values.map(
|
||||
(key, value) => MapEntry(key.split(',')[0], value),
|
||||
);
|
||||
if (valid) {
|
||||
this.values[fieldKey]?[i] = values;
|
||||
}
|
||||
someValueChanged(
|
||||
isBuilding: isBuilding,
|
||||
forceInvalid: !valid,
|
||||
);
|
||||
},
|
||||
),
|
||||
if (!compact)
|
||||
Text(
|
||||
'${(widget.items[r][e] as GeneratedFormSubForm).label} (${i + 1})',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
GeneratedForm(
|
||||
key: internalFormKey,
|
||||
items: cloneFormItems(
|
||||
(widget.items[r][e] as GeneratedFormSubForm).items)
|
||||
.map((x) => x.map((y) {
|
||||
y.defaultValue = values[fieldKey]?[i]?[y.key];
|
||||
y.key = '${y.key.toString()},$internalFormKey';
|
||||
return y;
|
||||
}).toList())
|
||||
.toList(),
|
||||
onValueChanges: (values, valid, isBuilding) {
|
||||
values = values.map(
|
||||
(key, value) => MapEntry(key.split(',')[0], value));
|
||||
if (valid) {
|
||||
this.values[fieldKey]?[i] = values;
|
||||
}
|
||||
someValueChanged(
|
||||
isBuilding: isBuilding, forceInvalid: !valid);
|
||||
},
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton.icon(
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton.icon(
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor:
|
||||
Theme.of(context).colorScheme.error),
|
||||
foregroundColor: Theme.of(context).colorScheme.error,
|
||||
),
|
||||
onPressed: (values[fieldKey].length > 0)
|
||||
? () {
|
||||
var temp = List.from(values[fieldKey]);
|
||||
@@ -686,33 +761,40 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
||||
label: Text(
|
||||
'${(widget.items[r][e] as GeneratedFormSubForm).label} (${i + 1})',
|
||||
),
|
||||
icon: const Icon(
|
||||
Icons.delete_outline_rounded,
|
||||
))
|
||||
],
|
||||
)
|
||||
],
|
||||
));
|
||||
icon: const Icon(Icons.delete_outline_rounded),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
subformColumn.add(Padding(
|
||||
padding: const EdgeInsets.only(bottom: 0, top: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
subformColumn.add(
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 0, top: 8),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
values[fieldKey].add(getDefaultValuesFromFormItems(
|
||||
(widget.items[r][e] as GeneratedFormSubForm)
|
||||
.items));
|
||||
forceUpdateKeyCount++;
|
||||
someValueChanged();
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text((widget.items[r][e] as GeneratedFormSubForm)
|
||||
.label))),
|
||||
],
|
||||
onPressed: () {
|
||||
values[fieldKey].add(
|
||||
getDefaultValuesFromFormItems(
|
||||
(widget.items[r][e] as GeneratedFormSubForm).items,
|
||||
),
|
||||
);
|
||||
forceUpdateKeyCount++;
|
||||
someValueChanged();
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text(
|
||||
(widget.items[r][e] as GeneratedFormSubForm).label,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
);
|
||||
formInputs[r][e] = Column(children: subformColumn);
|
||||
}
|
||||
}
|
||||
@@ -726,38 +808,43 @@ class _GeneratedFormState extends State<GeneratedForm> {
|
||||
height: widget.items[rowInputs.key - 1][0] is GeneratedFormSwitch
|
||||
? 8
|
||||
: 25,
|
||||
)
|
||||
),
|
||||
]);
|
||||
}
|
||||
List<Widget> rowItems = [];
|
||||
rowInputs.value.asMap().entries.forEach((rowInput) {
|
||||
if (rowInput.key > 0) {
|
||||
rowItems.add(const SizedBox(
|
||||
width: 20,
|
||||
));
|
||||
rowItems.add(const SizedBox(width: 20));
|
||||
}
|
||||
rowItems.add(Expanded(
|
||||
rowItems.add(
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
rowInput.value,
|
||||
...widget.items[rowInputs.key][rowInput.key].belowWidgets
|
||||
])));
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
rowInput.value,
|
||||
...widget.items[rowInputs.key][rowInput.key].belowWidgets,
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
rows.add(rowItems);
|
||||
});
|
||||
|
||||
return Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
...rows.map((row) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [...row.map((e) => e)],
|
||||
))
|
||||
],
|
||||
));
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
...rows.map(
|
||||
(row) => Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [...row.map((e) => e)],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user