Flutter应用启动管理插件app_starter_plus的使用

Flutter应用启动管理插件app_starter_plus的使用

app_starter_plus 是一个帮助你从特定模板启动Flutter应用程序的包。

开始使用

首先激活该插件:

flutter pub global activate app_starter_plus

如何使用它?

在你想创建应用程序的位置运行以下命令:

app_starter_plus --name <package_identifier> --org <organisation> --template <template_git_repository>

例如:

app_starter_plus --name toto --org io.example --template https://github.com/ThomasEcalle/flappy_template

或者使用缩写形式:

app_starter_plus -n toto -o io.example -t https://github.com/ThomasEcalle/flappy_template

显示配置文件存储的值:

app_starter_plus --config

显示帮助信息:

app_starter_plus --help

参数

以下是你可以使用的参数列表:

key abbreviation description example
name n Dart包标识符 example
org o 组织标识符 com.example
template t 模板Git仓库地址 https://github.com/ThomasEcalle/flappy_template
config c 显示配置文件存储的值 –config
save s 保存值到配置文件 –save
help h 显示帮助信息 –help

它是如何工作的?

  1. 此工具使用基本的 flutter create 命令从你计算机上安装的Flutter版本创建一个新的Flutter应用程序。
  2. 它会获取你的模型仓库并克隆它。
  3. 然后,它会将 libtest 文件夹以及 pubspec.yaml 文件从你的模型仓库复制到新应用中。
  4. 第四步:它会更改这些目录(以及 pubspec.yaml)中的所有导入,以确保新的Dart包标识符正确。
  5. 最后,该工具会删除临时克隆的仓库,并且…

你就可以开始使用了!

动机

作为一个Flutter开发者,你可能经常需要创建新的应用程序。每次创建应用程序时,你通常需要做同样的事情:

  • 使用正确的名称和组织创建应用程序
  • 设置你习惯的架构
  • 添加你习惯使用的依赖
  • (根据需求)创建多个口味
  • 等等

作为一名自由职业者,有多个客户或作为公司中的技术负责人,这种情况在你的生活中可能会频繁发生。

此包旨在帮助你在这些过程中节省一些时间。

多种哲学

既然你想自动化这些过程,你必须选择如何去做。

使用已存在的“启动器”

你可以使用已存在的启动器,比如著名的来自Very Good Ventures的very_good_cli

老实说:这是一个很好的启动器!

但是你不能根据自己的需求进行定制……而开发者往往有不同的需求!

不同的架构哲学,不同的依赖等等。

克隆一个仓库并“更改名称”

我之前使用的方法是创建一个模型仓库。这个仓库实现了所有我需要的基本功能。

然后,我只是需要克隆它并更改应用程序的名称。

但……这从来都不是那么简单

通过命令行创建Flutter应用程序会自动为你处理很多工作。

从为Android和iOS添加正确的包到配置文件中的正确名称,以及其他许多事情。

因此,“仅仅更改应用程序的名称”从来不是那么简单。

更不用提随着Flutter的发展,需要更新的配置文件数量可能会变化!

使用app_starter 🚀

我希望创造一种方式来结合两个世界的最佳部分:

  • 使用Flutter的命令行 flutter create 创建应用程序可以防止开发人员直接修改所有配置文件。
  • 克隆模型仓库的架构可以让开发人员构建自己的架构和模板。

Flutter发生变化?

没问题,你仍然能够轻松地基于你的基础架构创建新的应用程序🎉。

关于应用口味

口味在应用开发中非常重要。大多数情况下,应用程序至少有一个 dev 和一个 prod 口味,有时更多。

但是创建口味并不是很容易(参见官方文档)。

在Android上,这相对简单。

在iOS上……嗯……虽然不难,但需要花一点时间确保一切正常。

我不想强制app_starter用户使用口味。

首先,因为app_starter应该非常通用,以适应每个开发者的需要。

其次,自动化口味创建并不容易。

但是,app_starter仍然可以帮助你在模板中轻松处理口味🔥。

例如,在默认模板中,我使用了出色的包 flutter_flavorizr

