Browse Source

refactor(home): 简化主页布局

weicu
lxm 1 year ago
parent
commit
c6a3acc818
  1. 5
      lib/modules/agent/views/agent_view.dart
  2. 189
      lib/modules/home/views/home_view.dart

5
lib/modules/agent/views/agent_view.dart

@ -405,8 +405,7 @@ class _AgentViewState extends State<AgentView> with WidgetsBindingObserver {
SizedBox(height: 12.h), SizedBox(height: 12.h),
], ],
); );
} else if (controller.isTextInputMode.value && } else {
!controller.hasTextInput.value) {
// 文本输入模式下,且输入框为空 // 文本输入模式下,且输入框为空
return Column( return Column(
children: [ children: [
@ -449,8 +448,6 @@ class _AgentViewState extends State<AgentView> with WidgetsBindingObserver {
SizedBox(height: 12.h), SizedBox(height: 12.h),
], ],
); );
} else {
return const SizedBox.shrink();
} }
}, },
), ),

189
lib/modules/home/views/home_view.dart

@ -72,28 +72,23 @@ class HomeView extends GetView<HomeController> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// 预缓存图片
precacheImage(
const AssetImage('assets/images/headphone_new3.png'), context);
precacheImage(
const AssetImage('assets/images/headphone_new4.png'), context);
final bool isDarkMode = Theme.of(context).brightness == Brightness.dark; final bool isDarkMode = Theme.of(context).brightness == Brightness.dark;
return Scaffold( return Scaffold(
backgroundColor: isDarkMode ? Colors.black : const Color(0xFFF9FAFC), backgroundColor: isDarkMode ? Colors.black : const Color(0xFFF9FAFC),
body: SafeArea( body: SafeArea(
child: Padding( child: SingleChildScrollView(
padding: EdgeInsets.all(16.w), padding: EdgeInsets.all(16.w),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// 顶部标题栏 // 顶部标题栏
_buildAppBar(isDarkMode), _buildAppBar(isDarkMode),
// 可变高度的顶部图片区域 - 使用Expanded让它占用剩余空间
Expanded( // 顶部图片
child: Column( _buildEarbudsImage(),
children: [
Obx(() => _buildAdaptiveEarbudsImage()),
SizedBox(height: 20.h), SizedBox(height: 20.h),
// 设备状态卡片 // 设备状态卡片
IntroStepBuilder( IntroStepBuilder(
group: 'home', group: 'home',
@ -101,7 +96,6 @@ class HomeView extends GetView<HomeController> {
onWidgetLoad: () { onWidgetLoad: () {
controller.noviceGuideService.startHomeGuide(); controller.noviceGuideService.startHomeGuide();
}, },
getOverlayPosition: IntroOverlayBuilder.overlayPosition,
overlayBuilder: (params) { overlayBuilder: (params) {
return IntroOverlayBuilder.aiBuilder( return IntroOverlayBuilder.aiBuilder(
params, params,
@ -111,17 +105,28 @@ class HomeView extends GetView<HomeController> {
}, },
); );
}, },
builder: (BuildContext context, GlobalKey key) => builder: (BuildContext context, GlobalKey key) => SizedBox(
SizedBox(
key: key, key: key,
child: _buildAnimatedDeviceStatusCard(isDarkMode), child: _buildDeviceStatusCard(isDarkMode),
), ),
), ),
SizedBox(height: 20.h), SizedBox(height: 20.h),
IntroStepBuilder( IntroStepBuilder(
group: 'home', group: 'home',
order: 2, order: 2,
getOverlayPosition: IntroOverlayBuilder.overlayPosition, getOverlayPosition: ({
required Offset offset,
required Size screenSize,
required Size size,
}) {
return OverlayPosition(
top: 200.h,
width: screenSize.width,
crossAxisAlignment: CrossAxisAlignment.end,
);
},
overlayBuilder: (params) { overlayBuilder: (params) {
return Obx( return Obx(
() => IntroOverlayBuilder.homeBuilder( () => IntroOverlayBuilder.homeBuilder(
@ -133,27 +138,17 @@ class HomeView extends GetView<HomeController> {
), ),
); );
}, },
builder: (BuildContext context, GlobalKey key) => builder: (BuildContext context, GlobalKey key) => SizedBox(
SizedBox(
key: key, key: key,
), ),
), ),
],
), // 热门AI体验
),
// 热门AI体验 - 固定在底部,基于内容的固定高度
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min, // 重要:让高度基于内容
children: [
_buildSectionTitle(isDarkMode), _buildSectionTitle(isDarkMode),
SizedBox(height: 16.h), SizedBox(height: 16.h),
// 给AI布局一个固定的高度,或者让它基于内容自适应
_buildPremiumAILayout(), _buildPremiumAILayout(),
], ],
), ),
],
),
), ),
), ),
bottomNavigationBar: const CommonBottomNav(currentIndex: 0), bottomNavigationBar: const CommonBottomNav(currentIndex: 0),
@ -201,47 +196,70 @@ class HomeView extends GetView<HomeController> {
); );
} }
// 自适应高度的耳机图片 - 根据设备连接状态动态调整高度 // 耳机图片
// 在自适应图片组件中添加RepaintBoundary Widget _buildEarbudsImage() {
Widget _buildAdaptiveEarbudsImage() {
final bool isDarkMode = Get.isDarkMode; final bool isDarkMode = Get.isDarkMode;
final double imageHeight = return SizedBox(
controller.isFullyConnectedRx.value ? 160.h : 220.h; height: 220.h,
return RepaintBoundary(
// 添加重绘边界,优化动画性能
child: AnimatedContainer(
duration: const Duration(milliseconds: 500),
curve: Curves.easeInOut,
height: imageHeight,
child: PageView( child: PageView(
children: [ children: [
// Center(
// child: ClipRRect(
// // 添加 ClipRRect 来实现圆角
// borderRadius: BorderRadius.circular(16.r), // 使用与其他部件一致的圆角大小
// child: SizedBox(
// width: double.infinity,
// height: 200.h,
// child: Image.asset(
// isDarkMode
// ? 'assets/images/robot_light.png'
// : 'assets/images/robot_light.png',
// fit: BoxFit.fitWidth,
// ),
// ),
// ),
// ),
Center( Center(
child: ClipRRect( child: ClipRRect(
// 添加 ClipRRect
borderRadius: BorderRadius.circular(16.r), borderRadius: BorderRadius.circular(16.r),
child: Image.asset( child: Image.asset(
isDarkMode isDarkMode
? 'assets/images/headphone_new3.png' ? 'assets/images/headphone_new3.png'
: 'assets/images/headphone_new4.png', : 'assets/images/headphone_new4.png',
height: imageHeight, height: 220.h,
fit: BoxFit.contain, fit: BoxFit.contain,
), ),
), ),
), ),
// Center(
// child: ClipRRect(
// // 添加 ClipRRect
// borderRadius: BorderRadius.circular(16.r),
// child: SizedBox(
// width: double.infinity,
// height: 200.h,
// child: Image.asset(
// isDarkMode
// ? 'assets/images/glasses_light.png'
// : 'assets/images/glasses_light.png',
// fit: BoxFit.fitWidth,
// ),
// ),
// ),
// ),
], ],
), ),
),
); );
} }
// 带动画的设备状态卡片 - 实现平滑的高度变化过渡 // 设备状态卡片
Widget _buildAnimatedDeviceStatusCard(bool isDarkMode) { Widget _buildDeviceStatusCard(bool isDarkMode) {
return Obx(() { return Obx(() {
// 使用连接状态作为key,强制在状态改变时重建widget
final isConnected = controller.isFullyConnectedRx.value; final isConnected = controller.isFullyConnectedRx.value;
return Container(
return AnimatedContainer( key: ValueKey('device_status_$isConnected'),
duration: const Duration(milliseconds: 500), // 平滑过渡动画
curve: Curves.easeInOut,
decoration: BoxDecoration( decoration: BoxDecoration(
color: isDarkMode ? const Color(0xFF1A1A1A) : Colors.white, color: isDarkMode ? const Color(0xFF1A1A1A) : Colors.white,
borderRadius: BorderRadius.circular(16.r), borderRadius: BorderRadius.circular(16.r),
@ -255,58 +273,30 @@ class HomeView extends GetView<HomeController> {
), ),
padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 16.h), padding: EdgeInsets.fromLTRB(16.w, 16.h, 16.w, 16.h),
child: Column( child: Column(
mainAxisSize: MainAxisSize.min, // 重要:让容器高度自适应内容
children: [ children: [
// 状态标题行 // 状态标题行
_buildDeviceStatusHeader(isDarkMode), _buildDeviceStatusHeader(isDarkMode),
// 设备信息 - 使用 AnimatedSwitcher 实现内容切换动画 // 设备信息
SizedBox(height: 12.h), SizedBox(height: 12.h),
AnimatedSwitcher( Obx(() {
duration: const Duration(milliseconds: 300), // 移除连接失败和连接中的状态判断,直接根据是否完全连接来判断
transitionBuilder: (Widget child, Animation<double> animation) { // 使用响应式访问确保UI更新
return FadeTransition( return controller.isFullyConnectedRx.value
opacity: animation, ? _buildConnectedDeviceInfo(isDarkMode) //已连接设备信息
child: SlideTransition( : _buildDisconnectedInfo(isDarkMode); //未连接提示
position: Tween<Offset>( }),
begin: const Offset(0, 0.1),
end: Offset.zero, // 连接按钮(仅在未连接时显示)
).animate(animation),
child: child,
),
);
},
child: Container(
key: ValueKey('device_info_$isConnected'),
child: isConnected
? _buildConnectedDeviceInfo(isDarkMode)
: _buildDisconnectedInfo(isDarkMode),
),
),
// 连接按钮 - 使用 AnimatedSwitcher 实现按钮切换动画
SizedBox(height: 12.h), SizedBox(height: 12.h),
AnimatedSwitcher( Obx(() {
duration: const Duration(milliseconds: 300), // 使用响应式访问确保UI更新
transitionBuilder: (Widget child, Animation<double> animation) { if (!controller.isFullyConnectedRx.value) {
return FadeTransition( return _buildConnectButton();
opacity: animation, } else {
child: SlideTransition( return _buildDeviceButton(isDarkMode);
position: Tween<Offset>( }
begin: const Offset(0, 0.1), }),
end: Offset.zero,
).animate(animation),
child: child,
),
);
},
child: Container(
key: ValueKey('device_button_$isConnected'),
child: !isConnected
? _buildConnectButton()
: _buildDeviceButton(isDarkMode),
),
),
], ],
), ),
); );
@ -740,15 +730,14 @@ class HomeView extends GetView<HomeController> {
child: IntroStepBuilder( child: IntroStepBuilder(
group: 'home', group: 'home',
order: 3, order: 3,
getOverlayPosition: IntroOverlayBuilder.overlayPosition, onHighlightWidgetTap: () {
Intro.of(Get.context!).dispose();
controller.exploreAIFeatures();
},
overlayBuilder: (params) { overlayBuilder: (params) {
return IntroOverlayBuilder.aiBuilder( return IntroOverlayBuilder.aiBuilder(
params, params,
'太棒了!你已成功唤醒小言,接下来试试让它帮你做件事吧', '太棒了!你已成功唤醒小言,接下来试试让它帮你做件事吧',
onTap: () {
params.onFinish();
controller.exploreAIFeatures();
},
); );
}, },
builder: (BuildContext context, GlobalKey key) => SizedBox( builder: (BuildContext context, GlobalKey key) => SizedBox(

Loading…
Cancel
Save