Files
feature-tenant/lib/features/main_container/presentation/main_wrapper.dart

284 lines
12 KiB
Dart
Raw Normal View History

2026-01-18 20:21:14 +08:00
import 'package:flutter/material.dart';
2026-05-13 10:34:07 +08:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
2026-05-13 10:34:07 +08:00
import 'package:maibu_satabot_v2/features/main_container/presentation/cubit/tab_config_cubit.dart';
import 'package:maibu_satabot_v2/features/main_container/domain/tab_config.dart';
import 'package:maibu_satabot_v2/features/home/presentation/pages/home_page.dart';
2026-05-18 17:18:58 +08:00
import 'package:maibu_satabot_v2/features/v2/home/presentation/pages/home_v2_page.dart';
import 'package:maibu_satabot_v2/features/v2/home/presentation/bloc/home_v2_bloc.dart';
import 'package:maibu_satabot_v2/features/v2/device_list/presentation/pages/device_status_page.dart';
2026-05-13 10:34:07 +08:00
import 'package:maibu_satabot_v2/features/ai/presentation/pages/ai_page.dart';
import 'package:maibu_satabot_v2/features/v2/waring_center/presentation/pages/alarm_center_page.dart';
import 'package:maibu_satabot_v2/features/v2/waring_center/presentation/bloc/alarm_cubit.dart';
2026-05-13 10:34:07 +08:00
import 'package:maibu_satabot_v2/features/my/presentation/pages/my_page.dart';
import 'package:maibu_satabot_v2/features/v2/my/presentation/pages/my_page.dart'
as my_v2;
import 'package:maibu_satabot_v2/features/v2/workorder/presentation/pages/workorder_page.dart';
import 'package:maibu_satabot_v2/features/v2/report/presentation/pages/report_page.dart';
import 'package:maibu_satabot_v2/features/v2/device_list/presentation/float_bar/view/float_bar_widget.dart';
import 'package:maibu_satabot_v2/features/v2/device_list/presentation/float_bar/cubit/float_bar_setting_cubit.dart';
2026-05-18 17:18:58 +08:00
import 'package:maibu_satabot_v2/core/di/injection.dart';
2026-08-18 08:52:33 +08:00
import 'package:maibu_satabot_v2/core/theme/AppTheme.dart';
2026-09-18 16:59:32 +08:00
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
2026-01-18 20:21:14 +08:00
2026-05-13 10:34:07 +08:00
class MainWrapper extends StatefulWidget {
const MainWrapper({super.key});
2026-01-18 20:21:14 +08:00
2026-05-13 10:34:07 +08:00
@override
State<MainWrapper> createState() => _MainWrapperState();
}
class _MainWrapperState extends State<MainWrapper> {
int _currentIndex = 0;
2026-09-18 16:59:32 +08:00
/// 上一次渲染的 Tab 数量:用于检测 Tab 集合变化(角色过滤/增删/热重载),
/// 变化时重置索引,避免停留在越界页导致 body 白屏。
int _lastItemCount = -1;
/// 底部导航:单个 Tab 的期望宽度与圆形指示器直径
static const double _kTabWidth = 88;
static const double _kIndicatorSize = 44;
@override
void initState() {
super.initState();
// 🔥 确保 FloatBarSettingService 已初始化
GetIt.I<FloatBarSettingService>();
2026-05-13 10:34:07 +08:00
}
void _onTabChanged(int index) {
setState(() {
_currentIndex = index;
});
}
2026-05-13 10:34:07 +08:00
IconData _getIconData(String iconName) {
switch (iconName) {
2026-05-18 17:18:58 +08:00
case 'home_rounded':
return Icons.home_rounded;
2026-05-13 10:34:07 +08:00
case 'grid_view_rounded':
return Icons.grid_view_rounded;
case 'devices_rounded':
return Icons.devices_rounded;
2026-05-13 10:34:07 +08:00
case 'auto_awesome_rounded':
return Icons.auto_awesome_rounded;
case 'warning_amber_rounded':
return Icons.warning_amber_rounded;
case 'assignment_rounded':
return Icons.assignment_rounded;
case 'post_add_rounded':
return Icons.post_add_rounded;
2026-05-13 10:34:07 +08:00
case 'person_rounded':
return Icons.person_rounded;
default:
return Icons.home_rounded;
}
}
Widget _buildCurrentPage(TabConfigItem tab) {
switch (tab.id) {
2026-05-18 17:18:58 +08:00
case 'home_v2':
return BlocProvider(
create: (_) => sl<HomeV2Bloc>(),
2026-05-18 17:18:58 +08:00
child: const HomeV2Page(),
);
2026-05-13 10:34:07 +08:00
case 'home':
return const HomePage();
case 'device':
return const DeviceStatusPage();
2026-05-13 10:34:07 +08:00
case 'ai':
return const AiPage();
case 'warning':
return BlocProvider(
create: (_) => sl<AlarmCubit>(),
child: const AlarmCenterPage(),
);
case 'workorder':
return const WorkOrderPage();
case 'report':
return const ReportPage();
2026-05-13 10:34:07 +08:00
case 'my':
return const MyPage();
case 'me':
return const my_v2.MyPage();
2026-05-13 10:34:07 +08:00
default:
return const HomePage();
}
}
2026-01-18 20:21:14 +08:00
@override
Widget build(BuildContext context) {
2026-09-18 16:59:32 +08:00
final roleKey = context.watch<AppUserCubit>().state.user?.roleKey;
2026-05-13 10:34:07 +08:00
return BlocBuilder<TabConfigCubit, TabConfigState>(
builder: (context, state) {
if (state is! TabConfigLoaded) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
2026-05-13 10:34:07 +08:00
}
final config = state.config;
2026-09-18 16:59:32 +08:00
final enabledItems = config.navItemsForRole(roleKey);
2026-05-13 10:34:07 +08:00
if (enabledItems.isEmpty) {
return const Scaffold(body: Center(child: Text('请至少启用一个 Tab')));
2026-05-13 10:34:07 +08:00
}
2026-09-18 16:59:32 +08:00
// Tab 集合变化(角色过滤 / 切换账号 / 增删 Tab / 热重载)时子页数量随之改变。
// 用 IndexedStack 承载:不依赖 ScrollController / 滚动偏移,
// 数量变化时只会按 index 取页,绝不会出现"控制器越界 → body 白屏"。
final itemCount = enabledItems.length;
if (_lastItemCount != itemCount) {
_lastItemCount = itemCount;
2026-05-13 10:34:07 +08:00
_currentIndex = 0;
}
2026-09-18 16:59:32 +08:00
if (_currentIndex >= itemCount) {
_currentIndex = 0;
}
2026-05-13 10:34:07 +08:00
return Scaffold(
extendBody: false,
2026-09-18 16:59:32 +08:00
// 底部导航预留区(SafeArea 那条)与页面同色,消除突兀的底色带
backgroundColor: context.appColors.pageBackground,
body: Stack(
children: [
2026-09-18 16:59:32 +08:00
IndexedStack(
index: _currentIndex,
children: enabledItems
.map((tab) => _buildCurrentPage(tab))
.toList(),
),
// 悬浮条 - 可拖动的悬浮按钮
ValueListenableBuilder<bool>(
valueListenable: FloatBarSettingService.settingNotifier,
builder: (context, isEnabled, child) {
return Visibility(
visible: isEnabled,
child: const FloatBarWidget(),
);
},
),
],
2026-05-13 10:34:07 +08:00
),
2026-09-18 16:59:32 +08:00
bottomNavigationBar: SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
child: LayoutBuilder(
builder: (context, constraints) {
2026-09-18 16:59:32 +08:00
final count = enabledItems.length;
// 预留胶囊左右内边距(6+6);Tab 少时收紧居中,Tab 多时最多铺满可用宽度
final available = constraints.maxWidth - 12;
final desired = count * _kTabWidth;
final contentWidth = desired > available ? available : desired;
final tabWidth = contentWidth / count;
return Align(
alignment: Alignment.center,
// 关键:bottomNavigationBar 在部分布局下会拿到「有界高度」,
// Align 默认会撑满该高度 → 胶囊被垂直居中到屏幕中部、body 被
// 压到顶部几乎不可见(“内容压到上边了”)。heightFactor:1.0 强制
// Align 垂直方向按子组件高度收缩,底栏只占自身高度、稳居底部。
heightFactor: 1.0,
child: Container(
width: contentWidth + 12,
padding: const EdgeInsets.symmetric(
horizontal: 6,
vertical: 2,
),
2026-09-18 16:59:32 +08:00
decoration: BoxDecoration(
color: context.appColors.cardBackground,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: context.appColors.divider,
width: 1,
),
2026-09-18 16:59:32 +08:00
boxShadow: [
BoxShadow(
color: context.appColors.cardShadow,
blurRadius: 10,
offset: const Offset(0, 2),
),
],
),
child: Stack(
children: [
// 滑动圆形指示器(纯数学定位,杜绝错位/抖动)
AnimatedPositioned(
2026-09-18 16:59:32 +08:00
duration: const Duration(milliseconds: 280),
curve: Curves.easeOutCubic,
left:
_currentIndex * tabWidth +
(tabWidth - _kIndicatorSize) / 2,
top: 6,
child: Container(
2026-09-18 16:59:32 +08:00
width: _kIndicatorSize,
height: _kIndicatorSize,
decoration: BoxDecoration(
2026-09-18 16:59:32 +08:00
color: context.appColors.primary.withOpacity(
0.12,
),
2026-09-18 16:59:32 +08:00
shape: BoxShape.circle,
),
2026-05-18 17:18:58 +08:00
),
),
2026-09-18 16:59:32 +08:00
// Tab 项(用 Expanded 弹性平分,避免窄屏下固定宽度溢出)
Row(
children: enabledItems.asMap().entries.map((entry) {
final index = entry.key;
final item = entry.value;
final isSelected = _currentIndex == index;
2026-09-18 16:59:32 +08:00
return Expanded(
child: InkWell(
onTap: () => _onTabChanged(index),
borderRadius: BorderRadius.circular(20),
child: SizedBox(
height: 56,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Icon(
_getIconData(item.icon),
size: 24,
color: isSelected
2026-09-18 16:59:32 +08:00
? context.appColors.primary
2026-08-18 08:52:33 +08:00
: context.appColors.textTertiary,
),
2026-09-18 16:59:32 +08:00
const SizedBox(height: 4),
Text(
item.name,
style: TextStyle(
fontSize: 11,
color: isSelected
? context.appColors.primary
: context.appColors.textTertiary,
fontWeight: isSelected
? FontWeight.w600
: FontWeight.w400,
),
),
],
),
),
),
2026-09-18 16:59:32 +08:00
);
}).toList(),
),
],
),
2026-01-18 20:21:14 +08:00
),
2026-05-18 17:18:58 +08:00
);
},
2026-01-18 20:21:14 +08:00
),
),
),
2026-05-13 10:34:07 +08:00
);
},
2026-01-18 20:21:14 +08:00
);
}
}