customer_target_group_picker.dart
9.7 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:one_key/models/customer_target_group_model.dart';
import 'package:one_key/provider/coupon_provider.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/utils.dart';
import 'package:provider/provider.dart';
/// 选择目标客户群体弹窗 —— 标签选择 + 自定义添加
class CustomerTargetGroupPicker extends StatelessWidget {
final List<CustomerTargetGroupModel> groups;
const CustomerTargetGroupPicker({super.key, required this.groups});
static Future<List<CustomerTargetGroupModel>?> show(
BuildContext context, {
required List<CustomerTargetGroupModel> groups,
}) {
return showModalBottomSheet<List<CustomerTargetGroupModel>>(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: true,
builder: (_) => CustomerTargetGroupPicker(groups: groups),
);
}
@override
Widget build(BuildContext context) {
return _Sheet(groups: groups);
}
}
/// 弹窗主体
class _Sheet extends StatefulWidget {
final List<CustomerTargetGroupModel> groups;
const _Sheet({required this.groups});
@override
State<_Sheet> createState() => _SheetState();
}
class _SheetState extends State<_Sheet> {
late final List<CustomerTargetGroupModel> _items;
final _inputController = TextEditingController();
@override
void initState() {
super.initState();
_items = widget.groups.map((e) {
return CustomerTargetGroupModel(
id: e.id,
name: e.name,
isSelected: e.isSelected,
);
}).toList();
}
@override
void dispose() {
_inputController.dispose();
super.dispose();
}
void _onTagTap(int index) {
setState(() {
for (int i = 0; i < _items.length; i++) {
_items[i].isSelected = i == index;
}
});
Navigator.of(context).pop(_items);
}
void _onAddCustom() async {
final text = _inputController.text.trim();
if (text.isEmpty) {
Utils.showToast('请输入客户群体名称');
return;
}
final userModel = Provider.of<UserModel>(context, listen: false);
final couponProvider = Provider.of<CouponProvider>(context, listen: false);
final params = <String, String?>{
'sellerid': userModel.user!.sellerid,
'name': text,
};
final newId =
await couponProvider.createCustomerTargetGroup(context, params);
if (newId != null) {
print('TEST2 自定义群体创建成功: $text, id=$newId');
setState(() {
_items.add(CustomerTargetGroupModel(id: newId, name: text, isSelected: true));
_inputController.clear();
});
for (int i = 0; i < _items.length; i++) {
_items[i].isSelected = i == _items.length - 1;
}
Navigator.of(context).pop(_items);
}
}
@override
Widget build(BuildContext context) {
final bottomInset = MediaQuery.of(context).viewInsets.bottom;
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: ClipRRect(
borderRadius: BorderRadius.circular(ScreenUtil().setWidth(16)),
child: Container(
color: MyColours.color_ffffff,
child: SafeArea(
child: Padding(
padding: EdgeInsets.only(
top: ScreenUtil().setWidth(24),
bottom: ScreenUtil().setWidth(64) + bottomInset),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_Title(),
SizedBox(height: ScreenUtil().setWidth(49)),
_Divider(),
SizedBox(height: ScreenUtil().setWidth(32)),
_TagGrid(items: _items, onTap: _onTagTap),
SizedBox(height: ScreenUtil().setWidth(32)),
_Divider(),
SizedBox(height: ScreenUtil().setWidth(32)),
Padding(
padding: EdgeInsets.only(
left: ScreenUtil().setWidth(30),
right: ScreenUtil().setWidth(64)),
child: _AddCustomRow(
controller: _inputController,
onConfirm: _onAddCustom,
),
),
],
),
),
),
),
),
);
}
}
/// 标题:16px Semibold #000 90% 居中
class _Title extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
height: ScreenUtil().setWidth(48),
child: Center(
child: Text(
'选择目标客户群体',
style: TextStyle(
fontSize: ScreenUtil().setSp(32),
fontWeight: FontWeight.w600,
color: const Color(0xE6000000),
),
),
),
);
}
}
/// 分割线
class _Divider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: ScreenUtil().setWidth(1),
color: MyColours.color_E7E7E7,
);
}
}
/// 标签流式布局
class _TagGrid extends StatelessWidget {
final List<CustomerTargetGroupModel> items;
final void Function(int) onTap;
const _TagGrid({required this.items, required this.onTap});
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.symmetric(horizontal: ScreenUtil().setWidth(30)),
child: Wrap(
spacing: ScreenUtil().setWidth(24),
runSpacing: ScreenUtil().setWidth(24),
children: List.generate(items.length, (i) {
return _Tag(
text: items[i].name ?? '',
selected: items[i].isSelected,
onTap: () => onTap(i),
);
}),
),
);
}
}
/// 标签按钮:padding 5px 10px / 圆角5px / 选中#588C8C白字 / 未选中白底#515151边框
class _Tag extends StatelessWidget {
final String text;
final bool selected;
final VoidCallback onTap;
const _Tag({required this.text, required this.selected, required this.onTap});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
padding: EdgeInsets.symmetric(
horizontal: ScreenUtil().setWidth(20),
vertical: ScreenUtil().setWidth(10)),
decoration: BoxDecoration(
color: selected ? const Color(0xFF588C8C) : MyColours.color_ffffff,
borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10)),
border: selected
? null
: Border.all(
color: MyColours.color_515151,
width: ScreenUtil().setWidth(2)),
),
child: Text(
text,
style: TextStyle(
fontSize: ScreenUtil().setSp(28),
color: selected ? MyColours.color_ffffff : MyColours.color_515151,
),
),
),
);
}
}
/// 添加其他群体行
class _AddCustomRow extends StatelessWidget {
final TextEditingController controller;
final VoidCallback onConfirm;
const _AddCustomRow({required this.controller, required this.onConfirm});
@override
Widget build(BuildContext context) {
return Row(
children: [
Text(
'添加其他群体:',
style: TextStyle(
fontSize: ScreenUtil().setSp(28),
fontWeight: FontWeight.w400,
color: MyColours.color_515151,
),
),
SizedBox(width: ScreenUtil().setWidth(30)),
Expanded(
child: SizedBox(
height: ScreenUtil().setWidth(68),
child: TextField(
controller: controller,
style: TextStyle(fontSize: ScreenUtil().setSp(28)),
decoration: InputDecoration(
hintText: '输入客户群体名称',
hintStyle: TextStyle(
fontSize: ScreenUtil().setSp(28),
color: const Color(0xFFBBBBBB),
),
contentPadding: EdgeInsets.symmetric(
horizontal: ScreenUtil().setWidth(20),
vertical: ScreenUtil().setWidth(10)),
border: OutlineInputBorder(
borderRadius: BorderRadius.zero,
borderSide: const BorderSide(
color: Color(0xFF515151), width: 1),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.zero,
borderSide: const BorderSide(
color: Color(0xFF515151), width: 1),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.zero,
borderSide: const BorderSide(
color: Color(0xFF515151), width: 1),
),
),
),
),
),
SizedBox(width: ScreenUtil().setWidth(30)),
GestureDetector(
onTap: onConfirm,
child: Container(
width: ScreenUtil().setWidth(96),
height: ScreenUtil().setWidth(68),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(ScreenUtil().setWidth(10)),
border: Border.all(
color: MyColours.color_515151,
width: ScreenUtil().setWidth(2)),
),
child: Text(
'确认',
style: TextStyle(
fontSize: ScreenUtil().setSp(28),
fontWeight: FontWeight.w400,
color: MyColours.color_515151,
),
),
),
),
],
);
}
}