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

发布于 1周前 作者 caililin 来自 Flutter

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

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

开始使用

首先激活该包:

flutter pub global activate app_starter

如何使用它?

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

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

例如:

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

或者,使用缩写形式:

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

显示配置文件存储值:

app_starter --config

显示帮助信息:

app_starter --help

参数

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

key abbreviation description example
name n Dart包标识符 example
org o 组织标识符 com.example
template t 模板Git仓库URL <your_template_git_repository_url>
config c 显示配置文件存储值 –config
save s 保存值到配置文件 –save
help h 显示帮助信息 –help

你可以在这里找到一个模板示例:https://github.com/ThomasEcalle/flappy_template

它是如何工作的?

  1. 这个工具使用 flutter create 命令从你电脑上安装的Flutter版本创建一个新的Flutter应用。
  2. 它会获取你的模型仓库并克隆它。
  3. 然后,它将从模型仓库复制并粘贴 libtest 文件夹以及 pubspec.yaml 文件到你的新应用。
  4. 第四步:它将更改这些目录中的所有导入(以及 pubspec.yaml)以使用新的Dart包标识符。
  5. 最后,该工具将删除临时克隆的仓库,然后你就可以开始使用了!

动机

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

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

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

这个包是为了帮助你在这些过程中节省时间。

多种哲学

现在你想要自动化这些过程,你需要选择如何去做。

使用已有的“启动器”

你可以使用已有的启动器,如著名的 very_good_cli

老实说,这是一个很好的启动器!但你不能根据自己的需求进行定制…而开发者往往有不同的需求!

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

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

我以前使用的方法是创建一个模型仓库。这个仓库实现了我需要的所有基本功能。然后,我只需克隆它并更改应用名称。

但是,这从来不是那么简单

通过命令行创建Flutter应用会自动为你处理很多工作。从为Android和iOS添加正确的包到配置文件中的正确名称,以及其他许多事情。

所以,“仅仅改变应用名称”从来不是那么简单。更不用提随着Flutter的演进,需要更新的配置文件数量也可能发生变化!

使用 app_starter

我想创建一种方式来结合这两种方法的最佳部分:

  • 使用 flutter create 命令创建应用可以防止开发者手动处理所有配置文件
  • 从模型仓库克隆架构可以让开发者构建自己的架构和模板

Flutter演进?没问题,你仍然可以轻松地基于你的基础架构创建新的应用 🎉

关于应用口味

口味在应用开发中非常重要。大多数应用至少有两个口味:devprod,有时更多。但是创建口味并不简单(参见官方文档)。

在Android上,这相对简单。

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

我不想强迫 app_starter 的用户使用口味。一方面是因为 app_starter 应该足够通用,以适应每个开发者的需要。另一方面是因为自动化口味的创建并不容易。

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

例如,在这个简单的默认模板中,我使用了很棒的包 flutter_flavorizr

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

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

正如你所看到的,app_starter 的工作方式允许每个开发者轻松创建自己的启动器,而不必受限于其他人的模板。

请随意创建自己的模板并进行尝试!

示例代码

# 在你的项目目录中运行以下命令
app_starter -n my_app -o com.example -t https://github.com/your_repo/template

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

1 回复

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


在Flutter应用中,app_starter 插件可以用于管理应用的启动行为,例如处理深层链接(deep linking)或启动特定页面。以下是一个如何使用 app_starter 插件的基本示例代码。

首先,确保在你的 pubspec.yaml 文件中添加 app_starter 依赖:

dependencies:
  flutter:
    sdk: flutter
  app_starter: ^x.y.z  # 替换为最新版本号

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

接下来,在你的 Flutter 应用中实现以下代码:

主文件 main.dart

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

void main() {
  // 初始化 AppStarter
  AppStarter.initialize().then((_) {
    runApp(MyApp());
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter App Starter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        '/': (context) => HomeScreen(),
        '/second': (context) => SecondScreen(),
      },
      onGenerateRoute: (RouteSettings settings) {
        // 处理深层链接
        if (settings.name == null) return null;
        
        // 使用 AppStarter 获取启动参数
        AppStarter.getInitialLaunchArgs().then((args) {
          print("Initial launch args: $args");
          // 根据参数处理不同逻辑,例如打开特定页面
          if (args != null && args.containsKey('screen')) {
            String screen = args['screen'];
            if (screen == 'second') {
              Navigator.pushNamed(context, '/second');
            }
          }
        });

        // 默认返回主页面
        return MaterialPageRoute(builder: (context) => HomeScreen());
      },
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Screen'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            // 使用 AppStarter 启动第二屏幕
            Map<String, dynamic> args = {'screen': 'second'};
            AppStarter.launch(args: args, route: '/second');
          },
          child: Text('Go to Second Screen'),
        ),
      ),
    );
  }
}

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

AndroidManifest.xml (可选配置)

如果你需要处理从其他应用传递的深层链接,确保在 AndroidManifest.xml 中配置你的 MainActivity 来接收意图(Intent):

<activity
    android:name=".MainActivity"
    android:launchMode="singleTop"
    android:theme="@style/LaunchTheme"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
    android:hardwareAccelerated="true"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <!-- 深层链接配置 -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" android:host="yourapp.com" android:pathPrefix="/somepath" />
    </intent-filter>
</activity>

iOS 配置 (可选)

对于 iOS,你需要在 Info.plist 中配置你的深层链接 URL Scheme。这通常涉及到添加一个新的 URL Types 配置。

请注意,app_starter 插件的具体用法和 API 可能会有所不同,具体取决于插件的版本和你的具体需求。务必查阅最新的插件文档和示例代码以获取最准确的信息。

回到顶部