feat: 实现导航页基础功能

- 添加导航条目和分类数据模型
- 实现导航卡片网格展示
- 支持点击卡片打开链接
- 添加主题样式(渐变色、卡片阴影)
- 区分系统默认和自定义条目
- 添加分类统计信息"
This commit is contained in:
2026-09-06 21:44:33 +08:00
parent a484d42a29
commit 4075519075
6 changed files with 424 additions and 97 deletions

View File

@@ -1,5 +1,10 @@
import 'package:flutter/material.dart';
import 'models/nav_item.dart';
import 'models/category.dart';
import 'data/mock_data.dart';
import 'package:web/web.dart' as web;
import 'data/nav_data.dart';
import 'theme/app_theme.dart';
void main() {
runApp(const MyApp());
}
@@ -7,115 +12,233 @@ void main() {
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: .fromSeed(seedColor: Colors.deepPurple),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
title: 'Navius 导航',
theme: AppTheme.lightTheme,
home: NaviusHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
class NaviusHomePage extends StatelessWidget {
const NaviusHomePage({super.key});
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
// 获取分类数据
final categories = MockData.getCategories();
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: .center,
children: [
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
appBar: PreferredSize(
preferredSize: const Size.fromHeight(120),
child: Container(
decoration: BoxDecoration(
gradient: AppTheme.appBarGradient,
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(30),
),
],
),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 标题
const Text(
'Navius 导航',
style: TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Colors.white,
letterSpacing: 1.5,
),
),
const SizedBox(height: 4),
Text(
'${categories.length} 个分类 · ${_getTotalEntries(categories)} 个导航',
style: const TextStyle(
fontSize: 14,
color: Colors.white70,
),
),
],
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
body: ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: categories.length,
itemBuilder: (context, index) {
final category = categories[index];
return CategorySection(category: category);
},
),
);
}
// 计算总导航数
static int _getTotalEntries(List<Category> categories) {
int total = 0;
for (var category in categories) {
total += category.entries.length;
}
return total;
}
}
// 分类区块组件
class CategorySection extends StatelessWidget {
final Category category;
const CategorySection({super.key, required this.category});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 分类标题(美化版)
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: Row(
children: [
// 分类名称
Text(
category.name,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w700,
color: Color(0xFF1E293B),
letterSpacing: 0.5,
),
),
const SizedBox(width: 12),
// 条目数量
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
decoration: BoxDecoration(
color: AppTheme.systemTagColor,
borderRadius: BorderRadius.circular(12),
),
child: Text(
'${category.entries.length}',
style: const TextStyle(
fontSize: 12,
color: AppTheme.systemTagTextColor,
fontWeight: FontWeight.w600,
),
),
),
const Spacer(),
// 系统标签
if (category.isSystem)
Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
decoration: BoxDecoration(
color: AppTheme.systemTagColor,
borderRadius: BorderRadius.circular(12),
),
child: const Text(
'系统分类',
style: TextStyle(
fontSize: 11,
color: AppTheme.systemTagTextColor,
fontWeight: FontWeight.w500,
),
),
),
],
),
),
// 导航网格4列
GridView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 1.0,
crossAxisSpacing: 16,
mainAxisSpacing: 16,
),
itemCount: category.entries.length,
itemBuilder: (context, index) {
final item = category.entries[index];
return NavItemCard(item: item);
},
),
const SizedBox(height: 8),
],
);
}
}
// 单个导航卡片组件
class NavItemCard extends StatelessWidget {
final NavItem item;
const NavItemCard({super.key, required this.item});
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
boxShadow: AppTheme.cardShadow,
),
child: InkWell(
onTap: () {
web.window.open(item.url, '_blank');
},
borderRadius: BorderRadius.circular(16),
hoverColor: AppTheme.primaryColor.withOpacity(0.05),
splashColor: AppTheme.primaryColor.withOpacity(0.1),
child: Padding(
padding: const EdgeInsets.all(8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 图标(增大)
Text(
item.icon,
style: const TextStyle(fontSize: 36),
),
const SizedBox(height: 10),
// 标题
Text(
item.title,
style: const TextStyle(
fontSize: 14,
fontWeight: FontWeight.w500,
color: Color(0xFF1E293B),
),
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
const SizedBox(height: 4),
// 系统标签(改进样式)
if (item.isSystem)
Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
decoration: BoxDecoration(
color: AppTheme.systemTagColor,
borderRadius: BorderRadius.circular(10),
),
child: Text(
'默认',
style: TextStyle(
fontSize: 9,
color: AppTheme.systemTagTextColor,
fontWeight: FontWeight.w600,
),
),
),
],
),
),
),
);
}