Flutter交互管理插件interaction_manager的使用

Flutter交互管理插件interaction_manager的使用

interaction_manager

interaction_manager 是一个用于管理 Flutter 应用程序中各种交互对话框的插件。它允许开发者轻松地注册和调用不同类型的对话框,例如自定义对话框、消息对话框、查询对话框等。

Getting Started(开始使用)

此项目是一个 Dart 包项目的起点,可以作为一个库模块,包含可以在多个 Flutter 或 Dart 项目中轻松共享的代码。

对于如何开始使用 Flutter,您可以查看我们的在线文档,其中包含教程、示例、移动开发指南以及完整的 API 参考。


使用示例

以下是一个完整的示例,展示如何在 Flutter 应用程序中使用 interaction_manager 插件。

示例代码

import 'package:example/dialog_builders.dart'; // 自定义对话框构建器
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:interaction_manager/interaction_manager.dart'; // 引入 interaction_manager

void main() => runApp(MyApp()); // 应用程序入口

// 主应用程序类
class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: InteractionConnector( // 使用 InteractionConnector 来连接交互管理器
        dialogsInitFunction: registerDialogs, // 注册对话框
        child: MyHomePage(), // 主页面
      ),
    );
  }
}

// 主页面类
class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('InteractionManager')), // 设置应用栏标题
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 自定义对话框按钮
            MaterialButton(
              onPressed: () async {
                await GetIt.I<InteractionManager>() // 获取 InteractionManager 实例
                    .showRegisteredDialog<Map<String, String>, void>(
                  dialogName: 'TestDialog', // 对话框名称
                  data: { // 传递数据
                    'title': 'This is the Title',
                    'message': 'Registered Dialog',
                    'buttonText': 'OK',
                  },
                );
              },
              child: const Text('Registered CustomDialog:'), // 按钮文本
            ),
            // 消息对话框按钮
            MaterialButton(
              onPressed: () async {
                await GetIt.I<InteractionManager>().showMessageDialog(
                    'This is a message!', // 消息内容
                    title: 'Message Dialog', // 对话框标题
                    closeButtonText: 'OK'); // 关闭按钮文本
              },
              child: const Text('Message Dialog:'), // 按钮文本
            ),
            // 查询对话框按钮
            MaterialButton(
              onPressed: () async {
                MessageDialogResults result = await GetIt.I<InteractionManager>().showQueryDialog(
                  'This is a query dialog!', // 消息内容
                  title: 'Query Dialog', // 对话框标题
                );
                print(result); // 打印结果
              },
              child: const Text('Query Dialog:'), // 按钮文本
            ),
            // 网络配置对话框按钮
            MaterialButton(
              onPressed: () async {
                await GetIt.I<InteractionManager>().showNetworkConfigurationDialog(
                  title: 'Network Dialog', // 对话框标题
                  message: 'This is a message!', // 消息内容
                  okButtonText: 'OK', // 确定按钮文本
                );
              },
              child: const Text('Network Dialog:'), // 按钮文本
            ),
            // 登录对话框按钮
            MaterialButton(
              onPressed: () async {
                await GetIt.I<InteractionManager>().showLoginDialog(
                    okButtonText: 'OK', // 确定按钮文本
                    usernameValidator: (s) { // 验证用户名
                      return s.isEmpty ? 'You have to provide a name' : null;
                    });
              },
              child: const Text('Login Dialog:'), // 按钮文本
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter交互管理插件interaction_manager的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


interaction_manager 是 Flutter 中的一个插件,用于管理和控制用户界面的交互行为,例如防止用户在加载数据或执行耗时操作时进行不必要的交互。通过使用 interaction_manager,你可以在特定操作期间禁用用户交互,并在操作完成后重新启用交互。

安装

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

dependencies:
  flutter:
    sdk: flutter
  interaction_manager: ^1.0.0  # 请检查最新版本

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

基本用法

interaction_manager 提供了一个 InteractionManager 类,你可以使用它来管理用户交互。以下是一个简单的示例:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: InteractionManager(
        child: HomeScreen(),
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final interactionManager = InteractionManager.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Interaction Manager Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () async {
                // 禁用用户交互
                interactionManager.disable();

                // 模拟一个耗时操作
                await Future.delayed(Duration(seconds: 2));

                // 启用用户交互
                interactionManager.enable();
              },
              child: Text('Start Task'),
            ),
            SizedBox(height: 20),
            Text('Press the button to start a task.'),
          ],
        ),
      ),
    );
  }
}

解释

  1. InteractionManager: 这是一个包裹在 MaterialAppCupertinoApp 外层的 Widget,用于管理整个应用的交互状态。

  2. InteractionManager.of(context): 通过 InteractionManager.of(context) 获取当前上下文中的 InteractionManager 实例。

  3. disable(): 调用 disable() 方法可以禁用用户交互。

  4. enable(): 调用 enable() 方法可以重新启用用户交互。

其他功能

interaction_manager 还提供了一些其他功能,例如:

  • isInteractionEnabled: 一个布尔值,表示当前是否允许用户交互。
  • onInteractionChanged: 一个回调函数,当交互状态发生变化时触发。

示例

interactionManager.onInteractionChanged = (bool enabled) {
  print('Interaction is ${enabled ? 'enabled' : 'disabled'}');
};
回到顶部