-
-
Notifications
You must be signed in to change notification settings - Fork 246
/
Copy pathsentry_feedback_widget.dart
260 lines (243 loc) · 9.87 KB
/
sentry_feedback_widget.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// ignore_for_file: library_private_types_in_public_api
import 'package:flutter/material.dart';
import '../../sentry_flutter.dart';
class SentryFeedbackWidget extends StatefulWidget {
SentryFeedbackWidget({
super.key,
this.associatedEventId,
Hub? hub,
this.title = 'Report a Bug',
this.nameLabel = 'Name',
this.namePlaceholder = 'Your Name',
this.emailLabel = 'Email',
this.emailPlaceholder = '[email protected]',
this.messageLabel = 'Description',
this.messagePlaceholder = 'What\'s the bug? What did you expect?',
this.submitButtonLabel = 'Send Bug Report',
this.cancelButtonLabel = 'Cancel',
this.validationErrorLabel = 'Can\'t be empty',
this.isRequiredLabel = '(required)',
this.isNameRequired = false,
this.isEmailRequired = false,
this.screenshot,
}) : assert(associatedEventId != const SentryId.empty()),
_hub = hub ?? HubAdapter();
final SentryId? associatedEventId;
final Hub _hub;
final String title;
final String nameLabel;
final String namePlaceholder;
final String emailLabel;
final String emailPlaceholder;
final String messageLabel;
final String messagePlaceholder;
final String submitButtonLabel;
final String cancelButtonLabel;
final String validationErrorLabel;
final String isRequiredLabel;
final bool isNameRequired;
final bool isEmailRequired;
final SentryAttachment? screenshot;
@override
_SentryFeedbackWidgetState createState() => _SentryFeedbackWidgetState();
}
class _SentryFeedbackWidgetState extends State<SentryFeedbackWidget> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _messageController = TextEditingController();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Text(
key: const ValueKey('sentry_feedback_name_label'),
widget.nameLabel,
style: Theme.of(context).textTheme.labelMedium,
),
const SizedBox(width: 4),
if (widget.isNameRequired)
Text(
key: const ValueKey(
'sentry_feedback_name_required_label'),
widget.isRequiredLabel,
style: Theme.of(context).textTheme.labelMedium,
),
],
),
const SizedBox(height: 4),
TextFormField(
key: const ValueKey('sentry_feedback_name_textfield'),
style: Theme.of(context).textTheme.bodyLarge,
controller: _nameController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: widget.namePlaceholder,
),
keyboardType: TextInputType.text,
validator: (String? value) {
return _errorText(value, widget.isNameRequired);
},
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 16),
Row(
children: [
Text(
key: const ValueKey('sentry_feedback_email_label'),
widget.emailLabel,
style: Theme.of(context).textTheme.labelMedium,
),
const SizedBox(width: 4),
if (widget.isEmailRequired)
Text(
key: const ValueKey(
'sentry_feedback_email_required_label'),
widget.isRequiredLabel,
style: Theme.of(context).textTheme.labelMedium,
),
],
),
const SizedBox(height: 4),
TextFormField(
key: const ValueKey('sentry_feedback_email_textfield'),
controller: _emailController,
style: Theme.of(context).textTheme.bodyLarge,
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: widget.emailPlaceholder,
),
keyboardType: TextInputType.emailAddress,
validator: (String? value) {
return _errorText(value, widget.isEmailRequired);
},
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 16),
Row(
children: [
Text(
key:
const ValueKey('sentry_feedback_message_label'),
widget.messageLabel,
style: Theme.of(context).textTheme.labelMedium,
),
const SizedBox(width: 4),
Text(
key: const ValueKey(
'sentry_feedback_message_required_label'),
widget.isRequiredLabel,
style: Theme.of(context).textTheme.labelMedium,
),
],
),
const SizedBox(height: 4),
TextFormField(
key:
const ValueKey('sentry_feedback_message_textfield'),
controller: _messageController,
style: Theme.of(context).textTheme.bodyLarge,
minLines: 5,
maxLines: null,
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: widget.messagePlaceholder,
),
keyboardType: TextInputType.multiline,
validator: (String? value) {
return _errorText(value, true);
},
autovalidateMode: AutovalidateMode.onUserInteraction,
),
],
),
),
),
),
const SizedBox(height: 8),
Column(
children: [
SizedBox(
width: double.infinity,
child: ElevatedButton(
key: const ValueKey('sentry_feedback_submit_button'),
onPressed: () async {
if (!_formKey.currentState!.validate()) {
return;
}
final feedback = SentryFeedback(
message: _messageController.text,
contactEmail: _emailController.text,
name: _nameController.text,
associatedEventId: widget.associatedEventId,
);
Hint? hint;
final screenshot = widget.screenshot;
if (screenshot != null) {
hint = Hint.withScreenshot(screenshot);
}
await _captureFeedback(feedback, hint);
bool mounted;
try {
mounted = (this as dynamic).mounted as bool;
} on NoSuchMethodError catch (_) {
mounted = false;
}
if (mounted) {
// ignore: use_build_context_synchronously
await Navigator.maybePop(context);
}
},
child: Text(widget.submitButtonLabel),
),
),
SizedBox(
width: double.infinity,
child: TextButton(
key: const ValueKey('sentry_feedback_close_button'),
onPressed: () {
Navigator.pop(context);
},
child: Text(widget.cancelButtonLabel),
),
),
],
),
],
),
),
);
}
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_messageController.dispose();
super.dispose();
}
String? _errorText(String? value, bool isRequired) {
if (isRequired && (value == null || value.isEmpty)) {
return widget.validationErrorLabel;
}
return null;
}
Future<SentryId> _captureFeedback(SentryFeedback feedback, Hint? hint) {
return widget._hub.captureFeedback(feedback, hint: hint);
}
}