搜索插件、代码仓与 APP 分类

flutter_flavor 封面
插件详情v3.1.4

flutter_flavor

我们可以创建不同的版本,从而在发布到 App Store 和 Google Play 之前拥有应用程序的不同实例。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3.4万
喜欢数
479
Pub 分数
160

安装插件

flutter pub add flutter_flavor

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

flutter_flavor 是一个开发者工具,可创建不同的应用风味,从而在发布到应用商店之前,为不同环境(如开发、预发布和生产)提供不同的配置和构建。它简化了管理特定环境的设置(如 API 端点、日志级别和品牌标识)的过程,而无需复制代码。该包与 Flutter 的构建系统无缝集成,支持所有受支持的平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。它帮助团队保持清晰且可配置的工作流程,以进行测试和部署。

适合场景

  • 管理多个环境
  • 自定义生成配置
  • 发布前测试
  • 分离开发/预发布/生产环境设置
  • 优化CI/CD流水线

主要特点

  • 环境特定的配置
  • 跨平台支持
  • 与 Flutter 的轻松集成
  • 构建时风味区分
  • 最小化样板代码

适合对象

  • 开发团队
  • CI/CD 工作流
  • 多环境应用
  • 按版本的应用程序自定义
  • 使用不同部署阶段的团队

使用建议

  • 需要在构建脚本中进行设置
  • 口味名称必须在各平台上保持一致
  • 有限的运行时切换
  • 不用于面向用户的口味选择
  • 需要团队成员之间的协调

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_flavor

pub package Star on Github License: MIT

Flavors helps us to create builds for different instances of our app. For example, we can create a flavor for development, a flavor for production and another flavor for a demo of the app. In this way we can create different flavors, and thus have different instances of our apps before publishing it on the App Store and Google Play.

flutter_favor allows you to quickly configure and define dynamic variables for each flavor in your project. The flavors, as well as their names; they are dynamically defined by the developer or development team. In the configuration of a flavor you can set the name of each flavor, as well as the color and location of its banner. When attribute name is undefined or empty, the banner is hidden.

You can also configure the banners independently. The aforementioned attributes are globally configured in FlavorConfig, but can be changed in the FlavorBanner.

Screenshot

PROD DEV TEST
Screenshot Prod Screenshot Dev Screenshot Test

Note By default the banner is shown in BannerLocation.topStart and is visibility only when the attribute name of configuration is defined and not empty.

Getting Started

Adding package
flutter_flavor: ^3.1.4
Importing package
import 'package:flutter_flavor/flutter_flavor.dart';
Configuring
FlavorConfig(
    name: "DEVELOP",
    color: Colors.red,
    location: BannerLocation.bottomStart,
    variables: {
        "counter": 0,
        "baseUrl": "https://www.example.com",
    }
);
Using variables
 int _counter = FlavorConfig.instance.variables["counter"];

Example

Main
import 'package:flutter/material.dart';
import 'package:flutter_flavor/flutter_flavor.dart';

import 'app.dart';

void main() {
  FlavorConfig(
    name: "DEVELOP",
    color: Colors.red,
    location: BannerLocation.topStart,
    variables: {
      "counter": 5,
      "baseUrl": "https://www.example2.com",
    },
  );

  runApp(const MyApp());
}
App
import 'package:flutter/material.dart';
import 'package:flutter_flavor/flutter_flavor.dart';

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

  @override
  Widget build(BuildContext context) {
    return FlavorBanner(
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

VSCode Configuration

The .vscode folder is created in the workspace, if it does not exist it can be created by hand. Inside that folder a launch.json file is created and the configuration is established inside the file. For more information, visit: https://code.visualstudio.com/docs/editor/debugging and https://go.microsoft.com/fwlink/?linkid=830387

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "PROD-Flavor",
      "program": "[project-folder]/lib/main_prod.dart",
      "request": "launch",
      "type": "dart"
    },
    {
      "name": "DEV-Flavor",
      "program": "[project-folder]/lib/main_dev.dart",
      "request": "launch",
      "type": "dart"
    }
  ]
}

License

MIT License

所属分类

源码仓库

使用此插件的项目