showNetPromoterScore<T> function

Future<T> showNetPromoterScore<T>(
  1. {@required BuildContext context,
  2. ThemeData theme,
  3. VoidCallback onClosePressed,
  4. dynamic onScoreChanged(
    1. int newScore
    ),
  5. dynamic onFeedbackChanged(
    1. String newFeedback
    ),
  6. dynamic onSurveyCompleted(
    1. NetPromoterScoreResult result
    ),
  7. NpsSurveyTexts texts = const NpsSurveyTexts(),
  8. Widget thankYouIcon}
)

Show a modal Net Promoter Score as a material design bottom sheet.

The context argument is used to look up the Navigator and Theme for the bottom sheet. It is only used when the method is called. Its corresponding widget can be safely removed from the tree before the bottom sheet is closed.

Use onClosePressed parameter to get callback when the user close the survery using the close button

Use onScoreChanged parameter to get callback when the user change the score

Use onFeedbackChanged parameter to get callback when the user change the feedback text field

Use onSurveyCompleted parameter to get callback when the survery is campleted. This callback will provide NetPromoterScoreResult object with the final survery results.

Use texts parameter to customize the text in the survery using your own texts.

Use theme parameter to customize the look and feel of the survey. change font and colors using ThemeData object

Use thankYouIcon parameter to provide a your own Widget for the Thank You view image

showNetPromoterScore(
  context: context,
  texts: NpsSurveyTexts(
    selectScorePageTexts: NpsSelectScorePageTexts(
      surveyQuestionText:
          "How likely are you to recommend flutter_net_promoter_score to a friend or colleague?",
    ),
  ),
  onSurveyCompleted: (result) {
    print("NPS Completed");
    print("Score: ${result.score}");
    print("Feedback: ${result.feedback}");
    print("Promoter Type: ${result.promoterType}");
  },
  onClosePressed: () {
    print("User closed the survery");
  },
  onScoreChanged: (newScore) {
    print("User changed the score to $newScore");
  },
  onFeedbackChanged: (newFeedback) {
    print("User change the feedback to $newFeedback");
  },
  thankYouIcon: Icon(
    Icons.thumb_up,
  ),
  theme: ThemeData.dark()
);

Implementation

Future<T> showNetPromoterScore<T>({
  @required BuildContext context,
  ThemeData theme,
  VoidCallback onClosePressed,
  Function(int newScore) onScoreChanged,
  Function(String newFeedback) onFeedbackChanged,
  Function(NetPromoterScoreResult result) onSurveyCompleted,
  NpsSurveyTexts texts = const NpsSurveyTexts(),
  Widget thankYouIcon,
}) {
  assert(texts != null);

  bool currentlyShowingSurvey = true;

  Future<T> future = showModalBottomSheet(
    backgroundColor: Colors.transparent,
    isDismissible: false,
    isScrollControlled: true,
    context: context,
    builder: (context) {
      return FlutterNetPromoterScore(
        onClosePressed: () {
          Navigator.pop(context);
          if (onClosePressed != null) {
            onClosePressed();
          }
        },
        onSurveyCompleted: (NetPromoterScoreResult result) {
          // call survey completion block
          if (onSurveyCompleted != null) {
            onSurveyCompleted(result);
          }

          // Dismiss after delay
          Future.delayed(
            const Duration(milliseconds: 2000),
            () {
              // Check if the user didn't dismiss the modal view manually by him self
              if (currentlyShowingSurvey) {
                Navigator.pop(context);
              }
            },
          );
        },
        onScoreChanged: onScoreChanged,
        onFeedbackChanged: onFeedbackChanged,
        texts: texts,
        theme: theme == null ? Theme.of(context) : theme,
        thankYouIcon: thankYouIcon,
      );
    },
  );

  future.then((value) {
    currentlyShowingSurvey = false;
  });

  return future;
}