/
newIssue.dart
276 lines (260 loc) · 8.57 KB
/
newIssue.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_advanced_networkimage/provider.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:jira_time/actions/api.dart';
import 'package:jira_time/generated/i18n.dart';
import 'package:jira_time/pages/issue.dart';
import 'package:jira_time/util/redux.dart';
import 'package:jira_time/util/response.dart';
import 'package:jira_time/widgets/customSvg.dart';
import 'package:jira_time/widgets/loading.dart';
class NewIssue extends StatefulWidget {
@override
_NewIssueState createState() => _NewIssueState();
}
class _NewIssueState extends State<NewIssue> with SingleTickerProviderStateMixin {
AnimationController _controller;
GlobalKey _issueFormKey = GlobalKey<FormState>();
TextEditingController _summaryController = new TextEditingController();
TextEditingController _assigneeController = new TextEditingController();
bool _loaded = false; // 是否正在初始化完成
bool _fetching = false; // 是否正在异步传输
List _projects = [];
Map _formError = {};
var _selectedProject;
var _selectedIssueType;
@override
void initState() {
_controller = AnimationController(vsync: this);
super.initState();
//fetch project meta
fetchIssueMeta().then((projects) {
setState(() {
this._projects = projects;
this._selectedProject = projects.first;
this._selectedIssueType = getAvailableIssueTypes(projects.first['issuetypes']).first;
this._loaded = true;
});
}, onError: (e) {
print((e as DioError).request.data);
print((e as DioError).response.data);
});
// add assignee listener
this._assigneeController.addListener(this.handleInputAssignee);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
// add assignee listener
this._assigneeController.removeListener(this.handleInputAssignee);
}
List getAvailableIssueTypes(List issueTypes) =>
issueTypes.where((type) => !type['subtask']).toList();
handleInputAssignee() async {
// clear error
if (this._formError.containsKey('assignee')) {
setState(() {
this._formError.removeWhere((key, value) => key == 'assignee');
});
}
}
handleCreateIssue() async {
FocusScope.of(context).detach();
if ((_issueFormKey.currentState as FormState).validate()) {
setState(() {
this._fetching = true;
});
var payload;
try {
payload = await createIssue(
_selectedProject['id'],
_selectedIssueType['id'],
_summaryController.text,
assignee: _assigneeController.text,
);
} catch (error) {
// something
if (error is DioError && error.type == DioErrorType.RESPONSE) {
return setState(() {
final errorMsg = getServerErrorMsgHelper(error);
if (errorMsg.length > 0) {
Fluttertoast.showToast(msg: getServerErrorMsgHelper(error));
} else {
this._formError = error.response.data['errors'];
}
});
} else {
Fluttertoast.showToast(msg: S.of(context).error_server);
}
} finally {
setState(() {
this._fetching = false;
});
}
Navigator.of(context).push(MaterialPageRoute(builder: (context) => Issue(payload['key'])));
}
}
Widget buildScaffold(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(S.of(context).new_issue),
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
tooltip: S.of(context).create,
onPressed: this.handleCreateIssue,
),
],
),
body: this._loaded
? Container(
padding: EdgeInsets.symmetric(vertical: 10),
child: buildForm(context),
)
: Loading(),
);
}
Widget buildForm(BuildContext context) {
return Form(
key: _issueFormKey, //设置globalKey,用于后面获取FormState
child: Column(
children: <Widget>[
/// project
DropdownButtonFormField(
decoration: InputDecoration(
prefix: FormLabel(S.of(context).project),
),
value: this._selectedProject,
onChanged: (value) {
setState(() {
this._selectedProject = value;
this._selectedIssueType = getAvailableIssueTypes(value['issuetypes']).first;
});
},
items: this
._projects
.map((project) => DropdownMenuItem(
value: project,
child: Row(children: <Widget>[
Container(
margin: EdgeInsets.only(right: 5),
child: CustomSvg(
getAvatarUrl(project),
width: 32,
),
),
Container(
constraints: const BoxConstraints(
maxWidth: 190,
),
child: Text(
project['name'],
overflow: TextOverflow.ellipsis,
),
)
])))
.toList(),
),
/// issue type
DropdownButtonFormField(
decoration: InputDecoration(
prefix: FormLabel(S.of(context).issue_type),
),
value: this._selectedIssueType,
onChanged: (value) {
setState(() {
this._selectedIssueType = value;
});
},
items: getAvailableIssueTypes(this._selectedProject['issuetypes']).map((issueTypes) {
final String pictureUrl = issueTypes['iconUrl'];
final isImage = RegExp('(png|jpe?g)\$').hasMatch(pictureUrl);
const iconWidth = 32.0;
return DropdownMenuItem(
value: issueTypes,
child: Row(children: <Widget>[
Container(
margin: EdgeInsets.only(right: 5),
child: isImage
? Image.network(
pictureUrl,
width: iconWidth,
)
: CustomSvg(
pictureUrl,
width: iconWidth,
),
),
Text(issueTypes['name']),
]));
}).toList(),
),
/// Summary
TextFormField(
controller: _summaryController,
decoration: InputDecoration(
labelText: S.of(context).summary,
),
autovalidate: true,
validator: (String content) {
return content.length > 0 ? null : S.of(context).validator_summary_required;
},
),
/// Assignee
TextFormField(
controller: _assigneeController,
decoration: InputDecoration(
labelText: S.of(context).assignee,
suffix: GestureDetector(
onTap: () {
setState(() {
this._assigneeController.text = getAppState(context).userInfo.name;
});
},
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text(
S.of(context).assign_to_me,
style: TextStyle(
decoration: TextDecoration.underline,
),
),
)),
errorText: this._formError['assignee'],
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
final List<Widget> buildWidgets = [
buildScaffold(context),
];
if (this._fetching) {
buildWidgets.add(Loading());
}
return Scaffold(
body: Stack(children: buildWidgets),
);
}
}
class FormLabel extends StatelessWidget {
final String text;
const FormLabel(this.text);
@override
Widget build(BuildContext context) {
return Container(
width: 100,
padding: EdgeInsets.only(left: 10),
child: Text(
this.text + " : ",
style: Theme.of(context).textTheme.body2,
),
);
}
}