Skip to content
Snippets Groups Projects
Select Git revision
  • b8164a50489ba981ea57d9f02e2334f09cb8c6a7
  • master default protected
  • 21-add-onlongpress-with-popup-on-parameters
  • 23-center-vertically-buttons
  • 30-highlight-bin-when-selecting-disabled-item
  • 1.0.7 protected
  • 1.0.6 protected
  • 1.0.5 protected
  • 1.0.4 protected
  • 1.0.3 protected
  • 1.0.2 protected
  • 1.0.0 protected
  • 0.9.1 protected
  • 0.9.0 protected
  • 0.8.4 protected
  • 0.8.3 protected
  • 0.8.2 protected
  • 0.8.1 protected
  • 0.8.0 protected
  • 0.7.0 protected
  • 0.6.1 protected
  • 0.6.0 protected
  • 0.5.0 protected
  • 0.4.0 protected
  • 0.3.0 protected
25 results

application_config_definition.dart

Blame
  • application_config_definition.dart 4.45 KiB
    import 'package:flutter/material.dart';
    
    import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
    
    class ApplicationConfigDefinition {
      final String appTitle;
      final List<ApplicationSettingsParameter> activitySettings;
      final void Function(BuildContext context) startNewActivity;
      final void Function(BuildContext context) deleteCurrentActivity;
      final void Function(BuildContext context) resumeActivity;
    
      const ApplicationConfigDefinition({
        required this.appTitle,
        required this.activitySettings,
        required this.startNewActivity,
        required this.deleteCurrentActivity,
        required this.resumeActivity,
      });
    
      ApplicationSettingsParameter getFromCode(String paramCode) {
        final List<ApplicationSettingsParameter> settings = activitySettings;
    
        return settings.where((setting) => setting.code == paramCode).firstOrNull ??
            ApplicationSettingsParameter.empty();
      }
    }
    
    class ApplicationSettingsParameter {
      final String code;
      final bool displayedOnTop;
      final List<ApplicationSettingsParameterItemValue> values;
      final Widget Function({
        required BuildContext context,
        required double size,
        required ApplicationSettingsParameterItemValue itemValue,
        required VoidCallback onPressed,
      })? builder;
      final CustomPainter Function(BuildContext context, String value)? customPainter;
      final int Function(String value)? intValueGetter;
      final Color Function(String value)? colorGetter;
    
      const ApplicationSettingsParameter({
        required this.code,
        this.displayedOnTop = true,
        required this.values,
        this.builder,
        this.customPainter,
        this.colorGetter,
        this.intValueGetter,
      });
    
      List<String> get allowedValues {
        return values.map((ApplicationSettingsParameterItemValue item) => item.value).toList();
      }
    
      String get defaultValue => values.firstWhere((element) => element.isDefault).value;
    
      factory ApplicationSettingsParameter.empty() {
        return ApplicationSettingsParameter(
          code: '',
          values: [
            ApplicationSettingsParameterItemValue.empty(),
          ],
          intValueGetter: (value) => 0,
        );
      }
    
      Widget buildParameterItem({
        required BuildContext context,
        required ApplicationSettingsParameter parameter,
        required ApplicationSettingsParameterItemValue itemValue,
        required double size,
        required VoidCallback? onPressed,
      }) {
        final Color buttonColorActive = (onPressed != null) ? Colors.blue : Colors.grey;
        final Color buttonColorInactive = Theme.of(context).colorScheme.surface;
        const double buttonBorderWidth = 4.0;
        const double buttonBorderRadius = 12.0;
    
        final ActivitySettingsCubit activitySettingsCubit =
            BlocProvider.of<ActivitySettingsCubit>(context);
        final String currentValue = activitySettingsCubit.get(code);
    
        final bool isSelected = (currentValue == itemValue.value);
    
        final Color buttonColor = isSelected ? buttonColorActive : buttonColorInactive;
    
        Widget content = SizedBox.shrink();
    
        if (builder != null) {
          content = builder!(
            context: context,
            size: size,
            itemValue: itemValue,
            onPressed: onPressed ?? () {},
          );
        } else {
          if (customPainter != null) {
            content = StyledButton(
              color: itemValue.color ?? Colors.grey,
              onPressed: onPressed ?? () {},
              child: CustomPaint(
                size: Size(size, size),
                willChange: false,
                painter: customPainter!(context, itemValue.value),
                isComplex: true,
              ),
            );
          } else {
            content = StyledButton.text(
              color: itemValue.color ?? Colors.grey,
              caption: itemValue.text ?? itemValue.value,
              onPressed: onPressed ?? () {},
            );
          }
        }
    
        return Container(
          decoration: BoxDecoration(
            color: buttonColor,
            borderRadius: BorderRadius.circular(buttonBorderRadius),
            border: Border.all(
              color: buttonColor,
              width: buttonBorderWidth,
            ),
          ),
          child: content,
        );
      }
    }
    
    class ApplicationSettingsParameterItemValue {
      final String value;
      final bool isDefault;
      final Color? color;
      final String? text;
    
      const ApplicationSettingsParameterItemValue({
        required this.value,
        this.isDefault = false,
        this.color,
        this.text,
      });
    
      factory ApplicationSettingsParameterItemValue.empty() {
        return ApplicationSettingsParameterItemValue(
          value: '',
          isDefault: true,
        );
      }
    
      @override
      String toString() {
        return value;
      }
    }