Flutter今日展示视图插件today_showcaseview的使用

Flutter今日展示视图插件today_showcaseview的使用

today_showcaseview

pub

Flutter 今日风格的展示视图插件!


使用

today_showcaseview 是一个用于在 Flutter 应用中创建引导提示或展示视图的插件。它提供了类似于 Apple Today 插件的风格,帮助开发者为用户提供友好的引导体验。

安装

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

dependencies:
  today_showcaseview: ^版本号

然后运行以下命令以安装依赖:

flutter pub get

示例代码

以下是一个完整的示例代码,演示如何使用 today_showcaseview 插件来创建引导视图。

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ShowcaseViewExample(),
    );
  }
}

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

class _ShowcaseViewExampleState extends State<ShowcaseViewExample> {
  // 控制是否显示引导视图
  bool isShowcaseActive = true;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Today ShowcaseView 示例'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                // 点击按钮时隐藏引导视图
                setState(() {
                  isShowcaseActive = false;
                });
              },
              child: Text('完成引导'),
            ),
            SizedBox(height: 20),
            if (isShowcaseActive)
              // 使用 TodayShowcaseView 创建引导视图
              TodayShowcaseView(
                // 指定目标 widget 的键
                keyWidgetKey: Key('target_widget'),
                // 提示文本
                contentText: '这是目标区域!点击按钮完成引导。',
                // 显示样式
                shapeBorder: CircleBorder(),
                // 引导视图的偏移量
                padding: EdgeInsets.all(20),
                // 引导视图的背景颜色
                backgroundColor: Colors.blue.withOpacity(0.8),
                // 引导视图的文字颜色
                textColor: Colors.white,
                // 箭头方向
                arrowDirection: ArrowDirection.bottom,
                // 确认回调
                onConfirm: () {
                  setState(() {
                    isShowcaseActive = false;
                  });
                },
              )
          ],
        ),
      ),
    );
  }
}

更多关于Flutter今日展示视图插件today_showcaseview的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


today_showcaseview 是一个用于在 Flutter 应用中展示引导视图的插件。它可以帮助你在应用中高亮显示某些特定的 UI 元素,并向用户提供引导信息。这个插件通常用于新用户引导、功能提示等场景。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 today_showcaseview 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  today_showcaseview: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装插件。

基本用法

  1. 导入插件

    在你的 Dart 文件中导入 today_showcaseview 插件:

    import 'package:today_showcaseview/today_showcaseview.dart';
    
  2. 创建 Showcase 视图

    使用 Showcase 组件来包裹你想要高亮的 UI 元素。Showcase 组件需要以下几个参数:

    • key: 一个唯一的 GlobalKey,用于标识这个 Showcase
    • title: 引导视图的标题。
    • description: 引导视图的描述信息。
    • child: 你想要高亮的子组件。

    示例代码:

    class MyHomePage extends StatelessWidget {
      final GlobalKey _showcaseKey = GlobalKey();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('ShowcaseView Example'),
          ),
          body: Center(
            child: Showcase(
              key: _showcaseKey,
              title: 'Welcome',
              description: 'This is a showcase view example.',
              child: ElevatedButton(
                onPressed: () {
                  // 按钮点击事件
                },
                child: Text('Click Me'),
              ),
            ),
          ),
        );
      }
    }
    
  3. 启动 Showcase

    你可以使用 ShowCaseWidget 来启动展示引导视图。通常,你会在 initState 或某个事件触发时启动它。

    示例代码:

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final GlobalKey _showcaseKey = GlobalKey();
    
      @override
      void initState() {
        super.initState();
        WidgetsBinding.instance.addPostFrameCallback((_) {
          ShowCaseWidget.of(context).startShowCase([_showcaseKey]);
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('ShowcaseView Example'),
          ),
          body: Center(
            child: Showcase(
              key: _showcaseKey,
              title: 'Welcome',
              description: 'This is a showcase view example.',
              child: ElevatedButton(
                onPressed: () {
                  // 按钮点击事件
                },
                child: Text('Click Me'),
              ),
            ),
          ),
        );
      }
    }
    
  4. 自定义样式

    你可以通过 Showcase 组件的 overlayColortextColortitleTextStyle 等参数来自定义引导视图的样式。

    示例代码:

    Showcase(
      key: _showcaseKey,
      title: 'Welcome',
      description: 'This is a showcase view example.',
      overlayColor: Colors.black.withOpacity(0.5),
      textColor: Colors.white,
      titleTextStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
      descriptionTextStyle: TextStyle(fontSize: 16),
      child: ElevatedButton(
        onPressed: () {
          // 按钮点击事件
        },
        child: Text('Click Me'),
      ),
    );
    

高级用法

  • 多步骤引导:你可以通过传递多个 GlobalKeystartShowCase 方法来实现多步骤引导。

    ShowCaseWidget.of(context).startShowCase([_showcaseKey1, _showcaseKey2, _showcaseKey3]);
    
  • 自定义动画:你可以通过 Showcase 组件的 animationDurationanimationCurve 参数来自定义引导视图的动画效果。

    Showcase(
      key: _showcaseKey,
      title: 'Welcome',
      description: 'This is a showcase view example.',
      animationDuration: Duration(milliseconds: 500),
      animationCurve: Curves.easeInOut,
      child: ElevatedButton(
        onPressed: () {
          // 按钮点击事件
        },
        child: Text('Click Me'),
      ),
    );
回到顶部