Flutter Windows构建信息获取插件build_info_windows的使用

Flutter Windows构建信息获取插件build_info_windows的使用

使用

这个包是被支持的,这意味着你可以像使用普通的包一样使用build_info。当你这样做时,这个包会自动包含在你的应用中,因此你不需要在pubspec.yaml文件中添加它。

然而,如果你导入这个包来直接使用它的API,你应该像往常一样将它添加到你的pubspec.yaml文件中。

关于Windows版本的实现

Windows版本的build_info的工作方式如下:

  1. 从Flutter调用BuildInfo.fromPlatform方法。
  2. 获取IMAGE_NT_HEADERS64结构,并使用IMAGE_FILE_HEADER结构中的TimeDateStamp值作为构建日期和时间。
  3. 使用可执行文件的创建日期和时间或可执行文件所在目录的创建日期和时间中较早的一个作为安装日期和时间。

如果无法获取每个日期和时间,则它们将为null。

IMAGE_FILE_HEADER结构中用于构建日期和时间的TimeDateStamp字段是DWORD类型,因此在2106年2月7日06:28:15 UTC之后会溢出。因此,如果接收到的是2024年1月1日00:00:00 UTC之前的时间戳,将会加上0x100000000。(日期格式yyyy/mm/dd)

int64_t _tmp = inh.FileHeader.TimeDateStamp;
if (_tmp < 1704067200 /* 2024-01-01 00:00:00 UTC */) {
    _tmp += 0x100000000;
}

示例代码

以下是一个完整的示例代码,展示了如何使用build_info_windows插件来获取构建和安装日期。

// Copyright 2024 Craftsoft LLC. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:build_info_platform_interface/build_info_data.dart';
import 'package:build_info_windows/build_info_windows.dart';

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

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  BuildInfoData? _buildInfoData;
  final _buildInfoWindowsPlugin = BuildInfoWindows();

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 平台消息是异步的,所以我们初始化在一个异步方法中。
  Future<void> initPlatformState() async {
    BuildInfoData? buildInfoData;
    // 平台消息可能会失败,所以我们使用try/catch处理PlatformException。
    // 我们还处理消息可能返回null的情况。
    try {
      buildInfoData = await _buildInfoWindowsPlugin.fromPlatform();
    } on PlatformException catch (e, stackTrace) {
      debugPrint(e.message);
      debugPrintStack(stackTrace: stackTrace);
    }

    // 如果在异步平台消息飞行期间小部件从树中移除,我们想要丢弃回复而不是调用setState来更新我们的不存在的外观。
    if (!mounted) return;

    setState(() {
      _buildInfoData = buildInfoData;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件示例应用'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text('BuildDate: ${_buildInfoData?.buildDate}'),
              Text('InstallDate: ${_buildInfoData?.installDate}'),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter Windows构建信息获取插件build_info_windows的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter Windows构建信息获取插件build_info_windows的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用build_info_windows插件来获取Windows构建信息的代码示例。这个插件允许你访问Windows应用程序的构建信息,例如产品名称、公司名称、文件版本等。

首先,确保你的Flutter环境已经配置好,并且你的项目已经创建。然后,你可以按照以下步骤来使用build_info_windows插件。

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  build_info_windows: ^0.1.3  # 请检查最新版本号

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

2. 使用插件

接下来,你可以在你的Dart代码中导入并使用这个插件。以下是一个简单的示例,展示如何获取并显示Windows构建信息:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Build Info Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BuildInfoScreen(),
    );
  }
}

class BuildInfoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // 获取Windows构建信息
    final buildInfo = WindowsBuildInfo();

    return Scaffold(
      appBar: AppBar(
        title: Text('Windows Build Info'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('Product Name: ${buildInfo.productName}'),
            SizedBox(height: 16),
            Text('Company Name: ${buildInfo.companyName}'),
            SizedBox(height: 16),
            Text('File Version: ${buildInfo.fileVersion}'),
            SizedBox(height: 16),
            Text('Product Version: ${buildInfo.productVersion}'),
            SizedBox(height: 16),
            Text('Internal Name: ${buildInfo.internalName}'),
            SizedBox(height: 16),
            Text('Original Filename: ${buildInfo.originalFilename}'),
          ],
        ),
      ),
    );
  }
}

3. 运行应用

确保你的Flutter项目已经在Windows平台上配置好,然后运行应用:

flutter run -d windows

这将启动你的Flutter应用,并在UI中显示Windows构建信息。

注意事项

  • 确保你的Windows项目已经正确配置,并且你已经生成了包含构建信息的可执行文件。
  • build_info_windows插件依赖于Windows可执行文件的资源信息,因此,如果你没有正确设置这些信息,插件可能无法获取到预期的构建信息。

通过上述步骤,你应该能够在Flutter Windows应用中成功获取并显示Windows构建信息。

回到顶部