借助这个包,我只需要在 pubspec.yaml 中指定我想创建的口味。

然后,在app_starter运行之后,我可以使用 flutter_flavorizr 命令行生成这些口味🎉。

正如你所见,app_starter的工作方式使每位开发者都能够轻松地创建自己的启动器,而不必受限于别人的“模板”。

尽情创建自己的模板并玩转它吧!


示例Demo

# 创建一个新的Flutter项目
app_starter_plus -n my_app -o com.my_company -t https://github.com/my_template_repo

上面的命令会创建一个新的Flutter项目 my_app,并使用 https://github.com/my_template_repo 作为模板。

配置文件示例

# pubspec.yaml
name: my_app
description: A new Flutter project.
version: 1.0.0+1
environment:
  sdk: ">=2.12.0 <3.0.0"
dependencies:
  flutter:
    sdk: flutter
  flutter_flavorizr: ^1.0.0

创建多个口味

pubspec.yaml 中指定口味:

flutter:
  flavors:
    - name: dev
    - name: prod

然后使用 flutter_flavorizr 生成口味:

flutter_flavorizr

更多关于Flutter应用启动管理插件app_starter_plus的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用启动管理插件app_starter_plus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


app_starter_plus 是一个 Flutter 插件,用于管理和控制 Flutter 应用程序的启动过程。它可以帮助你在应用程序启动时执行一些特定的操作,例如初始化某些服务、检查更新、显示启动画面等。以下是如何使用 app_starter_plus 插件的详细步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 app_starter_plus 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  app_starter_plus: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 初始化插件

在你的 main.dart 文件中,使用 AppStarterPlus 来初始化插件并控制应用程序的启动流程。

import 'package:flutter/material.dart';
import 'package:app_starter_plus/app_starter_plus.dart';

void main() async {
  // 确保 Flutter 引擎初始化完成
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化 AppStarterPlus
  await AppStarterPlus.initialize();

  // 启动应用程序
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter App Starter Plus Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      body: Center(
        child: Text('Welcome to the app!'),
      ),
    );
  }
}

3. 使用 AppStarterPlus 控制启动流程

app_starter_plus 提供了一些方法来控制应用程序的启动流程。例如,你可以在启动时执行一些异步操作,并在操作完成后继续启动应用程序。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化 AppStarterPlus
  await AppStarterPlus.initialize();

  // 模拟一些启动时的异步操作
  await Future.delayed(Duration(seconds: 2));

  // 启动应用程序
  runApp(MyApp());
}

4. 使用 AppStarterPlus 的监听器

app_starter_plus 还提供了一些监听器,允许你在应用程序启动的不同阶段执行特定的操作。例如,你可以在应用程序启动前或启动后执行一些操作。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化 AppStarterPlus
  await AppStarterPlus.initialize();

  // 添加启动前监听器
  AppStarterPlus.onBeforeStart(() async {
    print('Before application starts');
    // 在这里执行一些初始化操作
  });

  // 添加启动后监听器
  AppStarterPlus.onAfterStart(() async {
    print('After application starts');
    // 在这里执行一些启动后的操作
  });

  // 启动应用程序
  runApp(MyApp());
}

5. 处理启动错误

你还可以使用 AppStarterPlus 来处理启动过程中可能出现的错误。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  try {
    // 初始化 AppStarterPlus
    await AppStarterPlus.initialize();

    // 模拟一些启动时的异步操作
    await Future.delayed(Duration(seconds: 2));

    // 启动应用程序
    runApp(MyApp());
  } catch (e) {
    print('Error during startup: $e');
    // 处理启动错误
  }
}

6. 自定义启动画面

app_starter_plus 允许你自定义应用程序的启动画面。你可以通过设置 AppStarterPlus 的配置来实现这一点。

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化 AppStarterPlus 并配置启动画面
  await AppStarterPlus.initialize(
    splashScreen: SplashScreen(
      image: AssetImage('assets/splash.png'),
      duration: Duration(seconds: 3),
    ),
  );

  // 启动应用程序
  runApp(MyApp());
}
回到顶部