428 lines
18 KiB
Dart
428 lines
18 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../domain/entities/region_draft.dart';
|
|
import '../cubit/region_editor_cubit.dart';
|
|
|
|
/// 地图编辑器、分区列表等入口共用同一份表单与保存链路。
|
|
Future<bool?> showRegionFormDialog(
|
|
BuildContext context, {
|
|
required RegionEditorCubit cubit,
|
|
required bool Function() isScopeCurrent,
|
|
}) => showDialog<bool>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) =>
|
|
RegionFormDialog(cubit: cubit, isScopeCurrent: isScopeCurrent),
|
|
);
|
|
|
|
class RegionFormDialog extends StatefulWidget {
|
|
const RegionFormDialog({
|
|
super.key,
|
|
required this.cubit,
|
|
required this.isScopeCurrent,
|
|
});
|
|
final RegionEditorCubit cubit;
|
|
final bool Function() isScopeCurrent;
|
|
|
|
@override
|
|
State<RegionFormDialog> createState() => _RegionFormDialogState();
|
|
}
|
|
|
|
class _RegionFormDialogState extends State<RegionFormDialog> {
|
|
final _form = GlobalKey<FormState>();
|
|
late final TextEditingController _name;
|
|
late final TextEditingController _code;
|
|
late final TextEditingController _remark;
|
|
late Set<int> _bound;
|
|
int? _defaultRoute;
|
|
String? _scopeError;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final draft = widget.cubit.state.draft;
|
|
_name = TextEditingController(text: draft.name);
|
|
_code = TextEditingController(text: draft.code);
|
|
_remark = TextEditingController(text: draft.remark);
|
|
_defaultRoute = draft.defaultRouteId;
|
|
_bound = draft.boundRouteIds.toSet();
|
|
widget.cubit.loadRoutes();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_name.dispose();
|
|
_code.dispose();
|
|
_remark.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
final state = widget.cubit.state;
|
|
if (state.isSaving || !_form.currentState!.validate()) return;
|
|
if (!widget.isScopeCurrent()) {
|
|
setState(() => _scopeError = '场站或账号已切换,请重新进入编辑器后保存');
|
|
return;
|
|
}
|
|
setState(() => _scopeError = null);
|
|
final draft = state.draft;
|
|
final saved = await widget.cubit.save(
|
|
RegionDraft(
|
|
id: draft.id,
|
|
siteId: draft.siteId,
|
|
name: _name.text,
|
|
code: _code.text,
|
|
remark: _remark.text,
|
|
enabled: draft.enabled,
|
|
defaultRouteId: _defaultRoute,
|
|
boundRouteIds: _bound.toList()..sort(),
|
|
scope: draft.scope,
|
|
),
|
|
);
|
|
if (!mounted) return;
|
|
if (saved) Navigator.of(context).pop(true);
|
|
}
|
|
|
|
Map<int, String> _options(RegionEditorState state) {
|
|
final result = {for (final route in state.routes) route.id: route.name};
|
|
for (final id in [..._bound, ?_defaultRoute]) {
|
|
result.putIfAbsent(id, () => '路线 #$id');
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Future<void> _pickBoundRoutes(Map<int, String> options) async {
|
|
FocusScope.of(context).unfocus();
|
|
final selected = {..._bound};
|
|
final result = await showDialog<Set<int>>(
|
|
context: context,
|
|
builder: (dialogContext) => StatefulBuilder(
|
|
builder: (_, update) => AlertDialog(
|
|
title: const Text('绑定路线'),
|
|
content: SizedBox(
|
|
width: 420,
|
|
height: (MediaQuery.sizeOf(dialogContext).height * 0.5).clamp(
|
|
80.0,
|
|
300.0,
|
|
),
|
|
child: options.isEmpty
|
|
? const Center(child: Text('当前场站暂无可绑定路线'))
|
|
: ListView(
|
|
children: [
|
|
for (final route in options.entries)
|
|
CheckboxListTile(
|
|
key: ValueKey('region_bind_${route.key}'),
|
|
value: selected.contains(route.key),
|
|
title: Text(route.value),
|
|
controlAffinity: ListTileControlAffinity.leading,
|
|
onChanged: (checked) => update(() {
|
|
if (checked == true) {
|
|
selected.add(route.key);
|
|
} else {
|
|
selected.remove(route.key);
|
|
}
|
|
}),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(),
|
|
child: const Text('取消'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(selected),
|
|
child: const Text('确定'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
if (result != null && mounted) setState(() => _bound = result);
|
|
}
|
|
|
|
InputDecoration _decoration(String label) => InputDecoration(
|
|
labelText: label,
|
|
isDense: true,
|
|
border: const OutlineInputBorder(),
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
|
|
);
|
|
|
|
@override
|
|
Widget build(
|
|
BuildContext context,
|
|
) => BlocBuilder<RegionEditorCubit, RegionEditorState>(
|
|
bloc: widget.cubit,
|
|
builder: (context, state) {
|
|
final options = _options(state);
|
|
final loading = state.routesStatus == RegionRoutesStatus.loading;
|
|
final available =
|
|
(MediaQuery.sizeOf(context).height -
|
|
MediaQuery.viewInsetsOf(context).bottom -
|
|
32)
|
|
.clamp(180.0, 640.0);
|
|
return PopScope(
|
|
canPop: !state.isSaving,
|
|
child: Dialog(
|
|
insetPadding: const EdgeInsets.symmetric(
|
|
horizontal: 24,
|
|
vertical: 16,
|
|
),
|
|
child: ConstrainedBox(
|
|
constraints: BoxConstraints(maxWidth: 640, maxHeight: available),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 12, 8, 0),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
state.draft.id == null ? '新建分区' : '编辑分区',
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.w800,
|
|
),
|
|
),
|
|
),
|
|
IconButton(
|
|
onPressed: state.isSaving
|
|
? null
|
|
: () => Navigator.of(context).pop(false),
|
|
icon: const Icon(Icons.close_rounded),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Flexible(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.fromLTRB(20, 12, 20, 8),
|
|
child: AbsorbPointer(
|
|
absorbing: state.isSaving,
|
|
child: Form(
|
|
key: _form,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
TextFormField(
|
|
key: const ValueKey('region_name'),
|
|
controller: _name,
|
|
decoration: _decoration('分区名称 *'),
|
|
validator: (value) =>
|
|
value == null || value.trim().isEmpty
|
|
? '请填写分区名称'
|
|
: null,
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
key: const ValueKey('region_code'),
|
|
controller: _code,
|
|
decoration: _decoration('分区编码'),
|
|
),
|
|
const SizedBox(height: 16),
|
|
if (loading) ...[
|
|
const LinearProgressIndicator(minHeight: 2),
|
|
const SizedBox(height: 10),
|
|
],
|
|
if (state.routesStatus ==
|
|
RegionRoutesStatus.failure)
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Text(
|
|
state.routesError ?? '路线加载失败',
|
|
style: const TextStyle(
|
|
color: Colors.red,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
),
|
|
TextButton(
|
|
onPressed: widget.cubit.loadRoutes,
|
|
child: const Text('重新加载路线'),
|
|
),
|
|
],
|
|
),
|
|
InputDecorator(
|
|
decoration: _decoration('默认路线'),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: DropdownButtonHideUnderline(
|
|
child: DropdownButton<int>(
|
|
key: const ValueKey(
|
|
'region_default_route',
|
|
),
|
|
value: _defaultRoute,
|
|
isExpanded: true,
|
|
isDense: true,
|
|
menuMaxHeight: 260,
|
|
hint: Text(
|
|
loading ? '路线加载中' : '请选择默认路线(可选)',
|
|
style: const TextStyle(fontSize: 13),
|
|
),
|
|
items: [
|
|
for (final route in options.entries)
|
|
DropdownMenuItem(
|
|
value: route.key,
|
|
child: Text(
|
|
route.value,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(
|
|
fontSize: 13,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
onChanged: loading
|
|
? null
|
|
: (value) => setState(
|
|
() => _defaultRoute = value,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
if (_defaultRoute != null)
|
|
InkWell(
|
|
onTap: () =>
|
|
setState(() => _defaultRoute = null),
|
|
child: const Padding(
|
|
padding: EdgeInsets.only(left: 8),
|
|
child: Icon(
|
|
Icons.close_rounded,
|
|
size: 17,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
InkWell(
|
|
key: const ValueKey('region_bound_routes'),
|
|
onTap: loading
|
|
? null
|
|
: () => _pickBoundRoutes(options),
|
|
child: InputDecorator(
|
|
decoration: _decoration('绑定路线'),
|
|
child: Wrap(
|
|
spacing: 6,
|
|
runSpacing: 4,
|
|
children: [
|
|
if (_bound.isEmpty)
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(
|
|
vertical: 4,
|
|
),
|
|
child: Text(
|
|
'请选择绑定路线(可多选)',
|
|
style: TextStyle(
|
|
fontSize: 13,
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
),
|
|
for (final id in _bound)
|
|
InputChip(
|
|
label: Text(
|
|
options[id] ?? '路线 #$id',
|
|
style: const TextStyle(fontSize: 12),
|
|
),
|
|
onDeleted: () =>
|
|
setState(() => _bound.remove(id)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextFormField(
|
|
key: const ValueKey('region_remark'),
|
|
controller: _remark,
|
|
minLines: 2,
|
|
maxLines: 3,
|
|
decoration: _decoration('备注'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
'当前区域范围点数:${state.draft.scope.length}/4',
|
|
style: TextStyle(
|
|
fontSize: 12,
|
|
color: state.draft.scopeError == null
|
|
? Colors.grey
|
|
: Colors.red,
|
|
),
|
|
),
|
|
if (state.draft.scopeError != null)
|
|
Text(
|
|
state.draft.scopeError!,
|
|
style: const TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.red,
|
|
),
|
|
),
|
|
if (_scopeError != null || state.error != null)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 10),
|
|
child: Text(
|
|
_scopeError ?? state.error!,
|
|
key: const ValueKey('region_save_error'),
|
|
style: const TextStyle(
|
|
color: Colors.red,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 8, 20, 16),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: [
|
|
TextButton(
|
|
onPressed: state.isSaving
|
|
? null
|
|
: () => Navigator.of(context).pop(false),
|
|
child: const Text('取消'),
|
|
),
|
|
const SizedBox(width: 12),
|
|
FilledButton(
|
|
key: const ValueKey('region_save'),
|
|
onPressed:
|
|
state.isSaving ||
|
|
state.savedWithoutId ||
|
|
state.draft.scopeError != null
|
|
? null
|
|
: _submit,
|
|
child: state.isSaving
|
|
? const SizedBox(
|
|
width: 18,
|
|
height: 18,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
color: Colors.white,
|
|
),
|
|
)
|
|
: const Text('确认保存'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|