Flutter应用更新日志展示插件gg_changelog的使用

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

Flutter应用更新日志展示插件gg_changelog的使用

gg_changelog 提供了各种工具来操作 Dart 的 CHANGELOG.md 文件。基于 cider 包。

示例代码

以下是一个简单的示例,展示了如何在 Flutter 应用中使用 gg_changelog 插件。

#!/usr/bin/env dart
// @license
// Copyright (c) 2019 - 2024 Dr. Gabriel Gatzsche. All Rights Reserved.
//
// Use of this source code is governed by terms that can be
// found in the LICENSE file in the root of this package.

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

Future<void> main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('gg_changelog 示例')),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              // 加载 CHANGELOG.md 文件
              final changelog = await Changelog.load();
              // 打印更新日志
              print(changelog);
            },
            child: Text('加载并显示更新日志'),
          ),
        ),
      ),
    );
  }
}

说明

  1. 导入包

    import 'package:flutter/material.dart';
    import 'package:gg_changelog/gg_changelog.dart';
    
  2. 主函数

    Future<void> main() async {
      runApp(MyApp());
    }
    
  3. MyApp 类

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: Text('gg_changelog 示例')),
            body: Center(
              child: ElevatedButton(
                onPressed: () async {
                  // 加载 CHANGELOG.md 文件
                  final changelog = await Changelog.load();
                  // 打印更新日志
                  print(changelog);
                },
                child: Text('加载并显示更新日志'),
              ),
            ),
          ),
        );
      }
    }
    
  4. 按钮点击事件

    onPressed: () async {
      // 加载 CHANGELOG.md 文件
      final changelog = await Changelog.load();
      // 打印更新日志
      print(changelog);
    },
    

更多关于Flutter应用更新日志展示插件gg_changelog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用更新日志展示插件gg_changelog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter应用中使用gg_changelog插件来展示更新日志的一个示例。这个插件可以帮助你在应用启动时显示一个更新日志对话框,让用户了解新版本的变化。

1. 添加依赖

首先,在你的pubspec.yaml文件中添加gg_changelog依赖:

dependencies:
  flutter:
    sdk: flutter
  gg_changelog: ^x.y.z  # 请替换为最新版本号

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

2. 创建更新日志文件

在你的项目根目录下创建一个名为changelog.json的文件,并在其中写入你的更新日志。例如:

[
    {
        "version": "1.0.1",
        "date": "2023-10-01",
        "changes": [
            "修复了一些已知的bug",
            "优化了应用的性能"
        ]
    },
    {
        "version": "1.0.0",
        "date": "2023-09-15",
        "changes": [
            "首次发布应用",
            "添加了主要功能A",
            "添加了主要功能B"
        ]
    }
]

3. 初始化并使用gg_changelog

在你的main.dart文件中,初始化并使用gg_changelog插件。以下是一个示例代码:

import 'package:flutter/material.dart';
import 'package:gg_changelog/gg_changelog.dart';
import 'dart:convert';
import 'dart:io';

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

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    _checkAndShowChangelog();
  }

  Future<void> _checkAndShowChangelog() async {
    // 读取 changelog.json 文件
    final File changelogFile = File('changelog.json');
    if (await changelogFile.exists()) {
      final String changelogContent = await changelogFile.readAsString();
      final List<Changelog> changelogs = changelogContentFromJson(changelogContent);
      
      // 获取当前应用的版本号(这里假设你在pubspec.yaml中定义了版本号)
      final PackageInfo packageInfo = await PackageInfo.fromPlatform();
      final String currentVersion = packageInfo.version;
      
      // 查找当前版本的更新日志
      Changelog? currentChangelog = changelogs.firstWhereOrNull((changelog) => changelog.version == currentVersion);
      
      // 如果找到了当前版本的更新日志,并且用户之前没有看过,则显示更新日志
      // 这里假设你有一个SharedPreferences来存储用户是否已经看过更新日志
      final SharedPreferences preferences = await SharedPreferences.getInstance();
      final bool hasSeenCurrentVersion = preferences.getBool('hasSeenVersion_${currentVersion}') ?? false;
      
      if (currentChangelog != null && !hasSeenCurrentVersion) {
        showDialog(
          context: context,
          builder: (context) => ChangelogDialog(
            changelog: currentChangelog,
            onDismissed: () {
              // 用户关闭更新日志后,标记为已看
              preferences.setBool('hasSeenVersion_${currentVersion}', true);
            },
          ),
        );
      }
    }
  }

  // 辅助函数:将JSON字符串转换为Changelog对象列表
  List<Changelog> changelogContentFromJson(String str) => List<Changelog>.from(
      jsonDecode(str).map((x) => Changelog.fromJson(x)));

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Text('Hello, Flutter!'),
      ),
    );
  }
}

// 辅助类:表示更新日志的模型
class Changelog {
  String version;
  String date;
  List<String> changes;

  Changelog({
    required this.version,
    required this.date,
    required this.changes,
  });

  factory Changelog.fromJson(Map<String, dynamic> json) => Changelog(
        version: json['version'] as String,
        date: json['date'] as String,
        changes: List<String>.from(json['changes'] as List),
      );
}

注意事项

  1. 权限处理:确保你的应用有读取本地文件的权限(在Flutter中通常不需要额外处理,除非你在特定平台上运行)。
  2. SharedPreferences:用于存储用户是否已经看过某个版本的更新日志。如果你还没有添加shared_preferences依赖,请在你的pubspec.yaml文件中添加它。
  3. 错误处理:在实际应用中,你应该添加适当的错误处理逻辑,比如处理文件读取失败的情况。

这个示例展示了如何在Flutter应用中使用gg_changelog插件来展示更新日志。你可以根据实际需求进一步自定义和扩展这个示例。

回到顶部