Flutter垂直热力图展示插件flutter_vertical_heatmap的使用
Flutter垂直热力图展示插件flutter_vertical_heatmap的使用
Flutter Vertical Heatmap
🐙 一个为Flutter提供的垂直热力图图表包,提供精美设计的UI和终极灵活性!
开始使用
依赖它
flutter pub add flutter_vertical_heatmap
或
在你的个人包的pubspec.yaml
中添加以下行。
dependencies:
flutter_vertical_heatmap: ^1.0.3
然后运行flutter pub get
来安装。
导入它
import 'package:flutter_vertical_heatmap/flutter_vertical_heatmap.dart';
使用方法
下面是一个完整的示例demo,演示了如何使用flutter_vertical_heatmap
插件创建一个简单的垂直热力图:
import 'package:flutter/material.dart';
import 'package:flutter_vertical_heatmap/flutter_vertical_heatmap.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Vertical Heatmap Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late Map<DateTime, int> datasets = {};
@override
void initState() {
super.initState();
var now = DateTime.now();
datasets[DateTime(now.year, now.month, now.day)] = 1;
datasets[DateTime(now.year, now.month, now.day - 1)] = 2;
datasets[DateTime(now.year, now.month, now.day - 2)] = 3;
datasets[DateTime(now.year, now.month, now.day - 3)] = 4;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Flutter Vertical Heatmap'),
),
body: SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 80),
HeatMap(
startDate: HeatMapUtils.oneYearBefore(DateTime.now()),
endDate: DateTime.now(),
monthLabel: HeatMapDefault.monthLabel,
weekLabel: HeatMapDefault.weekLabel,
datasets: datasets,
size: 32,
// 如果你不想使用颜色提示,留空colorTipSize或传递null。
colorTipSize: 20,
margin: const EdgeInsets.all(3),
colorsets: {
1: Colors.green.shade100,
3: Colors.green.shade300,
5: Colors.green.shade500,
7: Colors.green.shade700,
},
colorTipLabel: const ["1~3", "3~5", "5~7", "7+"],
onClick: (value) {
setState(() {
datasets[value] =
datasets[value] != null ? datasets[value]! + 1 : 1;
});
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('${value.toString()} - ${datasets[value]}'),
),
);
},
),
const SizedBox(height: 80),
],
),
),
),
);
}
}
许可证
MIT License
Copyright © 2024 Lee Chang Woo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
更多关于Flutter垂直热力图展示插件flutter_vertical_heatmap的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter垂直热力图展示插件flutter_vertical_heatmap的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter中使用flutter_vertical_heatmap
插件来展示垂直热力图的示例代码。这个插件可以帮助你以垂直条形图的形式展示数据,通常用于表示某些数据的热度或频率。
首先,你需要在你的pubspec.yaml
文件中添加该插件的依赖:
dependencies:
flutter:
sdk: flutter
flutter_vertical_heatmap: ^最新版本号 # 请替换为实际发布的最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,你可以在你的Flutter项目中创建一个页面来展示垂直热力图。以下是一个完整的示例代码:
import 'package:flutter/material.dart';
import 'package:flutter_vertical_heatmap/flutter_vertical_heatmap.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Vertical Heatmap Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: VerticalHeatmapScreen(),
);
}
}
class VerticalHeatmapScreen extends StatelessWidget {
// 示例数据
final List<List<int>> data = [
[10, 20, 30, 40, 50],
[5, 15, 25, 35, 45],
[2, 12, 22, 32, 42],
[8, 18, 28, 38, 48],
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Vertical Heatmap Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: VerticalHeatmap(
data: data,
maxValue: 50, // 数据的最大值,用于确定颜色梯度
barWidth: 30.0, // 每个条形的宽度
barSpacing: 5.0, // 条形之间的间距
gradient: LinearGradient(
colors: [Colors.blue, Colors.red],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个VerticalHeatmapScreen
页面。这个页面使用VerticalHeatmap
组件来展示垂直热力图。
data
是一个二维列表,每个子列表代表一行数据。maxValue
用于确定颜色梯度,它应该设置为数据中的最大值。barWidth
和barSpacing
分别设置每个条形的宽度和条形之间的间距。gradient
定义了颜色梯度,用于根据数据值着色条形。
运行这个应用,你应该会看到一个简单的垂直热力图,其中条形的高度根据数据值变化,并且颜色根据定义的颜色梯度变化。
请确保你已经按照步骤正确添加了flutter_vertical_heatmap
插件,并且使用的是最新版本。如果插件的API有变化,请参考其官方文档进行调整。