Flutter单体架构支持插件flutter_mono的使用

Flutter单体架构支持插件flutter_mono的使用

该插件flutter_mono使得在Flutter项目中使用Mono连接组件变得简单。这是一份非官方的SDK。

📸 屏幕截图

屏幕截图1 屏幕截图2 屏幕截图3

🚀 如何使用插件

使用launchMono方法启动MonoFlutter

import 'package:flutter_mono/flutter_mono.dart';

void launch() async {
  FlutterMono(
    apiKey: 'Your Public Key', // 从 https://app.withmono.com/apps 获取
    reference: "some_random_string",
    showLogs: true,
    customer: const MonoCustomer(
      newCustomer: MonoNewCustomerModel(
        name: "Samuel Olamide", // 必填
        email: "samuel@neem.com", // 必填
        identity: MonoNewCustomerIdentity(
          type: "bvn",
          number: "2323233239",
        ),
      ),
      existingCustomer: MonoExistingCustomerModel(
        id: "1234-RTFG-ABCD", // 必填
      ),
    ),
    configJson: const {
      "selectedInstitution": {
        "id": "5f2d08be60b92e2888287702",
        "auth_method": "mobile_banking"
      }
    },
    onClose: (it) => log('Success: $it'), // 关闭回调
    onLoad: () => log('widget_loaded'), // 加载回调
    onEvent: (eventName, eventData) {
      switch (eventName) {
        case 'mono.connect.institution_selected':
          /// 执行某些操作
          break;
      }
    },
  ).launchMono(context); // 在当前上下文中启动Mono
}

使用MonoView组件

import 'package:flutter_mono/flutter_mono.dart';

...

FlutterMono(
  apiKey: 'Your Public Key', // 从 https://app.withmono.com/apps 获取
  reference: "some_random_string",
  showLogs: true,
  customer: const MonoCustomer(
    newCustomer: MonoNewCustomerModel(
      name: "Samuel Olamide", // 必填
      email: "samuel@neem.com", // 必填
      identity: MonoNewCustomerIdentity(
        type: "bvn",
        number: "2323233239",
      ),
    ),
    existingCustomer: MonoExistingCustomerModel(
      id: "1234-RTFG-ABCD", // 必填
    ),
  ),
  configJson: const {
    "selectedInstitution": {
      "id": "5f2d08be60b92e2888287702",
      "auth_method": "mobile_banking"
    }
  },
  onClose: (it) => log('Success: $it'), // 关闭回调
  onLoad: () => log('widget_loaded'), // 加载回调
  onEvent: (eventName, eventData) {
    switch (eventName) {
      case 'mono.connect.institution_selected':
        /// 执行某些操作
        break;
    }
  },
)

...

✨ 贡献

欢迎提交PR来改进此插件。任何建议和贡献都是受欢迎的。


下面是完整的示例代码:

import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mono/flutter_mono.dart';

