Skip to content

Commit

Permalink
Various changes and updated changelog.
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyost committed Jul 20, 2021
1 parent 0dc4bc6 commit 916defb
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 88 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.1
* Added support for dialogs transitions (thanks [Sadeesha-Sath](https://github.com/Sadeesha-Sath)).
* Added custom item and item color customization for rate dialogs (thanks [Sadeesha-Sath](https://github.com/Sadeesha-Sath)).

## 1.1.0+1
* Fixed bugs with some `invokeMethod` calls.

Expand Down
72 changes: 47 additions & 25 deletions lib/src/core.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pedantic/pedantic.dart';
Expand Down Expand Up @@ -54,7 +55,8 @@ class RateMyApp {
/// Initializes the plugin (loads base launch date, app launches and whether the dialog should not be opened again).
Future<void> init() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
conditions.forEach((condition) => condition.readFromPreferences(preferences, preferencesPrefix));
conditions.forEach((condition) =>
condition.readFromPreferences(preferences, preferencesPrefix));
await callEvent(RateMyAppEventType.initialized);
}

Expand Down Expand Up @@ -98,11 +100,13 @@ class RateMyApp {
}

/// Returns whether native review dialog is supported.
Future<bool?> get isNativeReviewDialogSupported => _channel.invokeMethod<bool>('isNativeDialogSupported');
Future<bool?> get isNativeReviewDialogSupported =>
_channel.invokeMethod<bool>('isNativeDialogSupported');

/// Launches the native review dialog.
/// You should check for [isNativeReviewDialogSupported] before running the method.
Future<void> launchNativeReviewDialog() => _channel.invokeMethod('launchNativeReviewDialog');
Future<void> launchNativeReviewDialog() =>
_channel.invokeMethod('launchNativeReviewDialog');

/// Shows the rate dialog.
Future<void> showRateDialog(
Expand All @@ -120,21 +124,23 @@ class RateMyApp {
VoidCallback? onDismissed,
bool? barrierDismissible,
String? barrierLabel,
DialogTransition dialogTransition = const DialogTransition.def(),
DialogTransition dialogTransition = const DialogTransition(),
}) async {
ignoreNativeDialog ??= Platform.isAndroid;
if (!ignoreNativeDialog && ((await isNativeReviewDialogSupported) ?? false)) {
if (!ignoreNativeDialog &&
((await isNativeReviewDialogSupported) ?? false)) {
unawaited(callEvent(RateMyAppEventType.iOSRequestReview));
await launchNativeReviewDialog();
return;
}

var rateMyAppDialog = RateMyAppDialog(
RateMyAppDialog rateMyAppDialog = RateMyAppDialog(
this,
title: title ?? 'Rate this app',
message: message ??
'If you like this app, please take a little bit of your time to review it !\nIt really helps us and it shouldn\'t take you more than one minute.',
contentBuilder: contentBuilder ?? ((context, defaultContent) => defaultContent),
contentBuilder:
contentBuilder ?? ((context, defaultContent) => defaultContent),
actionsBuilder: actionsBuilder,
rateButton: rateButton ?? 'RATE',
noButton: noButton ?? 'NO THANKS',
Expand All @@ -146,8 +152,11 @@ class RateMyApp {
unawaited(callEvent(RateMyAppEventType.dialogOpen));

// Using showDialog() when TransitionType.none to get rid of the default fading animation of showGeneralDialog()
RateMyAppDialogButton? clickedButton = await (dialogTransition.transitionType == TransitionType.none
? showDialog<RateMyAppDialogButton>(context: context, builder: (context) => rateMyAppDialog)
RateMyAppDialogButton? clickedButton = await (dialogTransition
.transitionType ==
TransitionType.none
? showDialog<RateMyAppDialogButton>(
context: context, builder: (context) => rateMyAppDialog)
: showGeneralDialog<RateMyAppDialogButton>(
context: context,
barrierLabel: barrierLabel ?? '',
Expand Down Expand Up @@ -179,10 +188,11 @@ class RateMyApp {
VoidCallback? onDismissed,
bool? barrierDismissible,
String? barrierLabel,
DialogTransition dialogTransition = const DialogTransition.def(),
DialogTransition dialogTransition = const DialogTransition(),
}) async {
ignoreNativeDialog ??= Platform.isAndroid;
if (!ignoreNativeDialog && ((await isNativeReviewDialogSupported) ?? false)) {
if (!ignoreNativeDialog &&
((await isNativeReviewDialogSupported) ?? false)) {
unawaited(callEvent(RateMyAppEventType.iOSRequestReview));
await launchNativeReviewDialog();
return;
Expand All @@ -191,11 +201,13 @@ class RateMyApp {
assert(actionsBuilder != null, 'You should provide an actions builder.');
unawaited(callEvent(RateMyAppEventType.starDialogOpen));

var starRateDialog = RateMyAppStarDialog(
RateMyAppStarDialog starRateDialog = RateMyAppStarDialog(
this,
title: title ?? 'Rate this app',
message: message ?? 'You like this app ? Then take a little bit of your time to leave a rating :',
contentBuilder: contentBuilder ?? ((context, defaultContent) => defaultContent),
message: message ??
'You like this app ? Then take a little bit of your time to leave a rating :',
contentBuilder:
contentBuilder ?? ((context, defaultContent) => defaultContent),
actionsBuilder: actionsBuilder,
dialogStyle: dialogStyle ??
const DialogStyle(
Expand All @@ -207,7 +219,9 @@ class RateMyApp {
);

// Using [showDialog()] when [TransitionType.none] to get rid of the default fading animation of [showGeneralDialog()]
RateMyAppDialogButton? clickedButton = await (dialogTransition.transitionType == TransitionType.none
RateMyAppDialogButton? clickedButton = await (dialogTransition
.transitionType ==
TransitionType.none
? showDialog(context: context, builder: (context) => starRateDialog)
: showGeneralDialog(
context: context,
Expand All @@ -229,8 +243,8 @@ class RateMyApp {

/// Launches the corresponding store.
Future<LaunchStoreResult> launchStore() async {
int? result =
await _channel.invokeMethod<int>('launchStore', storeIdentifier == null ? null : {'appId': storeIdentifier});
int? result = await _channel.invokeMethod<int>('launchStore',
storeIdentifier == null ? null : {'appId': storeIdentifier});
switch (result) {
case 0:
return LaunchStoreResult.storeOpened;
Expand All @@ -244,8 +258,8 @@ class RateMyApp {
/// Calls the specified event.
Future<void> callEvent(RateMyAppEventType eventType) async {
bool saveSharedPreferences = false;
conditions
.forEach((condition) => saveSharedPreferences = condition.onEventOccurred(eventType) || saveSharedPreferences);
conditions.forEach((condition) => saveSharedPreferences =
condition.onEventOccurred(eventType) || saveSharedPreferences);
if (saveSharedPreferences) {
await save();
}
Expand Down Expand Up @@ -277,31 +291,39 @@ class RateMyApp {
switch (dialogTransition.transitionType) {
case TransitionType.fade:
return FadeTransition(
opacity: CurvedAnimation(curve: dialogTransition.curve, parent: animation),
opacity:
CurvedAnimation(curve: dialogTransition.curve, parent: animation),
child: child,
);
case TransitionType.rotation:
return RotationTransition(
turns: CurvedAnimation(curve: dialogTransition.curve, parent: animation),
turns:
CurvedAnimation(curve: dialogTransition.curve, parent: animation),
child: child,
);
case TransitionType.scale:
return ScaleTransition(
alignment: dialogTransition.alignment ?? Alignment.center,
scale: CurvedAnimation(curve: dialogTransition.curve, parent: animation),
scale:
CurvedAnimation(curve: dialogTransition.curve, parent: animation),
child: child,
);
case TransitionType.scaleAndFade:
return FadeTransition(
opacity: CurvedAnimation(curve: dialogTransition.curve, parent: animation),
opacity:
CurvedAnimation(curve: dialogTransition.curve, parent: animation),
child: ScaleTransition(
scale: CurvedAnimation(curve: dialogTransition.curve, parent: animation),
scale: CurvedAnimation(
curve: dialogTransition.curve, parent: animation),
child: child,
),
);
case TransitionType.slide:
return SlideTransition(
position: Tween<Offset>(begin: dialogTransition.startOffset ?? const Offset(1, 0), end: Offset.zero).animate(
position: Tween<Offset>(
begin: dialogTransition.startOffset ?? const Offset(1, 0),
end: Offset.zero)
.animate(
CurvedAnimation(parent: animation, curve: dialogTransition.curve),
),
);
Expand Down
39 changes: 25 additions & 14 deletions lib/src/dialogs.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart'
as flutter_rating_bar;
import 'package:rate_my_app/rate_my_app.dart';
import 'package:rate_my_app/src/core.dart';
import 'package:rate_my_app/src/style.dart';

/// A simple dialog button click listener.
typedef RateMyAppDialogButtonClickListener = bool Function(RateMyAppDialogButton button);
typedef RateMyAppDialogButtonClickListener = bool Function(
RateMyAppDialogButton button);

/// Validates a state when called in a function.
typedef Validator = bool Function();

/// Allows to change the default dialog content.
typedef DialogContentBuilder = Widget Function(BuildContext context, Widget defaultContent);
typedef DialogContentBuilder = Widget Function(
BuildContext context, Widget defaultContent);

/// Allows to dynamically build actions.
typedef DialogActionsBuilder = List<Widget> Function(BuildContext context);

/// Allows to dynamically build actions according to the specified rating.
typedef StarDialogActionsBuilder = List<Widget> Function(BuildContext context, double? stars);
typedef StarDialogActionsBuilder = List<Widget> Function(
BuildContext context, double? stars);

/// The Android Rate my app dialog.
class RateMyAppDialog extends StatelessWidget {
Expand Down Expand Up @@ -98,17 +102,20 @@ class RateMyAppDialog extends StatelessWidget {
RateMyAppRateButton(
rateMyApp,
text: rateButton,
validator: () => listener == null || listener!(RateMyAppDialogButton.rate),
validator: () =>
listener == null || listener!(RateMyAppDialogButton.rate),
),
RateMyAppLaterButton(
rateMyApp,
text: laterButton,
validator: () => listener == null || listener!(RateMyAppDialogButton.later),
validator: () =>
listener == null || listener!(RateMyAppDialogButton.later),
),
RateMyAppNoButton(
rateMyApp,
text: noButton,
validator: () => listener == null || listener!(RateMyAppDialogButton.no),
validator: () =>
listener == null || listener!(RateMyAppDialogButton.no),
),
];
}
Expand Down Expand Up @@ -151,7 +158,8 @@ class RateMyAppStarDialog extends StatefulWidget {
State<StatefulWidget> createState() => _RateMyAppStarDialogState();

/// Used when there is no onRatingChanged callback.
List<Widget> _defaultOnRatingChanged(BuildContext context, double? rating) => [
List<Widget> _defaultOnRatingChanged(BuildContext context, double? rating) =>
[
RateMyAppRateButton(
rateMyApp,
text: 'RATE',
Expand Down Expand Up @@ -209,18 +217,20 @@ class _RateMyAppStarDialogState extends State<RateMyAppStarDialog> {
content: widget.contentBuilder(context, content),
contentPadding: widget.dialogStyle.contentPadding,
shape: widget.dialogStyle.dialogShape,
actions: (widget.actionsBuilder ?? widget._defaultOnRatingChanged)(context, currentRating),
actions: (widget.actionsBuilder ?? widget._defaultOnRatingChanged)(
context, currentRating),
);
}

/// Creates the rating bar.
RatingBar createRatingBar() {
flutter_rating_bar.RatingBar createRatingBar() {
if (widget.starRatingOptions.itemBuilder == null) {
return RatingBar(
return flutter_rating_bar.RatingBar(
onRatingUpdate: (rating) {
setState(() => currentRating = rating);
},
ratingWidget: widget.starRatingOptions.ratingWidget ?? createDefaultRatingWidget(),
ratingWidget: widget.starRatingOptions.ratingWidget ??
createDefaultRatingWidget(),
initialRating: widget.starRatingOptions.initialRating,
minRating: widget.starRatingOptions.minRating,
allowHalfRating: widget.starRatingOptions.allowHalfRating,
Expand All @@ -233,7 +243,7 @@ class _RateMyAppStarDialogState extends State<RateMyAppStarDialog> {
wrapAlignment: widget.starRatingOptions.wrapAlignment,
);
}
return RatingBar.builder(
return flutter_rating_bar.RatingBar.builder(
onRatingUpdate: (rating) {
setState(() => currentRating = rating);
},
Expand Down Expand Up @@ -295,7 +305,8 @@ class _RateMyAppStarDialogState extends State<RateMyAppStarDialog> {
),
empty: Icon(
Icons.star_border,
color: widget.starRatingOptions.borderColor ?? widget.starRatingOptions.itemColor,
color: widget.starRatingOptions.borderColor ??
widget.starRatingOptions.itemColor,
size: widget.starRatingOptions.itemSize,
),
);
Expand Down
Loading

0 comments on commit 916defb

Please sign in to comment.