Flutter应用更新说明插件release_notes_dialog的使用

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

Flutter应用更新说明插件release_notes_dialog的使用

release_notes_dialog 是一个易于使用的发布说明对话框插件。它可以自定义以适应您的应用程序设计。

示例截图

该插件旨在补充现有的 AboutDialog

安装

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

dependencies:
  release_notes_dialog: "^2.0.0"

在您的 .dart 文件中导入插件:

import 'package:release_notes_dialog/release_notes_dialog.dart';

基本用法

ReleaseNotesDialog 需要一个 Release 列表。每个 Release 需要一个标题,并包含一个 ChangeGroup 列表。ChangeGroup 用于对您的更改进行分组,例如功能、修复、改进等。

创建一个 Release 列表:

final List<Release> releases = [
  Release(
    title: "1.1.0",
    changes: [
      ChangeGroup(
        title: "Features",
        changes: [
          "Added a new feature",
          "Added a second feature",
        ],
      ),
      ChangeGroup(
        title: "Fixes",
        changes: [
          "Fixed the first bug",
          "Fixed a happy little accident",
          "Fixed another bug",
        ],
      ),
    ],
  ),
  Release(
    title: "1.0.0",
    changes: [
      ChangeGroup(
        title: "Release!",
      ),
    ],
  ),
];

您可以以多种方式显示这些发布说明:

使用 showDialog() 函数显示 ReleaseNotesDialog

ElevatedButton(
  onPressed: () => showDialog(
    context: context,
    builder: (BuildContext context) {
      return ReleaseNotesDialog(releases: releases);
    },
  ),
  child: Text("Show ReleaseNotesDialog"),
);

使用 showReleaseNotesDialog() 函数显示对话框

ElevatedButton(
  onPressed: () => showReleaseNotesDialog(
    context: context,
    releases: releases,
  ),
  child: Text("Show ReleaseNotesDialog"),
);

使用 Navigator 显示 ReleaseNotesPage

ElevatedButton(
  onPressed: () => Navigator.of(context, rootNavigator: useRootNavigator).push(
    MaterialPageRoute<void>(
      builder: (BuildContext context) {
        return ReleaseNotesPage(
          releases: releases,
        );
      },
    ),
  ),
  child: Text("Show ReleaseNotesPage"),
);

使用 showReleaseNotesPage() 函数显示页面

ElevatedButton(
  onPressed: () => showReleaseNotesPage(
    context: context,
    releases: releases,
  ),
  child: Text("Show ReleaseNotesPage"),
);

显示 ReleaseNotesListTile

ReleaseNotesListTile(releases: releases);

显示 ReleaseNotesWidget

ReleaseNotesWidget(releases: releases);

示例代码

以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 release_notes_dialog 插件:

import 'dart:io';

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

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

class ExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ReleaseNotesDialog Example',
      theme: ThemeData(
        useMaterial3: true,
      ),
      home: ExamplePage(),
    );
  }
}

class ExamplePage extends StatelessWidget {
  final List<Release> releases = [
    Release(
      title: "1.1.0",
      changes: [
        ChangeGroup(
          title: "Features",
          changes: [
            "Added new feature 1",
            "Added new feature 2",
            "Added a very long feature to show how multiple lines work",
          ],
        ),
        ChangeGroup(
          title: "Fixes",
          changes: [
            if (Platform.isAndroid) "Fixed bug on Android",
            if (Platform.isIOS) "Fixed bug on iOS",
            "Fixed a very long bug to show multiple lines again",
            "Fixed bug 1",
            "Fixed bug 2",
            "Fixed bug 3",
          ],
        ),
      ],
    ),
    Release(
      title: "1.0.0",
      changes: [ChangeGroup(title: "Release!")],
    ),
  ];

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('ReleaseNotesDialog Example'),
      ),
      body: ListView(
        padding: EdgeInsets.symmetric(vertical: 20),
        children: [
          Padding(
            padding: EdgeInsets.only(bottom: 15),
            child: Text(
              "Dialogs",
              style: theme.textTheme.headlineLarge,
              textAlign: TextAlign.center,
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ElevatedButton(
                onPressed: () => showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return ReleaseNotesDialog(releases: releases);
                  },
                ),
                child: Text("Show ReleaseNotesDialog"),
              ),
              ElevatedButton(
                onPressed: () => showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AboutDialog(
                      applicationVersion: releases.first.title,
                    );
                  },
                ),
                child: Text("Show AboutDialog"),
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.only(top: 50, bottom: 15),
            child: Text(
              "Pages",
              style: theme.textTheme.headlineLarge,
              textAlign: TextAlign.center,
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ElevatedButton(
                onPressed: () => showReleaseNotesPage(
                  context: context,
                  releases: releases,
                ),
                child: Text("Show ReleaseNotesPage"),
              ),
              ElevatedButton(
                onPressed: () => showLicensePage(
                  context: context,
                  applicationVersion: releases.first.title,
                ),
                child: Text("Show LicensePage"),
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.only(top: 50, bottom: 15),
            child: Text(
              "ListTiles",
              style: theme.textTheme.headlineLarge,
              textAlign: TextAlign.center,
            ),
          ),
          ReleaseNotesListTile(releases: releases),
          AboutListTile(),
        ],
      ),
    );
  }
}

