Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add padding field configurator #8

Merged
merged 6 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion example/lib/stage_data/my_other_widget_stage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'package:flutter/material.dart';
import 'package:widget_stage/widget_stage.dart';

class MyOtherWidgetStageData extends WidgetStageData {
MyOtherWidgetStageData() : _text = StringFieldConfigurator(value: 'MyOtherWidget', name: 'text');
MyOtherWidgetStageData()
: _text = StringFieldConfigurator(value: 'MyOtherWidget', name: 'text'),
_padding = PaddingFieldConfiguratorNullable(value: null, name: 'padding');

final StringFieldConfigurator _text;
final PaddingFieldConfiguratorNullable _padding;

@override
String get name => 'MyOtherWidget';
Expand All @@ -14,13 +17,15 @@ class MyOtherWidgetStageData extends WidgetStageData {
Widget widgetBuilder(BuildContext context) {
return MyOtherWidget(
text: _text.value,
padding: _padding.value,
);
}

@override
List<FieldConfigurator> get fieldConfigurators {
return [
_text,
_padding,
];
}
}
4 changes: 3 additions & 1 deletion example/lib/widgets/my_other_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ class MyOtherWidget extends StatelessWidget {
const MyOtherWidget({
super.key,
required this.text,
this.padding,
});

final String text;
final EdgeInsetsGeometry? padding;

@override
Widget build(BuildContext context) {
Expand All @@ -15,7 +17,7 @@ class MyOtherWidget extends StatelessWidget {
border: Border.all(width: 2),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
padding: padding ?? const EdgeInsets.all(8.0),
child: Row(
children: [
Text(text),
Expand Down
1 change: 1 addition & 0 deletions lib/src/field_configurators/field_configurators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export 'color_field_configurator.dart';
export 'double_field_configurator.dart';
export 'enum_field_configurator.dart';
export 'int_field_configurator.dart';
export 'padding_field_configurator.dart';
export 'string_field_configurator.dart';
177 changes: 177 additions & 0 deletions lib/src/field_configurators/padding_field_configurator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:widget_stage/src/field_configurators/field_configurator_widget.dart';
import 'package:widget_stage/src/widget_stage.dart';

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

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

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

@override
Widget build(BuildContext context) {
return PaddingFieldConfigurationWidget(
value: value,
updateValue: (value) => updateValue(value ?? EdgeInsets.zero),
);
}
}

class PaddingFieldConfigurationWidget extends StatefulConfigurationWidget<EdgeInsets?> {
const PaddingFieldConfigurationWidget({
super.key,
required super.value,
required super.updateValue,
});

@override
State<PaddingFieldConfigurationWidget> createState() => _PaddingFieldConfigurationWidgetState();
}

class _PaddingFieldConfigurationWidgetState extends State<PaddingFieldConfigurationWidget> {
@override
Widget build(BuildContext context) {
final padding = widget.value ?? EdgeInsets.zero;
return Column(
children: [
SizedBox(
width: 80,
child: _PaddingField(
value: widget.value?.top,
onChanged: (value) {
setState(() {
final newValue = padding.copyWith(top: value);
widget.updateValue(newValue);
});
},
label: 'top',
),
),
const SizedBox(height: 8.0),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
width: 60,
child: _PaddingField(
value: widget.value?.left,
onChanged: (value) {
setState(() {
final newValue = padding.copyWith(left: value);
widget.updateValue(newValue);
});
},
label: 'left',
),
),
const SizedBox(width: 8.0),
SizedBox(
width: 60,
child: _PaddingField(
value: widget.value?.right,
onChanged: (value) {
setState(() {
final newValue = padding.copyWith(right: value);
widget.updateValue(newValue);
});
},
label: 'right',
),
),
],
),
const SizedBox(height: 8.0),
SizedBox(
width: 80,
child: _PaddingField(
value: widget.value?.bottom,
onChanged: (value) {
setState(() {
final newValue = padding.copyWith(bottom: value);
widget.updateValue(newValue);
});
},
label: 'bottom',
),
),
],
);
}
}

class _PaddingField extends StatefulWidget {
const _PaddingField({
required this.onChanged,
required this.label,
required this.value,
});

final String label;

final double? value;
final void Function(double) onChanged;

@override
State<_PaddingField> createState() => _PaddingFieldState();
}

class _PaddingFieldState extends State<_PaddingField> {
late TextEditingController _controller;

@override
void initState() {
super.initState();
_controller = TextEditingController(text: widget.value?.toString());
}

@override
void didUpdateWidget(covariant _PaddingField oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.value == null && oldWidget.value != null) {
setState(() {
_controller.clear();
});
}
}

@override
Widget build(BuildContext context) {
return TextField(
inputFormatters: <TextInputFormatter>[
// Allow only digits, dot and 1 decimal
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d?')),
],
decoration: InputDecoration(
labelText: widget.label,
isDense: true,
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(8),
),
),
),
controller: _controller,
onChanged: (value) {
widget.onChanged.call(double.tryParse(value) ?? 0);
},
);
}
}