Flutter:零基础可操作的 mason 脚手架完整流程


1. 安装 mason CLI(如已安装跳过)

csharp 复制代码
dart pub global activate mason_cli

2. 初始化 mason(如已有 .mason/ 可跳过)

csharp 复制代码
mason init

3. 新建 brick

arduino 复制代码
mason new fast_page

会生成 .mason/bricks/fast_page/ 目录。


4. 在 .mason/bricks/fast_page/__brick__/ 下新建目录和文件

进入 .mason/bricks/fast_page/__brick__/,新建如下目录结构(直接写模板变量名):

scss 复制代码
view/
  {{name.snakeCase()}}_page.dart
provider/
  {{name.snakeCase()}}_provider.dart
route.txt
route_const.txt

5. 填写模板内容

view/{{name.snakeCase()}}_page.dart

typescript 复制代码
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../provider/{{name.snakeCase()}}_provider.dart';

class {{name.pascalCase()}}Page extends ConsumerWidget {
  const {{name.pascalCase()}}Page({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      appBar: AppBar(title: const Text('{{name.pascalCase()}}')),
      body: const Center(child: Text('This is {{name.pascalCase()}}Page')),
    );
  }
}

provider/{{name.snakeCase()}}_provider.dart

dart 复制代码
import 'package:flutter_riverpod/flutter_riverpod.dart';

final {{name.camelCase()}}Provider = StateProvider<int>((ref) => 0);

route.txt

scss 复制代码
GoRoute(
  path: RouterConst.{{name.camelCase()}}Page,
  name: '{{name.pascalCase()}}',
  builder: (context, state) => const {{name.pascalCase()}}Page(),
),

route_const.txt

arduino 复制代码
static const String {{name.camelCase()}}Page = '/{{name.paramCase()}}';

6. 配置 brick.yaml(在 .mason/bricks/fast_page/brick.yaml

yaml 复制代码
name: fast_page
description: Flutter page with provider and route

version: 0.1.0+1

environment:
  mason: ^0.1.1

vars:
  name:
    type: string
    description: The page name
    prompt: What is the name of the page?

7. 配置项目根目录 mason.yaml

在你项目根目录的 mason.yaml 里加:

yaml 复制代码
bricks:
  fast_page:
    path: .mason/bricks/fast_page

8. 拉取模板

arduino 复制代码
mason get

9. 使用 mason 生成页面

在你想生成的目录(建议项目根目录)下执行:

css 复制代码
mason make fast_page --name login

10. 复制文件到目标位置

  • view/login_page.dartprovider/login_provider.dart 移动到你实际 feature 模块目录(如 lib/features/login/view/lib/features/login/provider/
  • route.txt 内容复制到 lib/route/app_router.dart(你的 go_router 配置)
  • route_const.txt 内容复制到 lib/route/router_const.dart(你的 RouterConst 类)

结论

  1. 一次性生成页面/provider/路由片段/路由常量
  2. 复制粘贴,极大提升你的 Flutter 工程效率和规范

相关推荐
VT.馒头4 小时前
【力扣】2695. 包装数组
前端·javascript·算法·leetcode·职场和发展·typescript
css趣多多4 小时前
一个UI内置组件el-scrollbar
前端·javascript·vue.js
C澒5 小时前
前端整洁架构(Clean Architecture)实战解析:从理论到 Todo 项目落地
前端·架构·系统架构·前端框架
C澒5 小时前
Remesh 框架详解:基于 CQRS 的前端领域驱动设计方案
前端·架构·前端框架·状态模式
Charlie_lll5 小时前
学习Three.js–雪花
前端·three.js
onebyte8bits5 小时前
前端国际化(i18n)体系设计与工程化落地
前端·国际化·i18n·工程化
C澒5 小时前
前端分层架构实战:DDD 与 Clean Architecture 在大型业务系统中的落地路径与项目实践
前端·架构·系统架构·前端框架
BestSongC5 小时前
行人摔倒检测系统 - 前端文档(1)
前端·人工智能·目标检测
0思必得06 小时前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
Misnice6 小时前
Webpack、Vite、Rsbuild区别
前端·webpack·node.js