分区相关
This commit is contained in:
190
lib/features/regions/presentation/cubit/region_editor_cubit.dart
Normal file
190
lib/features/regions/presentation/cubit/region_editor_cubit.dart
Normal file
@@ -0,0 +1,190 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../domain/entities/region_draft.dart';
|
||||
import '../../domain/usecases/region_usecases.dart';
|
||||
|
||||
enum RegionEditorStatus { editing, saving, saved, failure }
|
||||
|
||||
enum RegionRoutesStatus { idle, loading, ready, failure }
|
||||
|
||||
class RegionEditorState {
|
||||
RegionEditorState({
|
||||
required this.draft,
|
||||
this.status = RegionEditorStatus.editing,
|
||||
this.routesStatus = RegionRoutesStatus.idle,
|
||||
this.routes = const [],
|
||||
this.error,
|
||||
this.routesError,
|
||||
});
|
||||
final RegionDraft draft;
|
||||
final RegionEditorStatus status;
|
||||
final RegionRoutesStatus routesStatus;
|
||||
final List<RegionRouteOption> routes;
|
||||
final String? error;
|
||||
final String? routesError;
|
||||
bool get isSaving => status == RegionEditorStatus.saving;
|
||||
bool get savedWithoutId =>
|
||||
status == RegionEditorStatus.saved && draft.id == null;
|
||||
|
||||
RegionEditorState copyWith({
|
||||
RegionDraft? draft,
|
||||
RegionEditorStatus? status,
|
||||
RegionRoutesStatus? routesStatus,
|
||||
List<RegionRouteOption>? routes,
|
||||
String? error,
|
||||
String? routesError,
|
||||
bool clearError = false,
|
||||
bool clearRoutesError = false,
|
||||
}) => RegionEditorState(
|
||||
draft: draft ?? this.draft,
|
||||
status: status ?? this.status,
|
||||
routesStatus: routesStatus ?? this.routesStatus,
|
||||
routes: routes ?? this.routes,
|
||||
error: clearError ? null : error ?? this.error,
|
||||
routesError: clearRoutesError ? null : routesError ?? this.routesError,
|
||||
);
|
||||
}
|
||||
|
||||
/// 可被地图编辑器及分区管理复用;点位与路线编辑草稿、障碍物、返航点完全独立。
|
||||
class RegionEditorCubit extends Cubit<RegionEditorState> {
|
||||
RegionEditorCubit(this._save, this._loadRoutes)
|
||||
: super(RegionEditorState(draft: RegionDraft(siteId: 0)));
|
||||
final SaveRegionUseCase _save;
|
||||
final LoadRegionRoutesUseCase _loadRoutes;
|
||||
int _generation = 0;
|
||||
|
||||
void initialize(RegionDraft draft) {
|
||||
if (state.isSaving) return;
|
||||
++_generation;
|
||||
emit(RegionEditorState(draft: draft));
|
||||
}
|
||||
|
||||
String? addPoint(RegionPoint point) {
|
||||
if (state.isSaving) return '分区正在保存';
|
||||
if (state.savedWithoutId) return '分区已保存,请点「新建」绘制下一个分区';
|
||||
if (!point.isValid) return '打点经纬度无效';
|
||||
if (state.draft.scope.length >= 4) return '分区只需要 4 个打点,请撤回或删除后重打';
|
||||
if (state.draft.scope.any(
|
||||
(p) => p.lat == point.lat && p.lng == point.lng,
|
||||
)) {
|
||||
return '该位置已打点,请移动到下一个位置';
|
||||
}
|
||||
emit(
|
||||
state.copyWith(
|
||||
draft: state.draft.withScope([...state.draft.scope, point]),
|
||||
status: RegionEditorStatus.editing,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
void removePoint(int index) {
|
||||
if (state.isSaving ||
|
||||
state.savedWithoutId ||
|
||||
index < 0 ||
|
||||
index >= state.draft.scope.length) {
|
||||
return;
|
||||
}
|
||||
final points = [...state.draft.scope]..removeAt(index);
|
||||
emit(
|
||||
state.copyWith(
|
||||
draft: state.draft.withScope(points),
|
||||
status: RegionEditorStatus.editing,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> loadRoutes() async {
|
||||
if (state.routesStatus == RegionRoutesStatus.loading || isClosed) return;
|
||||
final generation = _generation;
|
||||
final siteId = state.draft.siteId;
|
||||
emit(
|
||||
state.copyWith(
|
||||
routesStatus: RegionRoutesStatus.loading,
|
||||
clearRoutesError: true,
|
||||
),
|
||||
);
|
||||
try {
|
||||
final result = await _loadRoutes(siteId);
|
||||
if (isClosed || generation != _generation) return;
|
||||
result.fold(
|
||||
(failure) => emit(
|
||||
state.copyWith(
|
||||
routesStatus: RegionRoutesStatus.failure,
|
||||
routesError: failure.message,
|
||||
),
|
||||
),
|
||||
(routes) => emit(
|
||||
state.copyWith(
|
||||
routesStatus: RegionRoutesStatus.ready,
|
||||
routes: List.unmodifiable(routes),
|
||||
clearRoutesError: true,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
if (!isClosed && generation == _generation) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
routesStatus: RegionRoutesStatus.failure,
|
||||
routesError: '$error',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> save(RegionDraft draft) async {
|
||||
if (state.isSaving || state.savedWithoutId) return false;
|
||||
if (draft.siteId != state.draft.siteId || draft.id != state.draft.id) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: RegionEditorStatus.failure,
|
||||
error: '分区或场站已切换,请重新打开编辑',
|
||||
),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
final generation = _generation;
|
||||
emit(
|
||||
state.copyWith(
|
||||
draft: draft,
|
||||
status: RegionEditorStatus.saving,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
try {
|
||||
final result = await _save(draft);
|
||||
if (isClosed || generation != _generation) return false;
|
||||
return result.fold(
|
||||
(failure) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: RegionEditorStatus.failure,
|
||||
error: failure.message,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
},
|
||||
(receipt) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
draft: draft.withId(receipt.id ?? draft.id),
|
||||
status: RegionEditorStatus.saved,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
if (!isClosed && generation == _generation) {
|
||||
emit(
|
||||
state.copyWith(status: RegionEditorStatus.failure, error: '$error'),
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
183
lib/features/regions/presentation/cubit/region_list_cubit.dart
Normal file
183
lib/features/regions/presentation/cubit/region_list_cubit.dart
Normal file
@@ -0,0 +1,183 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../domain/entities/region_draft.dart';
|
||||
import '../../domain/usecases/region_usecases.dart';
|
||||
|
||||
enum RegionListStatus { idle, loading, ready, failure, noSite, noUser }
|
||||
|
||||
class RegionListState {
|
||||
const RegionListState({
|
||||
this.status = RegionListStatus.idle,
|
||||
this.regions = const [],
|
||||
this.siteId,
|
||||
this.selectedId,
|
||||
this.deletingId,
|
||||
this.error,
|
||||
this.operationError,
|
||||
});
|
||||
final RegionListStatus status;
|
||||
final List<RegionDraft> regions;
|
||||
final int? siteId;
|
||||
final int? selectedId;
|
||||
final int? deletingId;
|
||||
final String? error;
|
||||
final String? operationError;
|
||||
|
||||
RegionListState copyWith({
|
||||
List<RegionDraft>? regions,
|
||||
int? selectedId,
|
||||
bool clearSelection = false,
|
||||
int? deletingId,
|
||||
bool clearDeleting = false,
|
||||
String? operationError,
|
||||
}) => RegionListState(
|
||||
status: status,
|
||||
siteId: siteId,
|
||||
regions: regions ?? this.regions,
|
||||
selectedId: clearSelection ? null : selectedId ?? this.selectedId,
|
||||
deletingId: clearDeleting ? null : deletingId ?? this.deletingId,
|
||||
error: error,
|
||||
operationError: operationError,
|
||||
);
|
||||
}
|
||||
|
||||
class RegionListCubit extends Cubit<RegionListState> {
|
||||
RegionListCubit(this._load, this._delete) : super(const RegionListState());
|
||||
final LoadRegionsUseCase _load;
|
||||
final DeleteRegionsUseCase _delete;
|
||||
(String, int)? _scope;
|
||||
int _generation = 0;
|
||||
Future<void>? _pending;
|
||||
Object? get scopeKey => _scope;
|
||||
|
||||
Future<void> load({
|
||||
required int? siteId,
|
||||
required String? ownerId,
|
||||
bool force = false,
|
||||
}) {
|
||||
if (isClosed) return Future<void>.value();
|
||||
if (ownerId == null || ownerId.isEmpty || siteId == null || siteId <= 0) {
|
||||
++_generation;
|
||||
_scope = null;
|
||||
_pending = null;
|
||||
emit(
|
||||
RegionListState(
|
||||
status: ownerId == null || ownerId.isEmpty
|
||||
? RegionListStatus.noUser
|
||||
: RegionListStatus.noSite,
|
||||
),
|
||||
);
|
||||
return Future<void>.value();
|
||||
}
|
||||
final scope = (ownerId, siteId);
|
||||
if (_scope == scope && _pending != null && !force) return _pending!;
|
||||
if (_scope == scope && state.deletingId != null) {
|
||||
return Future<void>.value();
|
||||
}
|
||||
final selectedId = _scope == scope ? state.selectedId : null;
|
||||
final generation = ++_generation;
|
||||
_scope = scope;
|
||||
emit(RegionListState(status: RegionListStatus.loading, siteId: siteId));
|
||||
return _pending = _fetch(scope, generation, selectedId);
|
||||
}
|
||||
|
||||
Future<void> _fetch(
|
||||
(String, int) scope,
|
||||
int generation,
|
||||
int? selectedId,
|
||||
) async {
|
||||
try {
|
||||
final result = await _load(scope.$2);
|
||||
if (isClosed || generation != _generation) return;
|
||||
result.fold(
|
||||
(failure) => emit(
|
||||
RegionListState(
|
||||
status: RegionListStatus.failure,
|
||||
siteId: scope.$2,
|
||||
error: failure.message,
|
||||
),
|
||||
),
|
||||
(regions) => emit(
|
||||
RegionListState(
|
||||
status: RegionListStatus.ready,
|
||||
siteId: scope.$2,
|
||||
regions: List.unmodifiable(regions),
|
||||
selectedId: regions.any((r) => r.id == selectedId)
|
||||
? selectedId
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
if (!isClosed && generation == _generation) {
|
||||
emit(
|
||||
RegionListState(
|
||||
status: RegionListStatus.failure,
|
||||
siteId: scope.$2,
|
||||
error: '$error',
|
||||
),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (generation == _generation) _pending = null;
|
||||
}
|
||||
}
|
||||
|
||||
void select(int? id) {
|
||||
if (isClosed || state.status != RegionListStatus.ready) return;
|
||||
if (id == null) {
|
||||
emit(state.copyWith(clearSelection: true));
|
||||
return;
|
||||
}
|
||||
if (state.regions.any((r) => r.id == id)) {
|
||||
emit(state.copyWith(selectedId: id));
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> deleteRegion(RegionDraft region) async {
|
||||
if (isClosed ||
|
||||
state.deletingId != null ||
|
||||
region.id == null ||
|
||||
region.siteId != state.siteId ||
|
||||
!state.regions.any((r) => r.id == region.id)) {
|
||||
return false;
|
||||
}
|
||||
final generation = _generation;
|
||||
emit(state.copyWith(deletingId: region.id));
|
||||
try {
|
||||
final result = await _delete([region.id!]);
|
||||
if (isClosed || generation != _generation) return false;
|
||||
return result.fold(
|
||||
(failure) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
clearDeleting: true,
|
||||
operationError: failure.message,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
},
|
||||
(deleted) {
|
||||
if (!deleted) {
|
||||
emit(
|
||||
state.copyWith(clearDeleting: true, operationError: '分区未删除,请重试'),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
emit(
|
||||
state.copyWith(
|
||||
regions: state.regions.where((r) => r.id != region.id).toList(),
|
||||
clearDeleting: true,
|
||||
clearSelection: state.selectedId == region.id,
|
||||
),
|
||||
);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
if (!isClosed && generation == _generation) {
|
||||
emit(state.copyWith(clearDeleting: true, operationError: '$error'));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
/// 独立分区图层,不复用或修改圈地路线的草稿点、障碍和返航线。
|
||||
class RegionBoundaryLayer extends StatelessWidget {
|
||||
const RegionBoundaryLayer({
|
||||
super.key,
|
||||
required this.points,
|
||||
this.selected = true,
|
||||
this.enabled = true,
|
||||
this.showVertices = true,
|
||||
this.label,
|
||||
});
|
||||
final List<LatLng> points;
|
||||
final bool selected;
|
||||
final bool enabled;
|
||||
final bool showVertices;
|
||||
final String? label;
|
||||
Color get _color => enabled ? const Color(0xFF1C96F6) : Colors.grey;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
if (points.length == 4)
|
||||
PolygonLayer(
|
||||
polygons: [
|
||||
Polygon(
|
||||
points: points,
|
||||
color: _color.withValues(alpha: selected ? 0.28 : 0.12),
|
||||
borderColor: _color,
|
||||
borderStrokeWidth: selected ? 3 : 1.5,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (points.length > 1 && points.length < 4)
|
||||
PolylineLayer(
|
||||
polylines: [Polyline(points: points, color: _color, strokeWidth: 2)],
|
||||
),
|
||||
MarkerLayer(
|
||||
markers: [
|
||||
if (showVertices)
|
||||
for (var index = 0; index < points.length; index++)
|
||||
Marker(
|
||||
point: points[index],
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: Container(
|
||||
alignment: Alignment.center,
|
||||
decoration: BoxDecoration(
|
||||
color: _color,
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: Colors.white, width: 2),
|
||||
),
|
||||
child: Text(
|
||||
'${index + 1}',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w800,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (label != null && label!.isNotEmpty && points.isNotEmpty)
|
||||
Marker(
|
||||
point: LatLngBounds.fromPoints(points).center,
|
||||
width: 140,
|
||||
height: 32,
|
||||
child: Center(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8,
|
||||
vertical: 4,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: _color.withValues(alpha: selected ? 0.95 : 0.75),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Text(
|
||||
label!,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,427 @@
|
||||
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('确认保存'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
296
lib/features/regions/presentation/widgets/region_list_panel.dart
Normal file
296
lib/features/regions/presentation/widgets/region_list_panel.dart
Normal file
@@ -0,0 +1,296 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import '../../../../components/capsule_toast.dart';
|
||||
import '../../domain/entities/region_draft.dart';
|
||||
import '../cubit/region_list_cubit.dart';
|
||||
|
||||
/// 地图分区入口、竖屏区域管理共享的真实列表与操作界面。
|
||||
class RegionListPanel extends StatelessWidget {
|
||||
const RegionListPanel({
|
||||
super.key,
|
||||
required this.cubit,
|
||||
required this.onCreate,
|
||||
required this.onEdit,
|
||||
required this.onRefresh,
|
||||
required this.currentScope,
|
||||
this.onSelect,
|
||||
this.onDeleted,
|
||||
this.onClose,
|
||||
});
|
||||
final RegionListCubit cubit;
|
||||
final VoidCallback onCreate;
|
||||
final ValueChanged<RegionDraft> onEdit;
|
||||
final ValueChanged<RegionDraft>? onSelect;
|
||||
final ValueChanged<RegionDraft>? onDeleted;
|
||||
final VoidCallback onRefresh;
|
||||
final VoidCallback? onClose;
|
||||
final Object? Function() currentScope;
|
||||
|
||||
Future<void> _delete(BuildContext context, RegionDraft region) async {
|
||||
final scope = currentScope();
|
||||
if (scope != cubit.scopeKey) return;
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text('删除分区'),
|
||||
content: Text(
|
||||
'确定删除「${region.name.isEmpty ? '分区 #${region.id}' : region.name}」?删除后无法恢复。',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
FilledButton(
|
||||
key: const ValueKey('region_delete_confirm'),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: const Text('删除'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (!context.mounted || confirmed != true) return;
|
||||
if (currentScope() != scope || cubit.scopeKey != scope) {
|
||||
CapsuleToast.show('场站或账号已切换,请重新选择分区', showCheck: false);
|
||||
return;
|
||||
}
|
||||
final deleted = await cubit.deleteRegion(region);
|
||||
if (!context.mounted || !deleted) return;
|
||||
onDeleted?.call(region);
|
||||
CapsuleToast.show('分区已删除');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(
|
||||
BuildContext context,
|
||||
) => BlocBuilder<RegionListCubit, RegionListState>(
|
||||
bloc: cubit,
|
||||
builder: (context, state) {
|
||||
final busy = state.deletingId != null;
|
||||
final scoped = state.siteId != null && currentScope() == cubit.scopeKey;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 6, 4, 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
state.status == RegionListStatus.ready
|
||||
? '分区列表(${state.regions.length})'
|
||||
: '分区列表',
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: Color(0xFF1A2B4A),
|
||||
),
|
||||
),
|
||||
),
|
||||
TextButton.icon(
|
||||
key: const ValueKey('region_list_create'),
|
||||
onPressed: !scoped || busy ? null : onCreate,
|
||||
icon: const Icon(Icons.add_rounded, size: 17),
|
||||
label: const Text('新增'),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: '刷新分区',
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: busy || state.status == RegionListStatus.loading
|
||||
? null
|
||||
: onRefresh,
|
||||
icon: const Icon(Icons.refresh_rounded, size: 19),
|
||||
),
|
||||
if (onClose != null)
|
||||
IconButton(
|
||||
tooltip: '关闭列表',
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: busy ? null : onClose,
|
||||
icon: const Icon(Icons.close_rounded, size: 19),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (state.operationError != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
child: Text(
|
||||
state.operationError!,
|
||||
key: const ValueKey('region_delete_error'),
|
||||
style: const TextStyle(fontSize: 12, color: Colors.red),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: switch (state.status) {
|
||||
RegionListStatus.loading || RegionListStatus.idle => const Center(
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
),
|
||||
RegionListStatus.noUser => const Center(child: Text('请先登录')),
|
||||
RegionListStatus.noSite => const Center(
|
||||
child: Text('请选择场站后查看分区'),
|
||||
),
|
||||
RegionListStatus.failure => Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
state.error ?? '分区加载失败',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextButton(
|
||||
onPressed: onRefresh,
|
||||
child: const Text('重新加载分区'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
RegionListStatus.ready =>
|
||||
state.regions.isEmpty
|
||||
? const Center(child: Text('当前场站暂无分区,可点击「新增」'))
|
||||
: ListView.separated(
|
||||
padding: const EdgeInsets.fromLTRB(10, 4, 10, 16),
|
||||
itemCount: state.regions.length,
|
||||
separatorBuilder: (_, _) => const SizedBox(height: 8),
|
||||
itemBuilder: (context, index) {
|
||||
final region = state.regions[index];
|
||||
final selected = state.selectedId == region.id;
|
||||
return Material(
|
||||
key: ValueKey('region_row_${region.id}'),
|
||||
color: selected
|
||||
? const Color(0xFFEAF3FE)
|
||||
: Colors.white,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: selected
|
||||
? const Color(0xFF1C96F6)
|
||||
: const Color(0xFFE5EDF7),
|
||||
),
|
||||
),
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
onTap: !scoped || busy
|
||||
? null
|
||||
: () {
|
||||
cubit.select(region.id);
|
||||
onSelect?.call(region);
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(
|
||||
12,
|
||||
10,
|
||||
8,
|
||||
4,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment:
|
||||
CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
region.name.isEmpty
|
||||
? '分区 #${region.id}'
|
||||
: region.name,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.w800,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Text(
|
||||
region.enabled ? '启用' : '停用',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: region.enabled
|
||||
? const Color(0xFF1D9E57)
|
||||
: Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (region.code.isNotEmpty) ...[
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
'编码:${region.code}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
'${region.scope.length} 个范围点 · ${region.boundRouteIds.length} 条绑定路线',
|
||||
style: const TextStyle(
|
||||
fontSize: 11.5,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
if (region.scopeError != null)
|
||||
const Text(
|
||||
'范围需重新打点',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: Colors.orange,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
TextButton(
|
||||
key: ValueKey(
|
||||
'region_edit_${region.id}',
|
||||
),
|
||||
onPressed: !scoped || busy
|
||||
? null
|
||||
: () => onEdit(region),
|
||||
child: const Text('修改'),
|
||||
),
|
||||
TextButton(
|
||||
key: ValueKey(
|
||||
'region_delete_${region.id}',
|
||||
),
|
||||
onPressed: !scoped || busy
|
||||
? null
|
||||
: () => _delete(context, region),
|
||||
child: state.deletingId == region.id
|
||||
? const SizedBox(
|
||||
width: 14,
|
||||
height: 14,
|
||||
child:
|
||||
CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
),
|
||||
)
|
||||
: const Text(
|
||||
'删除',
|
||||
style: TextStyle(
|
||||
color: Color(0xFFD54941),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user