robiness / stage_craft

MIT License
5 stars 1 forks source link

Refactor configuration fields #6

Closed robiness closed 1 year ago

robiness commented 1 year ago

Each ConfigurationField now consists out of three components:

The first both are the ones used in the stage to represent a non-nullable value or a nullable value. The ConfigurationWidget is the actual widget shown to the user to adjust the values. It contains the shared logic of both FieldConfigurators.

This has a little boilerplate where i try to build a IntelliJ plugin for. Before that you can use this livetemplates to generate the basic structure:

ConfigurationWidget

/// Represents a nullable $TYPE$ parameter for a widget on a [WidgetStage].
class $TYPENAME$FieldConfiguratorNullable extends FieldConfigurator<$TYPE$?> {
  $TYPENAME$FieldConfiguratorNullable({
    required super.value,
    required super.name,
  });

  @override
  Widget build(BuildContext context) {
    return $TYPENAME$FieldConfigurationWidget(
      value: value,
      updateValue: updateValue,
    );
  }
}

/// Represents a $TYPE$ parameter for a widget on a [WidgetStage].
class $TYPENAME$FieldConfigurator extends FieldConfigurator<$TYPE$> {
  $TYPENAME$FieldConfigurator({
    required super.value,
    required super.name,
  });

  @override
  Widget build(BuildContext context) {
    return $TYPENAME$FieldConfigurationWidget(
      value: value,
      updateValue: (value) {
        updateValue(value ?? false);
      },
    );
  }
}

class $TYPENAME$FieldConfigurationWidget extends ConfigurationWidget<$TYPE$?> {
  const $TYPENAME$FieldConfigurationWidget({
    required super.value,
    required super.updateValue,
  });

  @override
  Widget build(BuildContext context) {
    // return youre field configurator here.
    // Use the value to set the initial value and updateValue to update the value.
  }
}

StatefulConfigurationWidget

/// Represents a nullable $TYPE$ parameter for a widget on a [WidgetStage].
class $CNAME$FieldConfiguratorNullable extends FieldConfigurator<$TYPE$?> {
  $CNAME$FieldConfiguratorNullable({
    required super.value,
    required super.name,
  });

  @override
  Widget build(BuildContext context) {
    return $CNAME$FieldConfigurationWidget(
      value: value,
      updateValue: updateValue,
    );
  }
}

/// Represents a $TYPE$ parameter for a widget on a [WidgetStage].
class $CNAME$FieldConfigurator extends FieldConfigurator<$TYPE$> {
  $CNAME$FieldConfigurator({
    required super.value,
    required super.name,
  });

  @override
  Widget build(BuildContext context) {
    return $CNAME$FieldConfigurationWidget(
      value: value,
      updateValue: (value) {
        updateValue(value ?? '');
      },
    );
  }
}

class $CNAME$FieldConfigurationWidget extends StatefulConfigurationWidget<$TYPE$?> {
  const $CNAME$FieldConfigurationWidget({
    super.key,
    required super.value,
    required super.updateValue,
  });

  @override
  State<$CNAME$FieldConfigurationWidget> createState() => _$CNAME$FieldConfigurationWidgetState();
}

class _$CNAME$FieldConfigurationWidgetState extends State<$CNAME$FieldConfigurationWidget> {
  // for example
  late final TextEditingController _controller = TextEditingController(text: widget.value);

  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration: const InputDecoration(
        border: OutlineInputBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(8),
          ),
        ),
      ),
      controller: _controller,
      onChanged: widget.updateValue,
    );
  }
}