Flutter教程引导插件tutorial_plus的使用

Flutter教程引导插件tutorial_plus的使用

本README描述了该包的功能。如果你将此包发布到pub.dev,此README的内容将会出现在你包的首页。

对于如何编写一个好的包README的更多信息,请参阅编写包页面指南

对于开发包的一般信息,请参阅Dart关于创建库包的指南和Flutter关于开发包和插件的指南。

使用

示例代码

以下是一个完整的示例,展示了如何使用tutorial_plus插件来实现教程引导功能。

示例代码

import 'package:flutter/material.dart';
import 'package:tutorial_plus/tutorial.dart';
import 'package:tutorial_plus/tutorial_controller.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // 这个小部件是你的应用的根。
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  late TutorialController controllers;

  void _incrementCounter() {
    controllers.show(); // 显示教程步骤
  }

  @override
  Widget build(BuildContext context) {
    return Tutorials.builder(
      onStatusEnd: (controller) {}, // 教程结束时的回调
      customWidgetMessage: (controller) {
        return Text(controller.message ?? ''); // 自定义消息显示
      },
      onStatusChanged: (controller) {
        controllers = controller; // 获取教程控制器
      },
      builder: (context, controller) {
        return Scaffold(
          body: Padding(
            padding: const EdgeInsets.only(top: 25.0),
            child: SingleChildScrollView(
              controller: controller.scrollController, // 滚动控制器
              child: Column(
                children: [
                  Container(
                    height: 30,
                    width: MediaQuery.of(context).size.width,
                    color: Colors.black,
                  ),
                  TutorialStep(
                    controller: controller.apply( // 第三个教程步骤
                      message: 'ini tutorial ke 3', // 提示信息
                      id: '3', // 步骤ID
                      isActive: true, // 是否激活
                      order: 3, // 步骤顺序
                      messagePadding: 30, // 提示信息内边距
                      scrollPadding: 100, // 滚动区域填充
                      isFloatings: false, // 是否浮动
                    ),
                    child: Container(
                      height: 200,
                      width: MediaQuery.of(context).size.width,
                      color: Colors.pink,
                    ),
                  ),
                  Container(
                    height: 500,
                    width: MediaQuery.of(context).size.width,
                    color: Colors.blue,
                  ),
                  TutorialStep(
                    controller: controller.apply( // 第一个教程步骤
                      message: 'ini tutorial ke 1',
                      id: '11',
                      isActive: true,
                      order: 1,
                      messagePadding: 30,
                      scrollPadding: 100,
                      isFloatings: false,
                    ),
                    child: Container(
                      height: 200,
                      width: MediaQuery.of(context).size.width,
                      color: Colors.green,
                    ),
                  ),
                ],
              ),
            ),
          ),
          floatingActionButton: TutorialStep(
            controller: controller.apply( // 第二个教程步骤
              message: 'ini tutorial ke 2',
              id: '1',
              isActive: true,
              order: 2,
              messagePadding: 30,
              scrollPadding: 100,
            ),
            child: FloatingActionButton(
              onPressed: _incrementCounter,
              tooltip: 'Increment',
              child: const Icon(Icons.add),
            ),
          ),
        );
      },
    );
  }
}

代码说明

  1. 导入必要的包

    import 'package:flutter/material.dart';
    import 'package:tutorial_plus/tutorial.dart';
    import 'package:tutorial_plus/tutorial_controller.dart';
    
  2. 初始化教程控制器

    late TutorialController controllers;
    
  3. 定义教程步骤

    TutorialStep(
      controller: controller.apply(
        message: 'ini tutorial ke 3',
        id: '3',
        isActive: true,
        order: 3,
        messagePadding: 30,
        scrollPadding: 100,
        isFloatings: false,
      ),
      child: Container(
        height: 200,
        width: MediaQuery.of(context).size.width,
        color: Colors.pink,
      ),
    )
    
  4. 触发教程显示

    void _incrementCounter() {
      controllers.show();
    }
    
  5. 构建教程界面

    return Tutorials.builder(
      onStatusEnd: (controller) {},
      customWidgetMessage: (controller) {
        return Text(controller.message ?? '');
      },
      onStatusChanged: (controller) {
        controllers = controller;
      },
      builder: (context, controller) {
        return Scaffold(
          body: Padding(
            padding: const EdgeInsets.only(top: 25.0),
            child: SingleChildScrollView(
              controller: controller.scrollController,
              child: Column(
                children: [
                  Container(
                    height: 30,
                    width: MediaQuery.of(context).size.width,
                    color: Colors.black,
                  ),
                  ...
                ],
              ),
            ),
          ),
          floatingActionButton: TutorialStep(
            controller: controller.apply(
              message: 'ini tutorial ke 2',
              id: '1',
              isActive: true,
              order: 2,
              messagePadding: 30,
              scrollPadding: 100,
            ),
            child: FloatingActionButton(
              onPressed: _incrementCounter,
              tooltip: 'Increment',
              child: const Icon(Icons.add),
            ),
          ),
        );
      },
    );
    

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

1 回复

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


tutorial_plus 是一个用于在 Flutter 应用中创建引导教程的插件。它可以帮助你轻松地在应用中添加步骤引导,帮助用户了解如何使用应用的不同功能。以下是如何使用 tutorial_plus 插件的详细步骤。

