如何使用Flutter实现参考效果图中的UI界面?

Flutter UI实现方案
实现思路
- 整体用
Scaffold作为基础容器,底部导航栏用BottomNavigationBar组件实现4个tab的切换效果,单独配置选中态、未选中态的颜色和图标即可匹配设计 - 顶部区域用
Stack实现叠加布局:下层是渐变背景横幅,上层是悬浮的用户信息卡片,用Positioned控制卡片的悬浮位置 - 中间功能按钮区用
GridView实现2行4列的功能入口,每个入口用Column组合图标+文字即可 - 下方内容区用
TabBar+TabBarView实现内容分类切换,支持左右滑动切换不同类别的内容列表 - 所有圆角效果用
Container的decoration属性的borderRadius实现,阴影效果通过boxShadow配置即可
核心实现代码
import 'package:flutter/material.dart'; class DemoPage extends StatefulWidget { const DemoPage({super.key}); @override State<DemoPage> createState() => _DemoPageState(); } class _DemoPageState extends State<DemoPage> with TickerProviderStateMixin { int _currentNavIndex = 0; late TabController _tabController; final List<Map> _functionBtns = [ {"icon": Icons.bookmark, "text": "收藏"}, {"icon": Icons.history, "text": "浏览历史"}, {"icon": Icons.offline_share, "text": "离线下载"}, {"icon": Icons.payment, "text": "我的钱包"}, {"icon": Icons.group, "text": "我的好友"}, {"icon": Icons.location_on, "text": "位置"}, {"icon": Icons.settings, "text": "设置"}, {"icon": Icons.help, "text": "帮助与反馈"}, ]; @override void initState() { super.initState(); _tabController = TabController(length: 3, vsync: this); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.grey[100], body: SingleChildScrollView( child: Column( children: [ // 顶部背景+用户信息区 Stack( clipBehavior: Clip.none, children: [ // 渐变背景 Container( height: 180, decoration: const BoxDecoration( gradient: LinearGradient( colors: [Colors.blueAccent, Colors.lightBlueAccent], begin: Alignment.topLeft, end: Alignment.bottomRight, ), ), padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 40), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.start, children: const [ Icon(Icons.qr_code_scanner, color: Colors.white, size: 24), Text("我的", style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold)), Icon(Icons.settings, color: Colors.white, size: 24), ], ), ), // 用户信息卡片 Positioned( top: 120, left: 16, right: 16, child: Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [ BoxShadow(color: Colors.black.withOpacity(0.1), blurRadius: 8, offset: const Offset(0, 2)) ] ), child: Row( children: [ const ClipOval( child: Image( image: NetworkImage("https://via.placeholder.com/80"), width: 80, height: 80, fit: BoxFit.cover, ), ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text("用户名", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), const SizedBox(height: 4), const Text("简介:这是用户的个人简介", style: TextStyle(color: Colors.grey, fontSize: 13)), const SizedBox(height: 8), Row( children: const [ Text("关注 128", style: TextStyle(fontSize: 13)), SizedBox(width: 24), Text("粉丝 256", style: TextStyle(fontSize: 13)), SizedBox(width: 24), Text("获赞 1.2k", style: TextStyle(fontSize: 13)), ], ) ], ), ), ElevatedButton( onPressed: () {}, style: ElevatedButton.styleFrom(backgroundColor: Colors.blue), child: const Text("编辑资料", style: TextStyle(color: Colors.white)), ) ], ), ), ) ], ), const SizedBox(height: 80), // 功能按钮区 Container( margin: const EdgeInsets.symmetric(horizontal: 16), padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), ), child: GridView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 4, childAspectRatio: 1, mainAxisSpacing: 16, ), itemCount: _functionBtns.length, itemBuilder: (context, index) { return Column( children: [ Container( width: 48, height: 48, decoration: BoxDecoration( color: Colors.blue.withOpacity(0.1), borderRadius: BorderRadius.circular(12), ), child: Icon(_functionBtns[index]['icon'], color: Colors.blue, size: 24), ), const SizedBox(height: 8), Text(_functionBtns[index]['text'], style: const TextStyle(fontSize: 13)), ], ); }, ), ), const SizedBox(height: 16), // 内容切换区 Container( decoration: const BoxDecoration(color: Colors.white), child: TabBar( controller: _tabController, labelColor: Colors.blue, unselectedLabelColor: Colors.grey, indicatorColor: Colors.blue, tabs: const [ Tab(text: "作品"), Tab(text: "收藏"), Tab(text: "喜欢"), ], ), ), SizedBox( height: 300, child: TabBarView( controller: _tabController, children: [ // 作品列表 GridView.builder( padding: const EdgeInsets.all(8), gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 3, childAspectRatio: 0.8, crossAxisSpacing: 8, mainAxisSpacing: 8, ), itemCount: 9, itemBuilder: (context, index) { return ClipRRect( borderRadius: BorderRadius.circular(8), child: const Image( image: NetworkImage("https://via.placeholder.com/200x300"), fit: BoxFit.cover, ), ); }, ), const Center(child: Text("暂无收藏内容")), const Center(child: Text("暂无喜欢内容")), ], ), ) ], ), ), // 底部导航 bottomNavigationBar: BottomNavigationBar( currentIndex: _currentNavIndex, onTap: (index) { setState(() { _currentNavIndex = index; }); }, selectedItemColor: Colors.blue, unselectedItemColor: Colors.grey, type: BottomNavigationBarType.fixed, items: const [ BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"), BottomNavigationBarItem(icon: Icon(Icons.explore), label: "发现"), BottomNavigationBarItem(icon: Icon(Icons.add_box), label: "发布"), BottomNavigationBarItem(icon: Icon(Icons.person), label: "我的"), ], ), ); } }
可根据实际效果图的细节调整颜色、图标、间距、圆角等参数即可完美匹配设计稿。
内容的提问来源于stack exchange,提问作者Manh
相关产品推荐
相关产品推荐

