Skip to content
Snippets Groups Projects
Commit 54fd229d authored by Benoît Harrault's avatar Benoît Harrault
Browse files

Add parameter type "array"

parent 5a62af8c
No related branches found
No related tags found
1 merge request!26Resolve "Add parameter type "array""
Pipeline #7836 passed
This commit is part of merge request !26. Comments created here will be created in the context of that merge request.
## 1.0.7
- Allow "array" parameter type
## 1.0.6
- Allow additional content in about page
......
......@@ -37,6 +37,7 @@ class ApplicationSettingsParameter {
final String code;
final bool displayedOnTop;
final List<ApplicationSettingsParameterItemValue> values;
final bool allowMultipleValues;
final int itemsPerLine;
final Widget Function({
required BuildContext context,
......@@ -52,6 +53,7 @@ class ApplicationSettingsParameter {
required this.code,
this.displayedOnTop = true,
required this.values,
this.allowMultipleValues = false,
this.itemsPerLine = 0,
this.builder,
this.customPainter,
......@@ -63,7 +65,15 @@ class ApplicationSettingsParameter {
return values.map((ApplicationSettingsParameterItemValue item) => item.value).toList();
}
String get defaultValue => values.firstWhere((element) => element.isDefault).value;
String get defaultValue => allowMultipleValues
? values
.where((itemValue) => itemValue.isDefault)
.toList()
.map((itemDefaultValue) => itemDefaultValue.value)
.join(',')
: values.firstWhere((itemValue) => itemValue.isDefault).value;
List<String> get defaultValues => defaultValue.split(',');
factory ApplicationSettingsParameter.empty() {
return ApplicationSettingsParameter(
......@@ -82,8 +92,8 @@ class ApplicationSettingsParameter {
required double size,
required VoidCallback? onPressed,
}) {
final Color buttonColorActive = (onPressed != null) ? Colors.blue : Colors.grey;
final Color buttonColorInactive = Theme.of(context).colorScheme.surface;
final Color colorAvailable = Theme.of(context).colorScheme.surface;
final Color colorSelected = (onPressed != null) ? Colors.blue : Colors.grey;
const double buttonBorderWidth = 4.0;
const double buttonBorderRadius = 12.0;
......@@ -91,9 +101,9 @@ class ApplicationSettingsParameter {
BlocProvider.of<ActivitySettingsCubit>(context);
final String currentValue = activitySettingsCubit.get(code);
final bool isSelected = (currentValue == itemValue.value);
final bool isSelected = (currentValue.split(',').contains(itemValue.value));
final Color buttonColor = isSelected ? buttonColorActive : buttonColorInactive;
final Color color = isSelected ? colorSelected : colorAvailable;
Widget content = SizedBox.shrink();
......@@ -127,10 +137,10 @@ class ApplicationSettingsParameter {
return Container(
decoration: BoxDecoration(
color: buttonColor,
color: color,
borderRadius: BorderRadius.circular(buttonBorderRadius),
border: Border.all(
color: buttonColor,
color: color,
width: buttonBorderWidth,
),
),
......
......@@ -26,6 +26,10 @@ class ActivitySettings {
String get(String code) {
if (values.keys.contains(code)) {
if (appConfig.getFromCode(code).allowMultipleValues) {
return getAsArray(code).join(',');
}
if (appConfig.getFromCode(code).allowedValues.contains(values[code])) {
return values[code] ?? appConfig.getFromCode(code).defaultValue;
}
......@@ -54,6 +58,19 @@ class ActivitySettings {
}
}
List<String> getAsArray(String code) {
final List<String> legitValues = [];
if (values[code] != null) {
values[code]?.split(',').forEach((candidateValue) {
if (appConfig.getFromCode(code).allowedValues.contains(candidateValue)) {
legitValues.add(candidateValue);
}
});
}
return legitValues;
}
void dump() {
printlog('$ActivitySettings:');
values.forEach((code, value) {
......
......@@ -153,8 +153,12 @@ class PageParameters extends StatelessWidget {
size: itemWidth,
onPressed: isEnabled
? () {
if (parameter.allowMultipleValues) {
activitySettingsCubit.toggle(parameter.code, item.value);
} else {
activitySettingsCubit.set(parameter.code, item.value);
}
}
: null,
),
);
......
......@@ -35,7 +35,7 @@ class ActivitySettingsCubit extends HydratedCubit<ActivitySettingsState> {
}
void set(String code, String value) {
Map<String, String> values = state.settings.values;
final Map<String, String> values = state.settings.values;
values[code] = value;
......@@ -44,6 +44,17 @@ class ActivitySettingsCubit extends HydratedCubit<ActivitySettingsState> {
);
}
void toggle(String code, String value) {
final List<String> currentValues = state.settings.values[code]?.split(',') ?? [];
if (currentValues.contains(value) == true) {
currentValues.removeWhere((candidate) => value == candidate);
} else {
currentValues.add(value);
}
set(code, currentValues.join(','));
}
@override
ActivitySettingsState? fromJson(Map<String, dynamic> json) {
Map<String, String> values = {};
......
......@@ -3,7 +3,7 @@ description: "Flutter custom toolbox for org.benoitharrault.* projects."
publish_to: https://pub.harrault.fr/
version: 1.0.6
version: 1.0.7
homepage: https://git.harrault.fr/android/flutter-toolbox
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment