Flutter自定义仪表盘插件customgauge_new的使用

Flutter自定义仪表盘插件customgauge_new的使用

customgauge_new

完全可定制的Flutter仪表盘小部件。

仪表盘示例

安装

在你的 pubspec.yaml 文件中添加以下依赖:

dependencies:
  customgauge_new: 1.0.2

示例用法

import 'package:customgauge_new/customgauge_new.dart';

CustomGauge(
    gaugeSize: 200,
    segments: [
        GaugeSegment('低', 20, Colors.red),
        GaugeSegment('中', 40, Colors.orange),
        GaugeSegment('高', 40, Colors.green),
    ],
    currentValue: 46,
    displayWidget: Text('油箱燃油量', style: TextStyle(fontSize: 12)),
),

特性

  • 完全功能的仪表盘小部件,从零开始构建。
  • 可以有任意数量的段,并且每个段可以有不同的长度和颜色。
  • 仪表盘可以设置为任何尺寸(仅限正方形),并且可以设置最小值和最大值。
  • 指针的颜色可以自定义。
  • 显示和数值小部件可以自定义,以便在仪表盘上显示所需的内容。
  • 可以开启或关闭标记,这些标记用于显示仪表盘上的最小值和最大值。你甚至可以自定义这些标记的样式!

许可证

该项目采用BSD 2-Clause许可证,详情请参阅LICENSE文件。

这个插件由jagan999开发,但后来被停止维护。我需要它,所以这里是dart 3版本。

示例代码

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

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

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

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

class MyAppState extends State<MyApp> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('CustomGauge 示例应用'),
        ),
        body: Container(
          color: Colors.grey[200],
          child: Column(
            children: [
              Expanded(
                child: ListView(
                  scrollDirection: Axis.horizontal,
                  children: [
                    CustomGauge(
                      gaugeSize: 200,
                      segments: [
                        GaugeSegment('低', 20, Colors.red),
                        GaugeSegment('中', 40, Colors.orange),
                        GaugeSegment('高', 40, Colors.green),
                      ],
                      currentValue: 60,
                      displayWidget: Text('油箱燃油量', style: TextStyle(fontSize: 12)),
                    ),
                    CustomGauge(
                      gaugeSize: 200,
                      segments: [
                        GaugeSegment('严重低', 10, Colors.red),
                        GaugeSegment('低', 20, Colors.orange),
                        GaugeSegment('中', 20, Colors.yellow),
                        GaugeSegment('高', 50, Colors.green),
                      ],
                      currentValue: 45,
                      needleColor: Colors.red,
                      showMarkers: false,
                      displayWidget: Text('油箱燃油量', style: TextStyle(fontSize: 12)),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: ListView(
                  scrollDirection: Axis.horizontal,
                  children: [
                    CustomGauge(
                      gaugeSize: 100,
                      segments: [
                        GaugeSegment('低', 20, Colors.blue[200]!),
                        GaugeSegment('中', 40, Colors.blue),
                        GaugeSegment('高', 40, Colors.blue[800]!),
                      ],
                      currentValue: 70,
                      displayWidget: Text('温度', style: TextStyle(fontSize: 12)),
                    ),
                    CustomGauge(
                      gaugeSize: 100,
                      segments: [
                        GaugeSegment('严重低', 10, Colors.red),
                        GaugeSegment('低', 20, Colors.orange),
                        GaugeSegment('中', 20, Colors.yellow),
                        GaugeSegment('高', 50, Colors.green),
                      ],
                      currentValue: 45,
                      needleColor: Colors.blue,
                      showMarkers: false,
                      valueWidget: Container(),
                      displayWidget: Text('油箱燃油量', style: TextStyle(fontSize: 12)),
                    ),
                    CustomGauge(
                      gaugeSize: 100,
                      minValue: 30,
                      maxValue: 150,
                      segments: [
                        GaugeSegment('低', 20, Colors.red),
                        GaugeSegment('稍低', 20, Colors.yellow),
                        GaugeSegment('正常', 20, Colors.green),
                        GaugeSegment('高', 60, Colors.orange),
                      ],
                      currentValue: 72,
                      displayWidget: Text('脉搏', style: TextStyle(fontSize: 12)),
                    ),
                    CustomGauge(
                      gaugeSize: 100,
                      minValue: 0,
                      maxValue: 150,
                      segments: [
                        GaugeSegment('好', 80, Colors.green),
                        GaugeSegment('高', 70, Colors.red),
                      ],
                      currentValue: 75,
                      showMarkers: false,
                      displayWidget: Text('速度', style: TextStyle(fontSize: 12)),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


customgauge_new 是一个用于 Flutter 的自定义仪表盘插件,可以帮助你创建各种类型的仪表盘,如速度计、温度计、进度指示器等。以下是使用 customgauge_new 插件的基本步骤和示例代码。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  customgauge_new: ^1.0.0  # 请确保使用最新版本

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

2. 导入插件

在你的 Dart 文件中导入 customgauge_new 插件:

import 'package:customgauge_new/customgauge_new.dart';

3. 使用 CustomGauge 组件

CustomGauge 组件提供了多种配置选项,你可以根据需要自定义仪表盘的外观和行为。

以下是一个简单的示例,展示如何使用 CustomGauge 创建一个基本的仪表盘:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Custom Gauge Example'),
        ),
        body: Center(
          child: CustomGauge(
            value: 50, // 当前值
            minValue: 0, // 最小值
            maxValue: 100, // 最大值
            displayValue: true, // 是否显示当前值
            valueFontSize: 30, // 值的字体大小
            valueColor: Colors.blue, // 值的颜色
            gaugeWidth: 20, // 仪表盘的宽度
            gaugeBackgroundColor: Colors.grey, // 仪表盘的背景颜色
            gaugeColor: Colors.green, // 仪表盘的颜色
            startAngle: 180, // 起始角度
            endAngle: 360, // 结束角度
            label: 'Speed', // 标签
            labelFontSize: 20, // 标签的字体大小
            labelColor: Colors.black, // 标签的颜色
          ),
        ),
      ),
    );
  }
}
回到顶部