1. 添加依赖

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

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

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

2. 导入包

在你的 Dart 文件中导入 tutorial_plus 包:

import 'package:tutorial_plus/tutorial_plus.dart';

3. 创建引导步骤

tutorial_plus 使用 TutorialStep 来定义每个引导步骤。每个步骤需要指定目标组件、描述文本以及其他可选参数。

List<TutorialStep> tutorialSteps = [
  TutorialStep(
    target: GlobalKey(), // 目标组件的 GlobalKey
    description: "This is the first step of the tutorial.",
    shape: ShapeLightFocus.RRect, // 高亮形状
    radius: 8.0, // 圆角半径
    padding: 8.0, // 内边距
  ),
  TutorialStep(
    target: GlobalKey(),
    description: "This is the second step of the tutorial.",
    shape: ShapeLightFocus.Circle, // 圆形高亮
  ),
];

4. 创建 TutorialPlus 实例

在你的 widget 树中,使用 TutorialPlus 包裹需要引导的组件,并传入引导步骤。

class MyApp extends StatelessWidget {
  final GlobalKey _firstKey = GlobalKey();
  final GlobalKey _secondKey = GlobalKey();

  [@override](/user/override)
  Widget build(BuildContext context) {
    List<TutorialStep> tutorialSteps = [
      TutorialStep(
        target: _firstKey,
        description: "This is the first step of the tutorial.",
        shape: ShapeLightFocus.RRect,
        radius: 8.0,
        padding: 8.0,
      ),
      TutorialStep(
        target: _secondKey,
        description: "This is the second step of the tutorial.",
        shape: ShapeLightFocus.Circle,
      ),
    ];

    return MaterialApp(
      home: TutorialPlus(
        steps: tutorialSteps,
        child: Scaffold(
          appBar: AppBar(
            title: Text('Tutorial Plus Example'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  key: _firstKey,
                  onPressed: () {},
                  child: Text('Button 1'),
                ),
                SizedBox(height: 20),
                ElevatedButton(
                  key: _secondKey,
                  onPressed: () {},
                  child: Text('Button 2'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

5. 启动引导

你可以通过调用 TutorialPlus.of(context).startTutorial() 来启动引导。

ElevatedButton(
  onPressed: () {
    TutorialPlus.of(context).startTutorial();
  },
  child: Text('Start Tutorial'),
);

6. 自定义引导样式

你可以通过 TutorialPlustutorialOptions 参数来自定义引导的样式,例如背景颜色、文本样式等。

TutorialPlus(
  steps: tutorialSteps,
  tutorialOptions: TutorialOptions(
    backgroundColor: Colors.black.withOpacity(0.7),
    textStyle: TextStyle(color: Colors.white, fontSize: 16),
  ),
  child: Scaffold(
    // Your widget tree
  ),
);

7. 处理引导完成事件

你可以通过 onFinish 回调来处理引导完成事件。

TutorialPlus(
  steps: tutorialSteps,
  onFinish: () {
    print("Tutorial finished!");
  },
  child: Scaffold(
    // Your widget tree
  ),
);

8. 处理引导步骤切换事件

你可以通过 onStepChange 回调来处理引导步骤切换事件。

TutorialPlus(
  steps: tutorialSteps,
  onStepChange: (int stepIndex) {
    print("Current step: $stepIndex");
  },
  child: Scaffold(
    // Your widget tree
  ),
);

9. 完整示例

以下是一个完整的示例,展示了如何使用 tutorial_plus 插件:

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

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

class MyApp extends StatelessWidget {
  final GlobalKey _firstKey = GlobalKey();
  final GlobalKey _secondKey = GlobalKey();

  [@override](/user/override)
  Widget build(BuildContext context) {
    List<TutorialStep> tutorialSteps = [
      TutorialStep(
        target: _firstKey,
        description: "This is the first step of the tutorial.",
        shape: ShapeLightFocus.RRect,
        radius: 8.0,
        padding: 8.0,
      ),
      TutorialStep(
        target: _secondKey,
        description: "This is the second step of the tutorial.",
        shape: ShapeLightFocus.Circle,
      ),
    ];

    return MaterialApp(
      home: TutorialPlus(
        steps: tutorialSteps,
        tutorialOptions: TutorialOptions(
          backgroundColor: Colors.black.withOpacity(0.7),
          textStyle: TextStyle(color: Colors.white, fontSize: 16),
        ),
        onFinish: () {
          print("Tutorial finished!");
        },
        onStepChange: (int stepIndex) {
          print("Current step: $stepIndex");
        },
        child: Scaffold(
          appBar: AppBar(
            title: Text('Tutorial Plus Example'),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  key: _firstKey,
                  onPressed: () {},
                  child: Text('Button 1'),
                ),
                SizedBox(height: 20),
                ElevatedButton(
                  key: _secondKey,
                  onPressed: () {},
                  child: Text('Button 2'),
                ),
                SizedBox(height: 20),
                ElevatedButton(
                  onPressed: () {
                    TutorialPlus.of(context).startTutorial();
                  },
                  child: Text('Start Tutorial'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
回到顶部