跳到正文
教程首页
FlutterCH 16ch16_bookmarks

ch16-bookmarks

综合项目(上):架构设计、数据层、依赖注入。完整工程共 116 个文件,其中 76 个文本源文件可在下面在线阅读。

76

在线文本文件

5,837

源码行数

dart / c / yaml

语言

301 KB

打包体积

在线阅读源码

左侧按目录浏览,点击文件切换。右上角可复制代码、复制路径或单独下载文件。在线视图只覆盖文本源码,完整工程请下载 zip。

textdartxmljsonkotlin

76 text files · 5,837 lines

dartlib/src/presentation/router.dart
// 路由:go_router。声明式配置 + 路径参数,比逐层 Navigator.push 更容易看全貌。
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

import 'pages/detail_page.dart';
import 'pages/editor_page.dart';
import 'pages/home_page.dart';

final routerProvider = Provider<GoRouter>((Ref ref) {
  final GoRouter router = GoRouter(
    initialLocation: '/',
    routes: <RouteBase>[
      GoRoute(
        path: '/',
        builder: (BuildContext context, GoRouterState state) => const HomePage(),
        routes: <RouteBase>[
          GoRoute(
            path: 'new',
            builder: (BuildContext context, GoRouterState state) => const EditorPage(),
          ),
          GoRoute(
            path: 'bookmark/:id',
            builder: (BuildContext context, GoRouterState state) => DetailPage(
              id: int.tryParse(state.pathParameters['id'] ?? '') ?? -1,
            ),
          ),
        ],
      ),
    ],
    errorBuilder: (BuildContext context, GoRouterState state) => const NotFoundPage(),
  );
  ref.onDispose(router.dispose);
  return router;
});

class NotFoundPage extends StatelessWidget {
  const NotFoundPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('页面不存在')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text('没有这个路径', key: Key('not-found')),
            const SizedBox(height: 12),
            FilledButton(onPressed: () => context.go('/'), child: const Text('回首页')),
          ],
        ),
      ),
    );
  }
}

代码构成:text 77 KB · dart 69 KB · xml 41 KB · json 4 KB

本地运行

zip → 解压 → 执行
$cd examples/ch16-bookmarks && flutter run -d macos

这个 zip 里有什么

  • • 完整工程目录(含 Android / iOS / 桌面平台脚手架)
  • • lib / bin / test / native 全部源码与测试
  • • pubspec.yaml、锁文件与平台配置文件
  • • 已排除 .dart_tool / build / target / ephemeral 等构建产物

已验证

  • flutter analyze 0 告警、flutter test 20 个用例全绿、debug 与 **release** 构建均真实启动成功
查看全部示例