一次大的提交
This commit is contained in:
@@ -4,23 +4,29 @@ import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../../../../core/network/tcp/tcp_client.dart';
|
||||
import '../../../home/domain/entities/site_entity.dart';
|
||||
import '../../../home/domain/usecases/get_site_list_usecase.dart';
|
||||
import '../../../../../core/app/app_user_cubit.dart';
|
||||
|
||||
class SiteState {
|
||||
final List<SiteEntity> sites;
|
||||
final SiteEntity? selectedSite;
|
||||
final bool isLoading;
|
||||
|
||||
const SiteState({
|
||||
this.sites = const [],
|
||||
this.selectedSite,
|
||||
this.isLoading = false,
|
||||
});
|
||||
|
||||
SiteState copyWith({
|
||||
List<SiteEntity>? sites,
|
||||
SiteEntity? selectedSite,
|
||||
bool? isLoading,
|
||||
}) {
|
||||
return SiteState(
|
||||
sites: sites ?? this.sites,
|
||||
selectedSite: selectedSite ?? this.selectedSite,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -28,9 +34,47 @@ class SiteState {
|
||||
class SiteCubit extends Cubit<SiteState> {
|
||||
final SharedPreferences sharedPreferences;
|
||||
static const String _selectedSiteIdKey = 'selected_site_id';
|
||||
bool _hasLoadedSites = false;
|
||||
|
||||
SiteCubit(this.sharedPreferences) : super(const SiteState());
|
||||
|
||||
/// 加载场站列表(首次调用时请求接口,之后不重复请求)
|
||||
Future<void> loadSites() async {
|
||||
if (_hasLoadedSites) return;
|
||||
_hasLoadedSites = true;
|
||||
|
||||
emit(state.copyWith(isLoading: true));
|
||||
|
||||
try {
|
||||
final userId = GetIt.I<AppUserCubit>().state.user?.userId;
|
||||
if (userId == null) {
|
||||
emit(state.copyWith(isLoading: false));
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await GetIt.I<GetSiteListUseCase>().call(userId);
|
||||
result.fold(
|
||||
(failure) {
|
||||
debugPrint('❌ [SiteCubit] 加载场站列表失败: ${failure.message}');
|
||||
emit(state.copyWith(isLoading: false));
|
||||
},
|
||||
(sites) {
|
||||
updateSites(sites);
|
||||
emit(state.copyWith(isLoading: false));
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [SiteCubit] 加载场站列表异常: $e');
|
||||
emit(state.copyWith(isLoading: false));
|
||||
}
|
||||
}
|
||||
|
||||
/// 强制重新加载场站列表
|
||||
Future<void> reloadSites() async {
|
||||
_hasLoadedSites = false;
|
||||
await loadSites();
|
||||
}
|
||||
|
||||
/// 更新场站列表
|
||||
void updateSites(List<SiteEntity> sites) {
|
||||
// 尝试恢复之前选中的场站
|
||||
@@ -52,8 +96,10 @@ class SiteCubit extends Cubit<SiteState> {
|
||||
/// 选择场站(持久化)
|
||||
void selectSite(SiteEntity site) {
|
||||
debugPrint('🏭 [SiteCubit] ========== 切换场站 ==========');
|
||||
debugPrint('🏭 [SiteCubit] 从 ${state.selectedSite?.siteName ?? "无"} 切换到 ${site.siteName}');
|
||||
|
||||
debugPrint(
|
||||
'🏭 [SiteCubit] 从 ${state.selectedSite?.siteName ?? "无"} 切换到 ${site.siteName}',
|
||||
);
|
||||
|
||||
// 🔥 关键修复:只有切换到不同场站时才断开TCP
|
||||
if (state.selectedSite?.id != site.id) {
|
||||
final tcpClient = GetIt.I<TcpClient>();
|
||||
@@ -67,7 +113,7 @@ class SiteCubit extends Cubit<SiteState> {
|
||||
} else {
|
||||
debugPrint('✅ [SiteCubit] 相同场站,保持TCP连接状态');
|
||||
}
|
||||
|
||||
|
||||
sharedPreferences.setInt(_selectedSiteIdKey, site.id);
|
||||
emit(state.copyWith(selectedSite: site));
|
||||
debugPrint('✅ [SiteCubit] 场站切换完成');
|
||||
@@ -81,6 +127,7 @@ class SiteCubit extends Cubit<SiteState> {
|
||||
|
||||
/// 清空所有场站数据(退出登录时调用)
|
||||
void clearAll() {
|
||||
_hasLoadedSites = false;
|
||||
sharedPreferences.remove(_selectedSiteIdKey);
|
||||
emit(const SiteState());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/site/presentation/cubit/site_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/domain/entities/site_entity.dart';
|
||||
|
||||
class SiteSelectorWidget extends StatefulWidget {
|
||||
final bool compact;
|
||||
final bool enabled;
|
||||
final void Function(SiteEntity site)? onSiteSelected;
|
||||
|
||||
const SiteSelectorWidget({
|
||||
super.key,
|
||||
this.compact = false,
|
||||
this.enabled = true,
|
||||
this.onSiteSelected,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SiteSelectorWidget> createState() => _SiteSelectorWidgetState();
|
||||
}
|
||||
|
||||
class _SiteSelectorWidgetState extends State<SiteSelectorWidget> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
GetIt.I<SiteCubit>().loadSites();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return StreamBuilder<SiteState>(
|
||||
stream: GetIt.I<SiteCubit>().stream,
|
||||
builder: (context, snapshot) {
|
||||
final siteState = snapshot.data ?? GetIt.I<SiteCubit>().state;
|
||||
final selectedSite = siteState.selectedSite;
|
||||
final sites = siteState.sites;
|
||||
|
||||
if (widget.compact) {
|
||||
return _buildCompact(context, selectedSite, sites);
|
||||
}
|
||||
return _buildNormal(context, selectedSite, sites);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 紧凑模式(用于空间有限的场景)
|
||||
Widget _buildCompact(
|
||||
BuildContext context,
|
||||
SiteEntity? selectedSite,
|
||||
List<SiteEntity> sites,
|
||||
) {
|
||||
const iconSize = 16.0;
|
||||
const spacing = 2.0;
|
||||
final text = selectedSite?.siteName ?? '请选择场站';
|
||||
|
||||
return InkWell(
|
||||
onTap: widget.enabled ? () => _showSitePicker(context, sites) : null,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final maxTextWidth = constraints.maxWidth - iconSize - spacing;
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: maxTextWidth > 0 ? maxTextWidth : 0,
|
||||
),
|
||||
child: Text(
|
||||
text,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: spacing),
|
||||
Icon(
|
||||
Icons.keyboard_arrow_down,
|
||||
size: iconSize,
|
||||
color: widget.enabled
|
||||
? const Color(0xFF4E5969)
|
||||
: const Color(0xFFC9CDD4),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 普通模式
|
||||
Widget _buildNormal(
|
||||
BuildContext context,
|
||||
SiteEntity? selectedSite,
|
||||
List<SiteEntity> sites,
|
||||
) {
|
||||
const iconSize = 18.0;
|
||||
const spacing = 3.0;
|
||||
final text = selectedSite?.siteName ?? '请选择场站';
|
||||
|
||||
return InkWell(
|
||||
onTap: widget.enabled ? () => _showSitePicker(context, sites) : null,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final maxTextWidth = constraints.maxWidth - iconSize - spacing;
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxWidth: maxTextWidth > 0 ? maxTextWidth : 0,
|
||||
),
|
||||
child: Text(
|
||||
text,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: spacing),
|
||||
Icon(
|
||||
Icons.keyboard_arrow_down,
|
||||
size: iconSize,
|
||||
color: widget.enabled
|
||||
? const Color(0xFF4E5969)
|
||||
: const Color(0xFFC9CDD4),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 弹出场站选择底部弹窗
|
||||
void _showSitePicker(BuildContext context, List<SiteEntity> sites) {
|
||||
if (sites.isEmpty) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(const SnackBar(content: Text('暂无场站数据')));
|
||||
return;
|
||||
}
|
||||
|
||||
final siteCubit = GetIt.I<SiteCubit>();
|
||||
final currentSiteId = siteCubit.state.selectedSite?.id;
|
||||
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
backgroundColor: Colors.white,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
||||
),
|
||||
builder: (context) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
constraints: BoxConstraints(
|
||||
maxHeight: MediaQuery.of(context).size.height * 0.6,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 标题
|
||||
Container(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
const Spacer(),
|
||||
const Text(
|
||||
'选择场站',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
GestureDetector(
|
||||
onTap: () => Navigator.pop(context),
|
||||
child: const Icon(
|
||||
Icons.close,
|
||||
size: 22,
|
||||
color: Color(0xFF86909C),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
const SizedBox(height: 8),
|
||||
// 场站列表
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: sites.length,
|
||||
itemBuilder: (context, index) {
|
||||
final site = sites[index];
|
||||
final isSelected = currentSiteId == site.id;
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected
|
||||
? const Color(0xFFE8F3FF)
|
||||
: Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ListTile(
|
||||
dense: true,
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 2,
|
||||
),
|
||||
title: Text(
|
||||
site.siteName,
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: isSelected
|
||||
? FontWeight.w600
|
||||
: FontWeight.w400,
|
||||
color: isSelected
|
||||
? const Color(0xFF165DFF)
|
||||
: const Color(0xFF1D2129),
|
||||
),
|
||||
),
|
||||
trailing: isSelected
|
||||
? const Icon(
|
||||
Icons.check,
|
||||
size: 20,
|
||||
color: Color(0xFF165DFF),
|
||||
)
|
||||
: null,
|
||||
onTap: () {
|
||||
siteCubit.selectSite(site);
|
||||
widget.onSiteSelected?.call(site);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user