Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/quiet-owls-skip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"posthog_flutter": minor
---

Optional open text, rating, single choice and multiple choice survey questions now show a separate Skip button below Submit, and Submit stays disabled until the question is answered. Skip sends the empty response (`""` for open text, no answer for the other types) and discards any answer already entered. Optional questions that submit automatically also show Skip. The Skip label comes from the survey's `skipButtonText` appearance setting and defaults to "Skip".
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class PostHogDisplaySurvey {
borderColor: a['borderColor'] as String?,
submitButtonColor: a['submitButtonColor'] as String?,
submitButtonText: a['submitButtonText'] as String?,
skipButtonText: a['skipButtonText'] as String?,
submitButtonTextColor: a['submitButtonTextColor'] as String?,
textColor: a['textColor'] as String?,
descriptionTextColor: a['descriptionTextColor'] as String?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PostHogDisplaySurveyAppearance {
this.borderColor,
this.submitButtonColor,
this.submitButtonText,
this.skipButtonText,
this.submitButtonTextColor,
this.textColor,
this.descriptionTextColor,
Expand Down Expand Up @@ -37,6 +38,10 @@ class PostHogDisplaySurveyAppearance {
final String? borderColor;
final String? submitButtonColor;
final String? submitButtonText;

/// Label of the Skip button on optional questions. Null or blank shows
/// "Skip".
final String? skipButtonText;
final String? submitButtonTextColor;
final String? textColor;
final String? descriptionTextColor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SurveyAppearance {
this.backgroundColor = Colors.white,
this.submitButtonColor = Colors.black,
this.submitButtonText = 'Submit',
this.skipButtonText = 'Skip',
this.submitButtonTextColor = Colors.white,
this.descriptionTextColor = Colors.black,
this.questionTextColor = Colors.black,
Expand Down Expand Up @@ -66,6 +67,7 @@ class SurveyAppearance {
final Color backgroundColor;
final Color submitButtonColor;
final String submitButtonText;
final String skipButtonText;
final Color submitButtonTextColor;
final Color descriptionTextColor;
final Color questionTextColor;
Expand Down Expand Up @@ -121,11 +123,15 @@ class SurveyAppearance {
final inputTextColor = _colorFromHex(appearance?.inputTextColor) ??
_getContrastingTextColor(inputBackgroundColor);
final inputPlaceholder = appearance?.placeholder;
final skipButtonText = appearance?.skipButtonText;

return SurveyAppearance(
backgroundColor: backgroundColor,
submitButtonColor: submitButtonColor,
submitButtonText: appearance?.submitButtonText ?? 'Submit',
skipButtonText: skipButtonText != null && skipButtonText.trim().isNotEmpty
? skipButtonText
: 'Skip',
submitButtonTextColor: _colorFromHex(appearance?.submitButtonTextColor) ??
_getContrastingTextColor(submitButtonColor),
descriptionTextColor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class _ChoiceQuestionWidgetState extends State<ChoiceQuestionWidget> {
!widget.hasOpenChoice;

bool get _canSubmit {
if (widget.optional) return true;
if (_selectedChoices.isEmpty) return false;
if (widget.hasOpenChoice &&
_selectedChoices.contains(widget.choices.last) &&
Expand Down Expand Up @@ -208,6 +207,14 @@ class _ChoiceQuestionWidgetState extends State<ChoiceQuestionWidget> {
appearance: widget.appearance,
),
],
if (widget.optional)
Padding(
padding: EdgeInsets.only(top: _shouldAutoSubmit ? 16 : 0),
child: SurveySkipButton(
onPressed: () => widget.onSubmit(null),
appearance: widget.appearance,
),
),
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class _OpenTextQuestionState extends State<OpenTextQuestion> {
String? _response;

bool get _canSubmit {
if (widget.optional) return true;
return _response?.isNotEmpty == true;
final response = _response;
if (response == null) return false;
return widget.optional ? response.trim().isNotEmpty : response.isNotEmpty;
}

@override
Expand Down Expand Up @@ -102,6 +103,11 @@ class _OpenTextQuestionState extends State<OpenTextQuestion> {
appearance: widget.appearance,
enabled: _canSubmit,
),
if (widget.optional)
SurveySkipButton(
onPressed: () => widget.onSubmit(''),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: Pre-existing cross-SDK divergence worth a policy decision in PostHog/sdk-specs (not a regression or merge blocker for #758 — merge base 28c95988 already emitted '' for empty optional open-text submit, matching posthog-js#5995).

Observed behavior across the native bridges and backend:

  • Skipping an optional open-text question sends '' → Android (PostHogFlutterSurveysDelegate.kt:168-169) wraps it as PostHogSurveyResponse.Text("") and iOS (PosthogFlutterPlugin.swift:811-812) wraps it as .openEnded("").
  • In both delegated native cores (PostHogSurveysIntegration.kt:1036,1060-1062 and PostHogSurveyIntegration.swift:794,817-829), "" is non-null, so surveyHasResponses returns true. If a user skips Q0 (optional open text) and then dismisses Q1 without answering anything, survey dismissed is captured with $survey_partially_completed: true and $survey_response_<id>: "".
  • In PostHog/posthog (products/surveys/backend/responses/fetch_rows.py:125-128), RESPONSE_EVENT_FILTER includes survey dismissed events where $survey_partially_completed = 'true', counting the skip-then-dismiss as a partial response row.
  • By contrast, Flutter's rating/choice skip sends null, and both delegated native cores' own UIs send null/nil on open-text skip (posthog-android#1030 SurveySheet.kt:189, posthog-ios#1146 QuestionTypes.swift:138), keeping $survey_partially_completed: false.

Because posthog-js#5995 (browser & RN) also sends "", consider opening a cross-SDK policy decision in PostHog/sdk-specs (Client Libraries / Surveys teams) on whether skipped optional open-text should emit null/omitted or "" (and whether surveyHasResponses should treat "" as unanswered) rather than changing it ad hoc here.

appearance: widget.appearance,
),
],
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class _RatingQuestionState extends State<RatingQuestion> {
int? _rating;

bool get _canSubmit {
if (widget.optional) return true;
return _rating != null;
}

Expand Down Expand Up @@ -245,6 +244,14 @@ class _RatingQuestionState extends State<RatingQuestion> {
},
),
],
if (widget.optional)
Padding(
padding: EdgeInsets.only(top: widget.skipSubmitButton ? 24 : 0),
child: SurveySkipButton(
onPressed: () => widget.onSubmit(null),
appearance: widget.appearance,
),
),
],
);
}
Expand Down
36 changes: 36 additions & 0 deletions posthog_flutter/lib/src/surveys/widgets/survey_button.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
import 'package:flutter/material.dart';
import '../models/survey_appearance.dart';

class SurveySkipButton extends StatelessWidget {
const SurveySkipButton({
super.key,
required this.onPressed,
this.appearance = SurveyAppearance.defaultAppearance,
});

final VoidCallback onPressed;
final SurveyAppearance appearance;

@override
Widget build(BuildContext context) {
return TextButton(
onPressed: onPressed,
style: TextButton.styleFrom(
foregroundColor: appearance.questionTextColor,
minimumSize: const Size.fromHeight(44),
),
child: Opacity(
opacity: 0.7,
child: Text(
appearance.skipButtonText,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
color: appearance.questionTextColor,
fontFamily: appearance.fontFamily,
decoration: TextDecoration.underline,
decorationColor: appearance.questionTextColor,
),
),
),
);
}
}

class SurveyButton extends StatelessWidget {
const SurveyButton({
super.key,
Expand Down
2 changes: 1 addition & 1 deletion posthog_flutter/test/survey_optional_choice_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void main() {
));
await tester.pumpAndSettle();

await tester.tap(find.text('Submit'));
await tester.tap(find.text('Skip'));
await tester.pumpAndSettle();

expect(responses, [null]);
Expand Down
14 changes: 7 additions & 7 deletions posthog_flutter/test/survey_optional_questions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ Future<List<Object?>> pumpQuestion(
}

void main() {
testWidgets('optional open text can be submitted empty with its button label',
testWidgets('optional open text skip submits empty with its own label',
(tester) async {
final responses = await pumpQuestion(tester, {
'type': 'open',
'question': 'Anything else?',
'isOptional': true,
'buttonText': 'Skip',
'buttonText': 'Send it',
});

final button = tester.widget<ElevatedButton>(
find.widgetWithText(ElevatedButton, 'Skip'),
find.widgetWithText(ElevatedButton, 'Send it'),
);
expect(button.onPressed, isNotNull);
expect(button.onPressed, isNull);

await tester.tap(find.text('Skip'));
await tester.pumpAndSettle();

expect(responses, ['']);
});

testWidgets('optional rating can be submitted with no selection',
testWidgets('optional rating skip submits null with no selection',
(tester) async {
final responses = await pumpQuestion(tester, {
'type': 'rating',
Expand All @@ -76,9 +76,9 @@ void main() {
final button = tester.widget<ElevatedButton>(
find.widgetWithText(ElevatedButton, 'Next'),
);
expect(button.onPressed, isNotNull);
expect(button.onPressed, isNull);

await tester.tap(find.text('Next'));
await tester.tap(find.text('Skip'));
await tester.pumpAndSettle();

expect(responses, [null]);
Expand Down
Loading
Loading