Flutter仪表盘插件new_flutter_gauge的使用

发布于 1周前 作者 h691938207 来自 Flutter

Flutter仪表盘插件new_flutter_gauge的使用

特性

以下是一些可以通过该库生成的图形示例:



开始使用

您可以在任何Flutter项目中使用此包。

import 'package:new_flutter_gauge/new_flutter_gauge.dart';

使用方法

要查看如何生成上述图像的完整示例,请查看页面的示例部分。

示例代码

以下是使用new_flutter_gauge的完整示例代码:

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Gauge Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Gauge Example'),
      ),
      body: ListView(
        padding: const EdgeInsets.symmetric(vertical: 20.0),
        children: [
          // 第一个仪表盘示例
          const FlutterGauge(
            index: 50, // 当前值
            hand: Hand.short, // 指针类型
            number: Number.endAndCenterAndStart, // 数字类型
            secondsMarker: SecondsMarker.secondsAndMinute, // 秒标记
            counterStyle: TextStyle(
              color: Colors.black,
              fontSize: 25,
            ), // 文本样式
          ),
          // 第二个仪表盘示例
          const FlutterGauge(
            secondsMarker: SecondsMarker.none, // 无秒标记
            hand: Hand.short, // 短指针
            number: Number.none, // 无数字
            width: 200, // 宽度
            index: 38.0, // 当前值
            counterStyle: TextStyle(
              color: Colors.black,
              fontSize: 35,
            ), // 文本样式
            counterAlign: CounterAlign.center, // 文本对齐方式
            isDecimal: false, // 不显示小数
          ),
          // 第三个仪表盘示例
          const FlutterGauge(
            handSize: 30, // 指针长度
            width: 200, // 宽度
            index: 50.0, // 当前值
            end: 100, // 最大值
            number: Number.endAndCenterAndStart, // 数字类型
            secondsMarker: SecondsMarker.secondsAndMinute, // 秒标记
            counterStyle: TextStyle(
              color: Colors.black,
              fontSize: 25,
            ), // 文本样式
          ),
          // 第四个仪表盘示例
          const FlutterGauge(
            handSize: 30, // 指针长度
            width: 200, // 宽度
            index: 65.0, // 当前值
            end: 500, // 最大值
            number: Number.endAndStart, // 数字类型
            secondsMarker: SecondsMarker.minutes, // 分钟标记
            isCircle: false, // 非圆形
            counterStyle: TextStyle(
              color: Colors.black,
              fontSize: 25,
            ), // 文本样式
          ),
          // 第五个仪表盘示例
          Container(
            color: Colors.black38,
            child: const FlutterGauge(
              inactiveColor: Colors.white38, // 未激活颜色
              activeColor: Colors.white, // 激活颜色
              handSize: 30, // 指针长度
              width: 200, // 宽度
              index: 65.0, // 当前值
              end: 400, // 最大值
              number: Number.none, // 无数字
              secondsMarker: SecondsMarker.minutes, // 分钟标记
              isCircle: false, // 非圆形
              hand: Hand.none, // 无指针
              counterAlign: CounterAlign.center, // 文本居中
              counterStyle: TextStyle(
                color: Colors.white,
                fontSize: 30,
              ), // 文本样式
              isDecimal: false, // 不显示小数
            ),
          ),
          // 第六个仪表盘示例
          const FlutterGauge(
            index: 50.0, // 当前值
            counterStyle: TextStyle(
              color: Colors.black,
              fontSize: 25,
            ), // 文本样式
            widthCircle: 10, // 圆圈宽度
            secondsMarker: SecondsMarker.none, // 无秒标记
            number: Number.all, // 全部数字
          ),
        ],
      ),
    );
  }
}

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

1 回复

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


new_flutter_gauge 是一个用于在 Flutter 应用中创建仪表盘的插件。它提供了多种类型的仪表盘,如圆形仪表盘、线性仪表盘等,并且可以自定义颜色、刻度、指针等属性。

安装

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

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

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

基本用法

以下是一个简单的示例,展示如何使用 new_flutter_gauge 创建一个圆形仪表盘:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Gauge Example'),
        ),
        body: Center(
          child: CircularGauge(
            gaugeSize: 300,
            minValue: 0,
            maxValue: 100,
            currentValue: 50,
            displayText: 'Speed',
            unit: 'km/h',
            needleColor: Colors.red,
            startAngle: 180,
            endAngle: 0,
            segments: [
              GaugeSegment(
                color: Colors.green,
                start: 0,
                end: 50,
              ),
              GaugeSegment(
                color: Colors.yellow,
                start: 50,
                end: 80,
              ),
              GaugeSegment(
                color: Colors.red,
                start: 80,
                end: 100,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

参数说明

  • gaugeSize: 仪表盘的大小。
  • minValue: 仪表盘的最小值。
  • maxValue: 仪表盘的最大值。
  • currentValue: 当前指针指向的值。
  • displayText: 仪表盘上显示的文本。
  • unit: 仪表盘上显示的单位。
  • needleColor: 指针的颜色。
  • startAngle: 仪表盘的起始角度。
  • endAngle: 仪表盘的结束角度。
  • segments: 仪表盘的分段,可以设置不同颜色范围。

自定义

你可以通过调整这些参数来自定义仪表盘的外观和行为。例如,你可以改变指针的颜色、调整仪表盘的角度范围、添加更多的分段等。

其他类型的仪表盘

new_flutter_gauge 还支持其他类型的仪表盘,如线性仪表盘。你可以根据需要使用 LinearGauge 来创建线性仪表盘。

LinearGauge(
  minValue: 0,
  maxValue: 100,
  currentValue: 75,
  displayText: 'Progress',
  unit: '%',
  gaugeColor: Colors.blue,
  valueColor: Colors.green,
),
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!