Flutter系统信息获取插件system_info3的使用
Flutter系统信息获取插件system_info3的使用
system_info3
提供了轻松访问系统基本信息的方法,包括系统架构、内核、内存、操作系统、CPU 和用户环境等详细信息。
这是一个 system_info2
的分支,在原作者退出维护后由其他贡献者继续维护。感谢所有先前的贡献者为原始包所做的工作。
使用 system_info3
,你可以轻松地获取关键的硬件和操作系统特性,如:
- ✅ 内核架构
- ✅ 内核位数
- ✅ 内核名称
- ✅ 内核版本
- ✅ 操作系统名称
- ✅ 操作系统版本
- ✅ 用户目录
- ✅ 用户ID
- ✅ 用户名
- ✅ 用户空间位数
- ✅ 物理内存和交换分区详情
- ✅ CPU信息
开始使用
1. 安装包
Dart
dart pub add system_info3
Flutter
flutter pub add system_info3
2. 导入包
import 'package:system_info3/system_info3.dart';
3. 使用包
示例
if (SysInfo.operatingSystemName == "Ubuntu") {
print("We love Ubuntu users");
}
截图
兼容性
- ✅ Windows
- ✅ Linux
- ✅ MacOS
- ✅ Android
- ❌ iOS
使用说明
该插件提供了各种系统信息,如内核细节、操作系统信息、CPU规格和内存统计信息。以下是如何在不同类别中使用该插件的指南。 有关可用类和方法的更多详细信息,请参阅API文档。
1. 内核信息
你可以获取有关系统内核架构、版本和其他与内核相关的信息。
import 'package:system_info3/system_info3.dart';
void main() {
print('Kernel architecture : ${SysInfo.kernelArchitecture}');
print('Raw Kernel architecture : ${SysInfo.rawKernelArchitecture}');
print('Kernel bitness : ${SysInfo.kernelBitness}');
print('Kernel name : ${SysInfo.kernelName}');
print('Kernel version : ${SysInfo.kernelVersion}');
}
可用内核信息:
kernelArchitecture
: 内核架构(例如x86_64
,arm64
)。rawKernelArchitecture
: 内核原始架构。kernelBitness
: 内核位数(例如 32位或 64位)。kernelName
: 内核名称(例如Linux
,Darwin
)。kernelVersion
: 内核版本。
2. 操作系统(OS)信息
你可以获取有关操作系统、用户信息和系统用户环境的详细信息。
import 'package:system_info3/system_info3.dart';
void main() {
print('Operating system name : ${SysInfo.operatingSystemName}');
print('Operating system version: ${SysInfo.operatingSystemVersion}');
print('User directory : ${SysInfo.userDirectory}');
print('User id : ${SysInfo.userId}');
print('User name : ${SysInfo.userName}');
print('User space bitness : ${SysInfo.userSpaceBitness}');
}
可用操作系统信息:
operatingSystemName
: 操作系统名称(例如Windows
,Linux
,macOS
)。operatingSystemVersion
: 操作系统版本。userDirectory
: 当前用户的目录。userId
: 当前用户的ID。userName
: 当前用户名。userSpaceBitness
: 用户空间位数(32位或64位)。
3. CPU信息
本节提供有关CPU的详细信息,如核心数量及其特定特征。
import 'package:system_info3/system_info3.dart';
void main() {
final cores = SysInfo.cores;
print('Number of cores : ${cores.length}');
for (final core in cores) {
print(' Architecture : ${core.architecture}');
print(' Name : ${core.name}');
print(' Socket : ${core.socket}');
print(' Vendor : ${core.vendor}');
}
}
可用CPU信息:
cores
: 包含详细信息(如架构、名称、插槽和供应商)的CPU核心列表。architecture
: 核心架构(例如x86_64
,arm
)。name
: CPU核心名称(例如Intel(R) Core(TM) i7-9700K
)。socket
: 核心所在的插槽编号。vendor
: CPU供应商(例如Intel
,AMD
)。
4. 内存/RAM信息
本节显示系统中总物理内存和虚拟内存的可用量。
注意: getFreePhysicalMemory()
返回的是未使用的内存,但可能不会立即可用于分配。getAvailablePhysicalMemory()
返回的是可以被回收并准备使用的内存。此区别仅适用于Linux和Android。在macOS和Windows上,这两个值是相同的。
import 'package:system_info3/system_info3.dart';
void main() {
const int megaByte = 1024 * 1024;
print('Total physical memory : ${SysInfo.getTotalPhysicalMemory() ~/ megaByte} MB');
print('Free physical memory : ${SysInfo.getFreePhysicalMemory() ~/ megaByte} MB');
print('Available physical memory: ${SysInfo.getAvailablePhysicalMemory() ~/ megaByte} MB');
print('Total virtual memory : ${SysInfo.getTotalVirtualMemory() ~/ megaByte} MB');
print('Free virtual memory : ${SysInfo.getFreeVirtualMemory() ~/ megaByte} MB');
print('Virtual memory size : ${SysInfo.getVirtualMemorySize() ~/ megaByte} MB');
}
可用内存信息:
getTotalPhysicalMemory()
: 返回总物理内存(以字节为单位)。getFreePhysicalMemory()
: 返回自由物理内存(以字节为单位)。getTotalVirtualMemory()
: 返回总虚拟内存(以字节为单位)。getFreeVirtualMemory()
: 返回自由虚拟内存(以字节为单位)。getVirtualMemorySize()
: 返回虚拟内存大小(以字节为单位)。
注意: 内存值以字节为单位返回。示例代码将其转换为兆字节以便更易于阅读。
完整的实现示例请查看这里。使用以下命令运行示例:
dart example/example.dart
更多关于Flutter系统信息获取插件system_info3的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter系统信息获取插件system_info3的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
system_info_3
是一个用于获取设备系统信息的 Flutter 插件。它可以获取设备的 CPU 信息、内存信息、磁盘信息、操作系统信息等。以下是如何使用 system_info_3
插件的基本指南。
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 system_info_3
插件的依赖:
dependencies:
flutter:
sdk: flutter
system_info_3: ^0.1.1 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 导入包
在 Dart 文件中导入 system_info_3
包:
import 'package:system_info_3/system_info_3.dart';
3. 获取系统信息
system_info_3
提供了多种方法来获取不同的系统信息。以下是一些常用的方法:
获取操作系统信息
void getOsInfo() async {
try {
var osInfo = await SystemInfo.osInfo;
print('OS Name: ${osInfo.name}');
print('OS Version: ${osInfo.version}');
print('OS Build: ${osInfo.build}');
print('OS Architecture: ${osInfo.architecture}');
} catch (e) {
print('Failed to get OS info: $e');
}
}
获取 CPU 信息
void getCpuInfo() async {
try {
var cpuInfo = await SystemInfo.cpuInfo;
print('CPU Model: ${cpuInfo.model}');
print('CPU Cores: ${cpuInfo.cores}');
print('CPU Speed: ${cpuInfo.speed}');
} catch (e) {
print('Failed to get CPU info: $e');
}
}
获取内存信息
void getMemoryInfo() async {
try {
var memoryInfo = await SystemInfo.memoryInfo;
print('Total Memory: ${memoryInfo.total}');
print('Available Memory: ${memoryInfo.available}');
print('Used Memory: ${memoryInfo.used}');
} catch (e) {
print('Failed to get Memory info: $e');
}
}
获取磁盘信息
void getDiskInfo() async {
try {
var diskInfo = await SystemInfo.diskInfo;
print('Total Disk Space: ${diskInfo.total}');
print('Free Disk Space: ${diskInfo.free}');
print('Used Disk Space: ${diskInfo.used}');
} catch (e) {
print('Failed to get Disk info: $e');
}
}
4. 完整示例
以下是一个完整的示例,展示如何获取和打印系统信息:
import 'package:flutter/material.dart';
import 'package:system_info_3/system_info_3.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('System Info Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: getOsInfo,
child: Text('Get OS Info'),
),
ElevatedButton(
onPressed: getCpuInfo,
child: Text('Get CPU Info'),
),
ElevatedButton(
onPressed: getMemoryInfo,
child: Text('Get Memory Info'),
),
ElevatedButton(
onPressed: getDiskInfo,
child: Text('Get Disk Info'),
),
],
),
),
),
);
}
void getOsInfo() async {
try {
var osInfo = await SystemInfo.osInfo;
print('OS Name: ${osInfo.name}');
print('OS Version: ${osInfo.version}');
print('OS Build: ${osInfo.build}');
print('OS Architecture: ${osInfo.architecture}');
} catch (e) {
print('Failed to get OS info: $e');
}
}
void getCpuInfo() async {
try {
var cpuInfo = await SystemInfo.cpuInfo;
print('CPU Model: ${cpuInfo.model}');
print('CPU Cores: ${cpuInfo.cores}');
print('CPU Speed: ${cpuInfo.speed}');
} catch (e) {
print('Failed to get CPU info: $e');
}
}
void getMemoryInfo() async {
try {
var memoryInfo = await SystemInfo.memoryInfo;
print('Total Memory: ${memoryInfo.total}');
print('Available Memory: ${memoryInfo.available}');
print('Used Memory: ${memoryInfo.used}');
} catch (e) {
print('Failed to get Memory info: $e');
}
}
void getDiskInfo() async {
try {
var diskInfo = await SystemInfo.diskInfo;
print('Total Disk Space: ${diskInfo.total}');
print('Free Disk Space: ${diskInfo.free}');
print('Used Disk Space: ${diskInfo.used}');
} catch (e) {
print('Failed to get Disk info: $e');
}
}
}