Flutter引导教程插件showcase_tutorial的使用

Flutter引导教程插件showcase_tutorial的使用

ShowCaseView

Flutter的一个包,允许你逐步展示/高亮你的小部件。

预览

以下是示例应用在iOS上运行的效果图: The example app running in IOS

1.0.0版本迁移指南

一些属性/字段名称发生了变化:

旧名称 新名称
autoPlayLockEnable enableAutoPlayLock
shapeBorder targetShapeBorder
showcaseBackgroundColor tooltipBackgroundColor
contentPadding tooltipPadding
overlayPadding targetPadding
radius targetBorderRadius
tipBorderRadius tooltipBorderRadius
disableAnimation disableMovingAnimation
animationDuration movingAnimationDuration

Showcase.withWidget()中移除了一些未使用的参数:

  • title
  • titleAlignment
  • titleTextStyle
  • description
  • descriptionAlignment
  • descTextStyle
  • textColor
  • tooltipBackgroundColor
  • tooltipBorderRadius
  • tooltipPadding

安装

  1. 在pubspec.yaml中添加依赖:
dependencies:
    showcaseview: <latest-version>
  1. 导入包:
import 'package:showcaseview/showcase_tutorial.dart';
  1. 添加ShowCaseWidget小部件:
ShowCaseWidget(
  builder: Builder(
    builder: (context) => Somewidget()
  ),
),
  1. 添加Showcase小部件:
GlobalKey _one = GlobalKey();
GlobalKey _two = GlobalKey();
GlobalKey _three = GlobalKey();

// ...

Showcase(
  key: _one,
  title: 'Menu',
  description: 'Click here to see menu options',
  child: Icon(
    Icons.menu,
    color: Colors.black45,
  ),
),

Showcase.withWidget(
  key: _three,
  height: 80,
  width: 140,
  targetShapeBorder: CircleBorder(),
  container: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      // 自定义内容
    ],
  ),
  child: ..., // 目标小部件
),
  1. 开始ShowCase:
void someEvent() {
    ShowCaseWidget.of(context).startShowCase([_one, _two, _three]);
}

// 如果希望UI构建完成后立即开始ShowCase
WidgetsBinding.instance.addPostFrameCallback((_) =>
  ShowCaseWidget.of(context).startShowCase([_one, _two, _three])
);

ShowCaseWidget.of(context)的功能

功能名 描述
startShowCase(List) 启动展示
next() 启动下一个展示
previous() 启动上一个展示
dismiss() 关闭所有展示

ShowCaseWidget的属性

名称 类型 默认行为 描述
blurValue double 0 提供模糊效果
autoPlay bool false 自动显示下一个展示
autoPlayDelay Duration 2000毫秒 当启用autoplay时的展示时间
enableAutoPlayLock bool false 启用自动播放时阻止用户交互
… … … …

Showcase和Showcase.withWidget的属性

名称 类型 默认行为 描述 Showcase Showcase.withWidget
key GlobalKey 每个展示的唯一全局键 ✅ ✅
child Widget 要展示的目标小部件 ✅ ✅
title String? 默认提示框标题 ✅
description String? 默认提示框描述 ✅
container Widget? 允许创建自定义提示框小部件 ✅
… … … … … …

如何使用

请查看example目录中的示例应用或pub.dartlang.org上的’Example’标签以获取更完整的示例。

自动滚动到活动展示

在渲染小部件的需求视图(如ListView、GridView)中,自动滚动到活动展示可能无法正常工作。如果使用SingleChildScrollView不是选项,则可以将ScrollController分配给该视图,并手动滚动到展示小部件的位置。例如:

final _controller = ScrollController();

ShowCaseWidget(
  onStart: (index, key) {
    if(index == 0) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        // 如果展示小部件在列表视图中的偏移量为1000。
        // 如果不知道确切位置,可以提供最近的可能位置。
        _controller.jumpTo(1000);
      });
    }
  },
);

完整示例代码

