Flutter功能扩展插件arch_empower的使用

Flutter功能扩展插件arch_empower的使用

有没有觉得绘制架构图很麻烦?有没有想过用代码生成架构图?有没有想过架构图可以自适应不同的屏幕宽度?

arch_empower 是一个基于 Flutter 的架构图绘制工具,让开发者能够以极高的效率生成美观、响应式的架构图。

特点

  • 声明式语法:使用简洁的 Dart 代码,以声明式的方式定义架构图的结构和样式。
  • 组件化设计:提供了 GraphGroupBoxRC 等基本组件,可以灵活组合,快速构建复杂的架构图。
  • 响应式布局:利用 Flutter 强大的布局能力,生成的架构图能够自适应不同的宽度,具有响应式特性。
  • 可扩展性:生成的架构图本身就是一个 Flutter 应用,可以对其进行进一步的扩展和定制。
  • Web 支持:可以将生成的架构图构建成 Web 应用,形成可交互的架构图,增强演示和展示效果。

安装

将以下内容添加到你的 pubspec.yaml 文件中:

dependencies:
  arch_empower: ^1.0.0

然后运行 flutter pub get 命令安装依赖。

使用方法

  1. 导入 arch_empower 库:

    import 'package:arch_empower/arch_empower.dart';
    
  2. 使用提供的组件构建架构图:

    Graph(
      title: 'arch_empower - 架构赋能',
      child: C(
        children: [
          Box(title: '顶层设计'),
          R(children: [
            Group(
              title: "边界赛道",
              child: R(children: [
                C(children: [
                  Box(title: '用户痛点'),
                  Box(title: '用户痒点'),
                  Box(title: '用户爽点'),
                ]),
                Box(title: '✌️葵花点穴手✌️'),
              ]),
            ),
            // 其他组件...
          ]),
          Box(title: '底层逻辑')
        ],
      ),
    )
    
  3. 运行 Flutter 应用,查看生成的架构图。

组件说明

  • Graph:架构图的根组件,表示整个架构图。
  • Group:表示一个分组,可以包含其他组件。
  • Box:表示一个矩形框,用于显示具体的架构元素。
  • R:表示一个横向的 Row 布局。
  • C:表示一个纵向的 Column 布局。

通过组合这些基本组件,可以快速构建出复杂的架构图。

示例

更多示例,请参考 example 目录。

许可证

arch_empower 采用 MIT 许可证进行发布。详细信息请参阅 LICENSE 文件。

关于作者

我是 Maeiee(Maxiee),欢迎访问我的 个人网站

如果你觉得 arch_empower 对你有帮助,欢迎在爱发电上支持我的工作:

联系方式

如果你在使用中遇到任何问题或有任何建议,欢迎随时与我们联系。你可以通过以下方式联系我们:

我们会尽快回复并解决你的问题。

希望 arch_empower 能够为你的架构设计工作带来便利和效率提升!


示例代码

main.dart

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

void main() {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyArch(),
    );
  }
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const Scaffold(
      backgroundColor: Colors.white,
      body: Padding(
          padding: const EdgeInsets.all(8),
          child: SingleChildScrollView(
              child: Graph(
            title: 'arch_empower - 架构赋能',
            child: C(
              children: [
                Box(title: '顶层设计'),
                R(children: [
                  Group(
                      title: "边界赛道",
                      child: R(children: [
                        C(children: [
                          Box(title: '用户痛点'),
                          Box(title: '用户痒点'),
                          Box(title: '用户爽点'),
                        ]),
                        Box(title: '✌️葵花点穴手✌️'),
                      ])),
                  C(children: [
                    Box(title: '作为架构师,没有架构图,再好的戏都出不来'),
                    Group(
                        title: "核心打法",
                        child: C(children: [
                          Box(title: '🥊组🥊合🥊拳🥊'),
                          Box(title: '🚀增长引擎🚀'),
                        ])),
                  ]),
                  Group(
                    title: '借东风',
                    child: C(children: [
                      Box(title: '人优我变'),
                      Box(title: '绝境逢生'),
                      Box(title: '🌪️风🌪️口🌪️'),
                    ]),
                  )
                ]),
                
                Box(title: '底层逻辑')
              ],
            ),
          ))),
    );
  }
}

更多关于Flutter功能扩展插件arch_empower的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter功能扩展插件arch_empower的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是一个关于如何在Flutter项目中使用arch_empower插件的代码案例。arch_empower是一个用于增强Flutter应用架构能力的插件,它通常提供了诸如依赖注入、状态管理等功能。不过,由于arch_empower不是一个广泛知名的开源库,并且具体的API和实现细节可能会有所不同,下面的代码案例将基于一个假设的插件功能和用法来展示。

首先,确保你已经在pubspec.yaml文件中添加了arch_empower依赖:

dependencies:
  flutter:
    sdk: flutter
  arch_empower: ^latest_version  # 替换为实际的最新版本号

然后,运行flutter pub get来安装依赖。

接下来,我们来看一个如何在Flutter应用中使用arch_empower进行依赖注入和状态管理的示例。

1. 设置依赖注入容器

首先,我们需要创建一个依赖注入容器,并注册我们需要的依赖项。

import 'package:arch_empower/arch_empower.dart';

// 假设我们有一个简单的服务类
class MyService {
  void doSomething() {
    print("Doing something in MyService");
  }
}

void main() {
  // 创建依赖注入容器
  final container = Container();

  // 注册MyService为一个单例
  container.registerSingleton<MyService>(MyService());

  // 使用容器创建应用
  runApp(container.provideDependencies(MyApp()));
}

2. 在组件中使用依赖注入

接下来,我们在Flutter组件中使用依赖注入来获取我们需要的服务。

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  // 使用@Inject注解来获取MyService的实例
  final MyService myService = Inject<MyService>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Arch Empower Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            // 调用MyService的方法
            myService.doSomething();
          },
          child: Text('Call MyService'),
        ),
      ),
    );
  }
}

3. 状态管理示例

假设arch_empower还提供了状态管理功能,我们可以这样使用它:

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

// 创建一个简单的状态类
class CounterState {
  int value = 0;

  void increment() {
    value++;
  }
}

void main() {
  final container = Container();

  // 注册CounterState为一个单例,并初始化为状态管理对象
  final counterState = CounterState();
  container.registerSingleton<CounterState>(counterState);

  // 创建一个ViewModel来封装状态逻辑
  class CounterViewModel {
    final CounterState counterState = Inject<CounterState>();

    void increment() {
      counterState.increment();
    }

    int getValue() {
      return counterState.value;
    }
  }

  container.registerFactory<CounterViewModel>(() => CounterViewModel());

  runApp(container.provideDependencies(MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CounterPage(),
    );
  }
}

class CounterPage extends StatefulWidget {
  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  final CounterViewModel counterViewModel = Inject<CounterViewModel>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Counter'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${counterViewModel.getValue()}',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            counterViewModel.increment();
          });
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

请注意,由于arch_empower并非一个实际存在的广泛认知的库(根据我的知识库),上述代码是基于假设的API设计的。如果你正在使用一个具体的arch_empower库,请查阅其官方文档以获取准确的API和用法。如果arch_empower确实存在但有不同的API设计,请根据实际情况调整上述代码。

回到顶部