Skip to content

Commit a3510a4

Browse files
Merge pull request #1069 from danvick/765
Added shape property to FormBuilderChoiceChip
2 parents ccb4ce2 + cc1bd61 commit a3510a4

File tree

2 files changed

+94
-25
lines changed

2 files changed

+94
-25
lines changed

packages/flutter_form_builder/lib/src/fields/form_builder_choice_chips.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,21 @@ class FormBuilderChoiceChip<T> extends FormBuilderField<T> {
5353
/// The default is [Colors.black].
5454
final Color? shadowColor;
5555

56-
/// The [ShapeBorder] to draw around the chip.
56+
/// The [OutlinedBorder] to draw around the chip.
5757
///
58-
/// Defaults to the shape in the ambient [ChipThemeData].
58+
/// Defaults to the shape in the ambient [ChipThemeData]. If the theme
59+
/// shape resolves to null, the default is [StadiumBorder].
60+
///
61+
/// This shape is combined with [side] to create a shape decorated with an
62+
/// outline. If it is a [MaterialStateOutlinedBorder],
63+
/// [MaterialStateProperty.resolve] is used for the following
64+
/// [MaterialState]s:
65+
///
66+
/// * [MaterialState.disabled].
67+
/// * [MaterialState.selected].
68+
/// * [MaterialState.hovered].
69+
/// * [MaterialState.focused].
70+
/// * [MaterialState.pressed].
5971
final OutlinedBorder? shape;
6072

6173
/// Configures the minimum size of the tap target.
@@ -311,6 +323,7 @@ class FormBuilderChoiceChip<T> extends FormBuilderField<T> {
311323
for (FormBuilderChipOption<T> option in options)
312324
ChoiceChip(
313325
label: option,
326+
shape: shape,
314327
selected: field.value == option.value,
315328
onSelected: state.enabled
316329
? (selected) {

packages/flutter_form_builder/test/form_builder_choice_chips_test.dart

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,84 @@ import 'package:flutter_form_builder/flutter_form_builder.dart';
66
import 'form_builder_tester.dart';
77

88
void main() {
9-
testWidgets('FormBuilderChoiceChip -- 1,3', (WidgetTester tester) async {
10-
const widgetName = 'cc1';
11-
final testWidget = FormBuilderChoiceChip<int>(
12-
shouldRequestFocus: false,
13-
name: widgetName,
14-
options: const [
15-
FormBuilderChipOption(key: ValueKey('1'), value: 1),
16-
FormBuilderChipOption(key: ValueKey('2'), value: 2),
17-
FormBuilderChipOption(key: ValueKey('3'), value: 3),
18-
],
19-
);
20-
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
21-
22-
expect(formSave(), isTrue);
23-
expect(formValue(widgetName), isNull);
24-
await tester.tap(find.byKey(const ValueKey('1')));
25-
await tester.pumpAndSettle();
26-
expect(formSave(), isTrue);
27-
expect(formValue(widgetName), equals(1));
28-
await tester.tap(find.byKey(const ValueKey('3')));
29-
await tester.pumpAndSettle();
30-
expect(formSave(), isTrue);
31-
expect(formValue(widgetName), equals(3));
9+
group('FormBuilderChoiceChip --', () {
10+
testWidgets('basic', (WidgetTester tester) async {
11+
const widgetName = 'cc1';
12+
13+
final testWidget = FormBuilderChoiceChip<int>(
14+
shouldRequestFocus: false,
15+
name: widgetName,
16+
options: const [
17+
FormBuilderChipOption(key: ValueKey('1'), value: 1),
18+
FormBuilderChipOption(key: ValueKey('2'), value: 2),
19+
FormBuilderChipOption(key: ValueKey('3'), value: 3),
20+
],
21+
);
22+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
23+
24+
expect(formSave(), isTrue);
25+
expect(formValue<int?>(widgetName), isNull);
26+
await tester.tap(find.byKey(const ValueKey('1')));
27+
await tester.pumpAndSettle();
28+
expect(formSave(), isTrue);
29+
expect(formValue<int?>(widgetName), equals(1));
30+
await tester.tap(find.byKey(const ValueKey('3')));
31+
await tester.pumpAndSettle();
32+
expect(formSave(), isTrue);
33+
expect(formValue<int?>(widgetName), equals(3));
34+
});
35+
});
36+
group('initial value -', () {
37+
testWidgets('to FormBuilder', (WidgetTester tester) async {
38+
const widgetName = 'cc2';
39+
40+
final testWidget = FormBuilderChoiceChip<int>(
41+
shouldRequestFocus: false,
42+
name: widgetName,
43+
options: const [
44+
FormBuilderChipOption(key: ValueKey('1'), value: 1),
45+
FormBuilderChipOption(key: ValueKey('2'), value: 2),
46+
FormBuilderChipOption(key: ValueKey('3'), value: 3),
47+
],
48+
);
49+
await tester.pumpWidget(buildTestableFieldWidget(
50+
testWidget,
51+
initialValue: {widgetName: 1},
52+
));
53+
54+
await tester.ensureVisible(find.byKey(const ValueKey('1')));
55+
expect(formInstantValue(widgetName), equals(1));
56+
expect(formSave(), isTrue);
57+
expect(formValue<int?>(widgetName), equals(1));
58+
await tester.tap(find.byKey(const ValueKey('3')));
59+
await tester.pumpAndSettle();
60+
expect(formSave(), isTrue);
61+
expect(formValue<int?>(widgetName), equals(3));
62+
});
63+
testWidgets('to Widget', (WidgetTester tester) async {
64+
const widgetName = 'cc3';
65+
66+
final testWidget = FormBuilderChoiceChip<int>(
67+
shouldRequestFocus: false,
68+
name: widgetName,
69+
initialValue: 1,
70+
options: const [
71+
FormBuilderChipOption(key: ValueKey('1'), value: 1),
72+
FormBuilderChipOption(key: ValueKey('2'), value: 2),
73+
FormBuilderChipOption(key: ValueKey('3'), value: 3),
74+
],
75+
);
76+
await tester.pumpWidget(buildTestableFieldWidget(testWidget));
77+
await tester.pumpAndSettle();
78+
79+
await tester.ensureVisible(find.byKey(const ValueKey('1')));
80+
expect(formInstantValue(widgetName), equals(1));
81+
expect(formSave(), isTrue);
82+
expect(formValue<int?>(widgetName), equals(1));
83+
await tester.tap(find.byKey(const ValueKey('3')));
84+
await tester.pumpAndSettle();
85+
expect(formSave(), isTrue);
86+
expect(formValue<int?>(widgetName), equals(3));
87+
});
3288
});
3389
}

0 commit comments

Comments
 (0)