贡献

欢迎贡献、功能请求和错误报告!


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

1 回复

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


当然,下面是一个关于如何在Flutter应用中使用release_notes_dialog插件的详细代码示例。这个插件可以帮助你在应用中展示更新说明(release notes)。

1. 添加依赖

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

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

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

2. 导入插件

在你的Dart文件中(通常是main.dart或某个具体的页面文件),导入release_notes_dialog

import 'package:release_notes_dialog/release_notes_dialog.dart';

3. 准备更新说明数据

你需要准备一个包含更新说明的数据结构。通常,你可以将这些数据存储在本地文件、远程服务器或者你的应用的配置文件中。

这里我们假设你有一个简单的JSON文件release_notes.json,内容如下:

[
  {
    "version": "1.0.1",
    "notes": "修复了一些小错误,优化了用户体验。"
  },
  {
    "version": "1.0.0",
    "notes": "初始版本发布,包含基本功能。"
  }
]

4. 读取并显示更新说明

在你的应用中,读取这个JSON文件并显示更新说明。以下是一个完整的示例:

import 'dart:convert';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:release_notes_dialog/release_notes_dialog.dart';
import 'package:flutter/services.dart' show rootBundle;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Release Notes Demo'),
        ),
        body: Center(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {
  List<ReleaseNote> _releaseNotes = [];

  @override
  void initState() {
    super.initState();
    _loadReleaseNotes();
  }

  Future<void> _loadReleaseNotes() async {
    String jsonString = await rootBundle.loadString('assets/release_notes.json');
    List<dynamic> jsonList = jsonDecode(jsonString);

    List<ReleaseNote> releaseNotes = jsonList.map((item) => ReleaseNote(
      version: item['version'],
      notes: item['notes']
    )).toList();

    setState(() {
      _releaseNotes = releaseNotes;
    });

    // 假设当前版本是1.0.1,检查并显示更新说明
    String currentVersion = "1.0.1";
    _showReleaseNotesDialogIfNeeded(currentVersion);
  }

  void _showReleaseNotesDialogIfNeeded(String currentVersion) {
    ReleaseNote latestNote = _releaseNotes.firstWhereOrNull((note) => note.version == currentVersion);
    if (latestNote != null && latestNote.notes != null && latestNote.notes.isNotEmpty) {
      showDialog(
        context: context,
        builder: (context) => ReleaseNotesDialog(
          releaseNote: latestNote,
          title: Text('新版本更新'),
          positiveButtonText: Text('确定'),
          onPositiveButtonPressed: () {
            Navigator.of(context).pop();
          },
        ),
      );
    }
  }
}

class ReleaseNote {
  String version;
  String notes;

  ReleaseNote({required this.version, required this.notes});
}

5. 确保资源文件正确引用

确保你在pubspec.yaml中添加了release_notes.json文件作为资源:

flutter:
  assets:
    - assets/release_notes.json

总结

上述代码展示了如何在Flutter应用中使用release_notes_dialog插件来显示更新说明。这个示例包括读取JSON格式的更新说明数据、在应用中显示对话框以及根据当前版本显示相应的更新内容。你可以根据自己的需求进一步定制和扩展这个示例。

回到顶部