以下是一个完整的示例代码,演示如何使用showcaseview插件:

import 'package:flutter/material.dart';
import 'package:showcaseview/showcase_tutorial.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ShowCaseWidget(
        builder: Builder(
          builder: (context) => HomePage()
        ),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  [@override](/user/override)
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final GlobalKey _one = GlobalKey();
  final GlobalKey _two = GlobalKey();
  final GlobalKey _three = GlobalKey();

  [@override](/user/override)
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) =>
      ShowCaseWidget.of(context).startShowCase([_one, _two, _three])
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Showcase Tutorial'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Showcase(
              key: _one,
              title: 'Menu',
              description: 'Click here to see menu options',
              child: Icon(
                Icons.menu,
                color: Colors.black45,
              ),
            ),
            SizedBox(height: 20),
            Showcase(
              key: _two,
              title: 'Search',
              description: 'Click here to search',
              child: Icon(
                Icons.search,
                color: Colors.black45,
              ),
            ),
            SizedBox(height: 20),
            Showcase.withWidget(
              key: _three,
              height: 80,
              width: 140,
              targetShapeBorder: CircleBorder(),
              container: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('Custom Tooltip'),
                  Text('This is a custom tooltip')
                ],
              ),
              child: Icon(
                Icons.notifications,
                color: Colors.black45,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter引导教程插件showcase_tutorial的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


当然,以下是如何在Flutter应用中使用showcase_tutorial插件来创建引导教程的详细代码示例。showcase_tutorial插件允许你在应用中创建精美的引导提示,帮助用户了解应用的主要功能。

第一步:添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  showcase_tutorial: ^1.2.0  # 请确保使用最新版本

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

第二步:导入插件

在你的主Dart文件(通常是main.dart)或者你需要使用引导教程的文件中导入插件:

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

第三步:设置引导教程

以下是一个完整的示例,展示了如何在一个简单的Flutter应用中设置和使用showcase_tutorial插件:

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

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

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

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  final ShowcaseTutorialController _showcaseController = ShowcaseTutorialController();

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      _showTutorial();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text('Showcase Tutorial Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ElevatedButton(
              onPressed: () {},
              child: Text('Button 1'),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {},
              child: Text('Button 2'),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }

  void _showTutorial() {
    _showcaseController.start(
      context,
      [
        // 第一个引导项:展示第一个按钮
        Showcase(
          title: 'Button 1',
          description: 'This is the first button!',
          child: find.byType(ElevatedButton).first, // 查找第一个ElevatedButton
        ),
        // 第二个引导项:展示第二个按钮
        Showcase(
          title: 'Button 2',
          description: 'This is the second button!',
          child: find.byType(ElevatedButton).skip(1).first, // 查找第二个ElevatedButton
        ),
        // 第三个引导项:展示浮动操作按钮
        Showcase(
          title: 'Floating Action Button',
          description: 'This is the floating action button!',
          child: find.byType(FloatingActionButton),
        ),
      ],
      // 引导教程结束后调用的回调
      onCompleted: () {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Tutorial Completed!')));
      },
    );
  }

  @override
  void dispose() {
    _showcaseController.dispose(); // 释放资源
    super.dispose();
  }
}

代码说明

  1. 添加依赖:在pubspec.yaml中添加showcase_tutorial依赖。
  2. 导入插件:在需要使用引导教程的文件中导入showcase_tutorial。
  3. 设置引导教程:
    • 使用ShowcaseTutorialController来管理引导教程。
    • 在initState方法中,通过Future.delayed(Duration.zero, ...)立即开始展示引导教程。
    • 使用Showcase类定义每个引导项,包括标题、描述和目标子组件(通过find.byType或类似的方法查找)。
    • onCompleted回调用于在引导教程完成后显示一个SnackBar。
  4. 释放资源:在dispose方法中释放ShowcaseTutorialController资源。

这个示例展示了如何使用showcase_tutorial插件来创建一个简单的引导教程。你可以根据需要调整引导项的内容和样式。

回到顶部