Flutter仪表盘插件moon_gauge_bar的使用

Flutter仪表盘插件moon_gauge_bar的使用

Features

一个简单易用的仪表盘条形图包!

1. 半圆形仪表盘条形图

video.gif

Getting started

在项目中添加依赖:

flutter pub add moon_gauge_bar

Usage

1. 半圆形仪表盘条形图
SizedBox(
    width: 200,
    height: 200,
    // 使用 moon gauge bar
    child: MoonGaugeBar.halfCircularGaugeBar(
        score: score,
        labelStyle: const MoonGaugeLabelStyleUIModel(
            startPointText: "0",
            endPointText: "100",
        ),
    ),
)

Additional information

properties objects
  1. gaugeStyle: MoonGaugeBarStyleUIModel()
    定义仪表盘条形图的样式。

  2. labelStyle: MoonGaugeLabelStyleUIModel()
    定义仪表盘条形图标签的样式。

  3. scoreStyle: MoonGaugeScoreStyleUIModel()
    定义仪表盘中心分数的样式。


完整示例代码

以下是完整的示例代码,展示如何使用 moon_gauge_bar 插件创建一个动态更新的半圆形仪表盘条形图。

import 'dart:math';

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

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

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

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

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

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  double score = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('moon gauge bar'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 半圆形仪表盘条形图
            SizedBox(
              width: 200,
              height: 200,
              child: MoonGaugeBar.halfCircularGaugeBar(
                score: score,
                labelStyle: const MoonGaugeLabelStyleUIModel(
                  startPointText: "0",
                  endPointText: "100",
                ),
              ),
            ),

            // 按钮用于动态更新分数
            ElevatedButton(
              onPressed: () {
                setState(() {
                  score = Random().nextInt(100).toDouble();
                });
              },
              child: const Text('改变分数'),
            )
          ],
        ),
      ),
    );
  }
}

更多关于Flutter仪表盘插件moon_gauge_bar的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter仪表盘插件moon_gauge_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


moon_gauge_bar 是一个用于 Flutter 的仪表盘插件,可以用来展示进度、指标或其他数据。它提供了一个可定制的仪表盘,可以用于各种场景,如健康应用、健身应用、车辆仪表盘等。

安装

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

dependencies:
  flutter:
    sdk: flutter
  moon_gauge_bar: ^1.0.0  # 请检查最新版本

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

基本使用

以下是一个简单的示例,展示如何使用 moon_gauge_bar 插件:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Moon Gauge Bar Example'),
        ),
        body: Center(
          child: MoonGaugeBar(
            value: 75, // 当前值
            maxValue: 100, // 最大值
            barColor: Colors.blue, // 进度条颜色
            backgroundColor: Colors.grey[300], // 背景颜色
            barWidth: 20, // 进度条宽度
            borderRadius: 10, // 圆角半径
            animationDuration: Duration(seconds: 1), // 动画持续时间
          ),
        ),
      ),
    );
  }
}

参数说明

  • value: 当前值,表示进度条的当前进度。
  • maxValue: 最大值,表示进度条的最大值。
  • barColor: 进度条的颜色。
  • backgroundColor: 进度条的背景颜色。
  • barWidth: 进度条的宽度。
  • borderRadius: 进度条的圆角半径。
  • animationDuration: 进度条动画的持续时间。

自定义

你可以通过调整这些参数来自定义 MoonGaugeBar 的外观和行为。例如,你可以改变颜色、宽度、圆角等,以适应你的应用设计。

高级用法

moon_gauge_bar 还支持更多的自定义选项,例如添加标签、刻度、动画效果等。你可以查看插件的文档或源代码,以了解更多高级用法。

示例代码

以下是一个更复杂的示例,展示了如何添加标签和刻度:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Moon Gauge Bar Example'),
        ),
        body: Center(
          child: MoonGaugeBar(
            value: 75,
            maxValue: 100,
            barColor: Colors.blue,
            backgroundColor: Colors.grey[300],
            barWidth: 20,
            borderRadius: 10,
            animationDuration: Duration(seconds: 1),
            label: Text('75%', style: TextStyle(fontSize: 20, color: Colors.black)),
            showTicks: true,
            tickCount: 5,
            tickColor: Colors.black,
          ),
        ),
      ),
    );
  }
}
回到顶部