void main() => runApp(const MyApp());

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

  // 应用程序根组件
  [@override](/user/override)
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarColor: Colors.white,
      ),
      child: MaterialApp(
        title: 'Mono Flutter Demo',
        theme: ThemeData(
          primaryColor: Colors.black,
        ),
        debugShowCheckedModeBanner: false,
        home: const HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  [@override](/user/override)
  HomePageState createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  String _code = 'no_code_yet'; // 用于存储返回的代码
  String get code => _code;
  set code(String value) {
    setState(() => _code = value);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'MonoConnect v2.0 Demo',
          style: TextStyle(
            color: Colors.black,
            fontSize: 18,
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
      ),
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SelectableText(
              code,
              style: const TextStyle(
                fontWeight: FontWeight.w400,
                fontSize: 14,
              ),
            ),
            const SizedBox(height: 60),
            Column(
              children: [
                SizedBox(
                  height: 50,
                  width: 260,
                  child: CupertinoButton(
                    color: Colors.black,
                    child: const Center(
                      child: Text(
                        'Launch Mono',
                        style: TextStyle(
                          color: Colors.white,
                        ),
                      ),
                    ),
                    onPressed: () => launch(context), // 启动Mono
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  void launch(BuildContext context) {
    try {
      FlutterMono(
        apiKey: 'test_pk_lwWSeByMA8yGfckIN87I',
        reference: 'random_string',
        showLogs: true,
        customer: const MonoCustomer(
          newCustomer: MonoNewCustomerModel(
            name: "Samuel Olamide", // 必填
            email: "samuel@neem.com", // 必填
            identity: MonoNewCustomerIdentity(
              type: "bvn",
              number: "2323233239",
            ),
          ),
        ),
        configJson: const {
          "selectedInstitution": {
            "id": "5f2d08be60b92e2888287702",
            "auth_method": "mobile_banking"
          }
        },
        onLoad: () => log('widget_loaded'), // 加载回调
        onEvent: (eventName, data) => log(
          '$eventName: $data',
        ), // 事件回调
        onClose: (it) {
          log('Success: $it'); // 成功关闭回调
          code = it;
        },
      ).launchMono(context); // 在当前上下文中启动Mono
    } catch (e) {
      log(e.toString()); // 捕获并记录错误信息
    }
  }
}

更多关于Flutter单体架构支持插件flutter_mono的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter单体架构支持插件flutter_mono的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_mono 是一个用于 Flutter 单体架构(Mono-repo)的工具,它可以帮助开发者在一个单一的代码库中管理多个 Flutter 项目和共享代码。通过 flutter_mono,你可以更容易地组织和管理多个 Flutter 模块、包或插件,尤其是在复杂的应用场景中。

使用 flutter_mono 的步骤

  1. 安装 flutter_mono

    首先,你需要全局安装 flutter_mono。你可以通过以下命令安装:

    dart pub global activate flutter_mono
    
  2. 初始化 Mono-repo

    在你的项目根目录下,运行以下命令来初始化一个 Mono-repo 结构:

    flutter_mono init
    

    这将会创建一个新的 mono.yaml 配置文件,以及一些默认的目录结构。

  3. 添加子项目

    你可以使用 flutter_mono 来添加新的 Flutter 项目或包。例如,添加一个新的 Flutter 应用或模块:

    flutter_mono create app my_app
    

    这将会在 apps 目录下创建一个名为 my_app 的 Flutter 应用。

  4. 添加共享代码

    你可以将共享的代码(例如通用的 Widgets、模型、工具类等)放在 packages 目录下。你可以使用以下命令创建一个新的包:

    flutter_mono create package my_package
    
  5. 配置依赖关系

    mono.yaml 文件中,你可以配置各个子项目之间的依赖关系。例如,如果你的 my_app 项目依赖于 my_package,你可以这样配置:

    dependencies:
      my_app:
        - my_package
    
  6. 运行子项目

    你可以使用 flutter_mono 来运行特定的子项目。例如,运行 my_app 项目:

    flutter_mono run my_app
    
  7. 构建子项目

    你也可以使用 flutter_mono 来构建特定的子项目。例如,构建 my_app 项目的 release 版本:

    flutter_mono build my_app --release
    

其他功能

  • 依赖管理flutter_mono 可以自动管理子项目之间的依赖关系,确保各个项目能够正确地引用共享代码。

  • 脚本支持:你可以在 mono.yaml 中定义自定义的脚本,以便更方便地执行常见的开发任务。

  • 多平台支持flutter_mono 支持多种平台(如 iOS、Android、Web 等),你可以为不同的子项目配置不同的构建目标。

示例 mono.yaml 文件

以下是一个简单的 mono.yaml 文件示例:

name: my_mono_repo
description: A mono-repo for managing multiple Flutter projects
dependencies:
  my_app:
    - my_package
  my_other_app:
    - my_package
scripts:
  build_all:
    run: flutter_mono build all
回到顶部