Skip to content

Commit

Permalink
Merge pull request #121 from wwt/Issue120
Browse files Browse the repository at this point in the history
Added fix for CheckboxListTiles not initially being checked when Checkbox other value is provided and SurveyWidget is built using a builder
  • Loading branch information
goxiaoy committed Nov 1, 2023
2 parents c49efbd + b3f8f85 commit 9ff2b29
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/flutter_survey_js/lib/ui/elements/checkbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class _CheckBoxElementState extends State<CheckBoxElement> {
otherController.setOtherValue(otherValueControl.value.toString());
}
}
});
}).then((value) => setState(() {}));
}

AbstractControl<Object?>? findPossibleOtherValueControl(
Expand Down Expand Up @@ -224,6 +224,7 @@ class _CheckBoxElementState extends State<CheckBoxElement> {
if (widget.element.showOtherItem ?? false) {
String? text = otherController.getOtherLocaledText(context);
list.add(CheckboxListTile(
key: const Key('other-checkbox-list-tile'),
value: otherController.showOther,
title: Text(text),
onChanged: (v) {
Expand Down
106 changes: 96 additions & 10 deletions packages/flutter_survey_js/test/questions/check_boxes_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_survey_js/flutter_survey_js.dart' as s;
import 'package:flutter_survey_js/generated/l10n.dart';
import 'package:flutter_survey_js/flutter_survey_js.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:reactive_forms/reactive_forms.dart';

Expand Down Expand Up @@ -35,7 +35,7 @@ void main() {
]
};
test("Serialize Deserialize Survey", () {
surveyFromJson(json);
s.surveyFromJson(json);
});

const extended = {
Expand Down Expand Up @@ -73,7 +73,7 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
home: Material(
child: SurveyWidget(survey: surveyFromJson(extended)!),
child: s.SurveyWidget(survey: s.surveyFromJson(extended)!),
),
),
);
Expand All @@ -99,7 +99,7 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
home: Material(
child: SurveyWidget(survey: surveyFromJson(extended)!),
child: s.SurveyWidget(survey: s.surveyFromJson(extended)!),
),
),
);
Expand Down Expand Up @@ -135,8 +135,8 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
home: Material(
child: SurveyWidget(
survey: surveyFromJson(extended)!,
child: s.SurveyWidget(
survey: s.surveyFromJson(extended)!,
answer: const {
"warships": ["UNN Thomas Prince"]
},
Expand Down Expand Up @@ -177,8 +177,8 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
home: Material(
child: SurveyWidget(
survey: surveyFromJson(extended)!,
child: s.SurveyWidget(
survey: s.surveyFromJson(extended)!,
answer: const {
"warships": ["UNN Thomas Prince"]
},
Expand Down Expand Up @@ -210,7 +210,7 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
home: Material(
child: SurveyWidget(survey: surveyFromJson(extended)!),
child: s.SurveyWidget(survey: s.surveyFromJson(extended)!),
),
),
);
Expand Down Expand Up @@ -246,7 +246,7 @@ void main() {
GlobalCupertinoLocalizations.delegate,
],
home: Material(
child: SurveyWidget(survey: surveyFromJson(extended)!),
child: s.SurveyWidget(survey: s.surveyFromJson(extended)!),
),
),
);
Expand All @@ -266,4 +266,90 @@ void main() {
await tester.idle();
expect(find.text("required"), findsOneWidget);
});

testWidgets(
"CheckboxListTile for 'other' option is initially checked when there is a comment and SurveyWidget is build using builder",
(WidgetTester tester) async {
IndexedWidgetBuilder itemBuilder(List<s.Elementbase> elements) {
return (context, index) {
if (index < elements.length && index >= 0) {
return s.SurveyConfiguration.of(context)!
.factory
.resolve(context, elements[index]);
} else {
return Container(
width: double.infinity,
);
}
};
}

await tester.pumpWidget(
MaterialApp(
localizationsDelegates: const [
appLocalizationDelegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: Material(
child: s.SurveyWidget(
survey: s.surveyFromJson(extended)!,
answer: const {
"warships": ["other"],
"warships-Comment": "UNN Thomas Prince"
},
builder: (context) {
final List<s.Elementbase> elements =
(s.SurveyProvider.of(context).survey.pages?.toList() ?? [])
.map<List<s.Elementbase>>((e) => e.getElements())
.fold(
<s.Elementbase>[],
(previousValue, element) =>
previousValue..addAll(element));
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
fit: FlexFit.loose,
child: ListView.separated(
itemBuilder: itemBuilder(elements),
separatorBuilder: (context, index) {
return const Divider();
},
itemCount: elements.length,
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
),
),
Flexible(
fit: FlexFit.loose,
child: TextButton(
onPressed: () =>
s.SurveyWidgetState.of(context).submit(),
child: const Text(
'Submit',
),
),
),
],
),
);
},
),
),
),
);

await tester.pumpAndSettle();
await tester.idle();

expect(
tester
.widget<CheckboxListTile>(
find.byKey(const Key('other-checkbox-list-tile')))
.value,
true);
});
}

0 comments on commit 9ff2b29

Please sign in to comment.