-
-
Notifications
You must be signed in to change notification settings - Fork 113
/
content.dart
170 lines (157 loc) · 5.32 KB
/
content.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:rate_my_app/rate_my_app.dart';
/// The app's main content widget.
class ContentWidget extends StatefulWidget {
/// The Rate my app instance.
final RateMyApp rateMyApp;
/// Creates a new content widget instance.
const ContentWidget({
super.key,
required this.rateMyApp,
});
@override
State<StatefulWidget> createState() => _ContentWidgetState();
}
/// The content widget state.
class _ContentWidgetState extends State<ContentWidget> {
/// Contains all debuggable conditions.
List<DebuggableCondition> debuggableConditions = [];
/// Whether the dialog should be opened.
bool shouldOpenDialog = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => refresh());
}
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.symmetric(horizontal: 40),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (DebuggableCondition condition in debuggableConditions)
textCenter(condition.toString()),
textCenter(
'Are conditions met : ${shouldOpenDialog ? 'Yes' : 'No'}'),
Padding(
padding: const EdgeInsets.only(top: 10),
child: ElevatedButton(
onPressed: () async {
await widget.rateMyApp.showRateDialog(
context,
ignoreNativeDialog: kDebugMode,
); // We launch the default Rate my app dialog.
refresh();
},
child: const Text('Launch "Rate my app" dialog'),
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: ElevatedButton(
onPressed: () async {
await widget.rateMyApp.showStarRateDialog(
context,
actionsBuilder: (_, stars) =>
starRateDialogActionsBuilder(context, stars),
ignoreNativeDialog: kDebugMode,
); // We launch the Rate my app dialog with stars.
refresh();
},
child: const Text('Launch "Rate my app" star dialog'),
),
),
Padding(
padding: const EdgeInsets.only(top: 10),
child: ElevatedButton(
onPressed: () async {
await widget.rateMyApp
.reset(); // We reset all Rate my app conditions values.
refresh();
},
child: const Text('Reset'),
),
),
],
),
);
/// Returns a centered text.
Text textCenter(String content) => Text(
content,
textAlign: TextAlign.center,
);
/// Allows to refresh the widget state.
void refresh() {
setState(() {
debuggableConditions =
widget.rateMyApp.conditions.whereType<DebuggableCondition>().toList();
shouldOpenDialog = widget.rateMyApp.shouldOpenDialog;
});
}
List<Widget> starRateDialogActionsBuilder(
BuildContext context, double? stars) {
final Widget cancelButton = RateMyAppNoButton(
// We create a custom "Cancel" button using the RateMyAppNoButton class.
widget.rateMyApp,
text: MaterialLocalizations.of(context).cancelButtonLabel.toUpperCase(),
callback: refresh,
);
if (stars == null || stars == 0) {
// If there is no rating (or a 0 star rating), we only have to return our cancel button.
return [cancelButton];
}
// Otherwise we can do some little more things...
String message = 'You put ${stars.round()} star(s). ';
Color color = Colors.black;
switch (stars.round()) {
case 1:
message += 'Did this app hurt you physically ?';
color = Colors.red;
break;
case 2:
message += 'That\'s not really cool man.';
color = Colors.orange;
break;
case 3:
message += 'Well, it\'s average.';
color = Colors.yellow;
break;
case 4:
message += 'This is cool, like this app.';
color = Colors.lime;
break;
case 5:
message += 'Great ! <3';
color = Colors.green;
break;
}
return [
TextButton(
onPressed: () async {
if (kDebugMode) {
print(message);
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
backgroundColor: color,
),
);
// This allow to mimic a click on the default "Rate" button and thus update the conditions based on it ("Do not open again" condition for example) :
await widget.rateMyApp
.callEvent(RateMyAppEventType.rateButtonPressed);
if (context.mounted) {
Navigator.pop<RateMyAppDialogButton>(
context, RateMyAppDialogButton.rate);
refresh();
}
},
child:
Text(MaterialLocalizations.of(context).okButtonLabel.toUpperCase()),
),
cancelButton,
];
}
}