284 lines
12 KiB
Dart
284 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:get_it/get_it.dart';
|
||
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';
|
||
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';
|
||
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';
|
||
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';
|
||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||
import 'package:maibu_satabot_v2/core/theme/AppTheme.dart';
|
||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||
|
||
class MainWrapper extends StatefulWidget {
|
||
const MainWrapper({super.key});
|
||
|
||
@override
|
||
State<MainWrapper> createState() => _MainWrapperState();
|
||
}
|
||
|
||
class _MainWrapperState extends State<MainWrapper> {
|
||
int _currentIndex = 0;
|
||
|
||
/// 上一次渲染的 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>();
|
||
}
|
||
|
||
void _onTabChanged(int index) {
|
||
setState(() {
|
||
_currentIndex = index;
|
||
});
|
||
}
|
||
|
||
IconData _getIconData(String iconName) {
|
||
switch (iconName) {
|
||
case 'home_rounded':
|
||
return Icons.home_rounded;
|
||
case 'grid_view_rounded':
|
||
return Icons.grid_view_rounded;
|
||
case 'devices_rounded':
|
||
return Icons.devices_rounded;
|
||
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;
|
||
case 'person_rounded':
|
||
return Icons.person_rounded;
|
||
default:
|
||
return Icons.home_rounded;
|
||
}
|
||
}
|
||
|
||
Widget _buildCurrentPage(TabConfigItem tab) {
|
||
switch (tab.id) {
|
||
case 'home_v2':
|
||
return BlocProvider(
|
||
create: (_) => sl<HomeV2Bloc>(),
|
||
child: const HomeV2Page(),
|
||
);
|
||
case 'home':
|
||
return const HomePage();
|
||
case 'device':
|
||
return const DeviceStatusPage();
|
||
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();
|
||
case 'my':
|
||
return const MyPage();
|
||
case 'me':
|
||
return const my_v2.MyPage();
|
||
default:
|
||
return const HomePage();
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final roleKey = context.watch<AppUserCubit>().state.user?.roleKey;
|
||
return BlocBuilder<TabConfigCubit, TabConfigState>(
|
||
builder: (context, state) {
|
||
if (state is! TabConfigLoaded) {
|
||
return const Scaffold(
|
||
body: Center(child: CircularProgressIndicator()),
|
||
);
|
||
}
|
||
|
||
final config = state.config;
|
||
final enabledItems = config.navItemsForRole(roleKey);
|
||
|
||
if (enabledItems.isEmpty) {
|
||
return const Scaffold(body: Center(child: Text('请至少启用一个 Tab')));
|
||
}
|
||
|
||
// Tab 集合变化(角色过滤 / 切换账号 / 增删 Tab / 热重载)时子页数量随之改变。
|
||
// 用 IndexedStack 承载:不依赖 ScrollController / 滚动偏移,
|
||
// 数量变化时只会按 index 取页,绝不会出现"控制器越界 → body 白屏"。
|
||
final itemCount = enabledItems.length;
|
||
if (_lastItemCount != itemCount) {
|
||
_lastItemCount = itemCount;
|
||
_currentIndex = 0;
|
||
}
|
||
if (_currentIndex >= itemCount) {
|
||
_currentIndex = 0;
|
||
}
|
||
|
||
return Scaffold(
|
||
extendBody: false,
|
||
// 底部导航预留区(SafeArea 那条)与页面同色,消除突兀的底色带
|
||
backgroundColor: context.appColors.pageBackground,
|
||
body: Stack(
|
||
children: [
|
||
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(),
|
||
);
|
||
},
|
||
),
|
||
],
|
||
),
|
||
bottomNavigationBar: SafeArea(
|
||
top: false,
|
||
child: Padding(
|
||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
|
||
child: LayoutBuilder(
|
||
builder: (context, constraints) {
|
||
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,
|
||
),
|
||
decoration: BoxDecoration(
|
||
color: context.appColors.cardBackground,
|
||
borderRadius: BorderRadius.circular(25),
|
||
border: Border.all(
|
||
color: context.appColors.divider,
|
||
width: 1,
|
||
),
|
||
boxShadow: [
|
||
BoxShadow(
|
||
color: context.appColors.cardShadow,
|
||
blurRadius: 10,
|
||
offset: const Offset(0, 2),
|
||
),
|
||
],
|
||
),
|
||
child: Stack(
|
||
children: [
|
||
// 滑动圆形指示器(纯数学定位,杜绝错位/抖动)
|
||
AnimatedPositioned(
|
||
duration: const Duration(milliseconds: 280),
|
||
curve: Curves.easeOutCubic,
|
||
left:
|
||
_currentIndex * tabWidth +
|
||
(tabWidth - _kIndicatorSize) / 2,
|
||
top: 6,
|
||
child: Container(
|
||
width: _kIndicatorSize,
|
||
height: _kIndicatorSize,
|
||
decoration: BoxDecoration(
|
||
color: context.appColors.primary.withOpacity(
|
||
0.12,
|
||
),
|
||
shape: BoxShape.circle,
|
||
),
|
||
),
|
||
),
|
||
// Tab 项(用 Expanded 弹性平分,避免窄屏下固定宽度溢出)
|
||
Row(
|
||
children: enabledItems.asMap().entries.map((entry) {
|
||
final index = entry.key;
|
||
final item = entry.value;
|
||
final isSelected = _currentIndex == index;
|
||
|
||
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
|
||
? context.appColors.primary
|
||
: context.appColors.textTertiary,
|
||
),
|
||
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,
|
||
),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}).toList(),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
},
|
||
),
|
||
),
|
||
),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
}
|