Flutter白板功能插件whiteboard_sdk_flutter的使用

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

Flutter白板功能插件whiteboard_sdk_flutter的使用

1. 简介

whiteboard_sdk_flutter 是一个用于在Flutter应用中集成Agora白板功能的插件。通过该插件,开发者可以轻松地在应用中实现实时白板和回放功能。

2. 安装

2.1 添加依赖

pubspec.yaml 文件中添加 whiteboard_sdk_flutter 依赖:

dependencies:
  whiteboard_sdk_flutter: ^0.5.4
2.2 Android配置

确保在 AndroidManifest.xml 文件中添加 INTERNET 权限:

<uses-permission android:name="android.permission.INTERNET"/>

3. 使用示例

3.1 实时房间 (Live Room)

以下是创建并加入一个实时白板房间的完整示例代码:

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

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

class _WhiteboardRoomPageState extends State<WhiteboardRoomPage> {
  WhiteSdk? whiteSdk;
  WhiteRoom? whiteRoom;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('实时白板'),
      ),
      body: WhiteboardView(
        options: WhiteOptions(
          appIdentifier: 'YOUR_APP_ID', // 替换为你的App ID
          log: true,
        ),
        onSdkCreated: (sdk) async {
          // 初始化SDK并加入房间
          var room = await sdk.joinRoom(
            options: RoomOptions(
              uuid: 'ROOM_UUID', // 替换为你的房间UUID
              roomToken: 'ROOM_TOKEN', // 替换为你的房间Token
              uid: 'UNIQUE_CLIENT_ID', // 替换为唯一的客户端ID
              isWritable: true, // 是否允许写入
            ),
          );

          setState(() {
            whiteSdk = sdk;
            whiteRoom = room;
          });
        },
      ),
    );
  }
}
3.2 回放 (Replay)

以下是创建并加入一个白板回放的完整示例代码:

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

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

class _WhiteboardReplayPageState extends State<WhiteboardReplayPage> {
  WhiteSdk? whiteSdk;
  WhiteReplay? whiteReplay;

  void _onPlayerStateChanged(WhitePlayerState state) {
    print('Player state changed to: $state');
  }

  void _onPlayerPhaseChanged(WhitePlayerPhase phase) {
    print('Player phase changed to: $phase');
  }

  void _onScheduleTimeChanged(int time) {
    print('Schedule time changed to: $time');
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('白板回放'),
      ),
      body: WhiteboardView(
        options: WhiteOptions(
          appIdentifier: 'YOUR_APP_ID', // 替换为你的App ID
          log: true,
          backgroundColor: Color(0xFFF9F4E7), // 设置背景颜色
        ),
        onSdkCreated: (sdk) async {
          // 初始化SDK并加入回放
          var replay = await sdk.joinReplay(
            options: ReplayOptions(
              room: 'ROOM_UUID', // 替换为你的房间UUID
              roomToken: 'ROOM_TOKEN', // 替换为你的房间Token
            ),
            onPlayerStateChanged: _onPlayerStateChanged,
            onPlayerPhaseChanged: _onPlayerPhaseChanged,
            onScheduleTimeChanged: _onScheduleTimeChanged,
          );

          setState(() {
            whiteSdk = sdk;
            whiteReplay = replay;
          });
        },
      ),
    );
  }
}

4. 完整示例项目

以下是一个完整的示例项目结构,包含实时房间和回放功能:

import 'package:flutter/material.dart';
import 'package:whiteboard_sdk_flutter_example/white_example_page.dart';
import 'package:whiteboard_sdk_flutter_example/window_test_page.dart';
import 'package:whiteboard_sdk_flutter_example/replay_test_page.dart';
import 'package:whiteboard_sdk_flutter_example/room_test_page.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Whiteboard',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'White Example'),
    );
  }
}

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

  final String title;

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

class MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(
                width: 200,
                height: 50,
                child: ElevatedButton(
                  onPressed: () => _pushPage(context, allPages[0]),
                  child: Text("Room"),
                )),
            SizedBox(height: 40),
            SizedBox(
                width: 200,
                height: 50,
                child: ElevatedButton(
                  onPressed: () => _pushPage(context, allPages[1]),
                  child: Text("Replay"),
                )),
            SizedBox(height: 40),
            SizedBox(
                width: 200,
                height: 50,
                child: ElevatedButton(
                  onPressed: () => _pushPage(context, allPages[2]),
                  child: Text("Window"),
                )),
          ],
        ),
      ),
    );
  }

  void _pushPage(BuildContext context, WhiteExamplePage page) {
    Navigator.of(context).push(MaterialPageRoute<void>(
        builder: (_) => Scaffold(
              appBar: AppBar(title: Text(page.title)),
              body: page,
            )));
  }
}

final List<WhiteExamplePage> allPages = <WhiteExamplePage>[
  RoomTestPage(),
  ReplayTestPage(),
  WindowTestPage(),
];

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

1 回复

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


当然,下面是一个关于如何在Flutter应用中使用whiteboard_sdk_flutter插件来实现白板功能的示例代码。假设你已经在pubspec.yaml文件中添加了whiteboard_sdk_flutter依赖并运行了flutter pub get

pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  whiteboard_sdk_flutter: ^最新版本号  # 请替换为实际的最新版本号

main.dart

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

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

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

class WhiteboardScreen extends StatefulWidget {
  @override
  _WhiteboardScreenState createState() => _WhiteboardScreenState();
}

class _WhiteboardScreenState extends State<WhiteboardScreen> {
  late WhiteboardController _whiteboardController;

  @override
  void initState() {
    super.initState();
    // 初始化WhiteboardController
    _whiteboardController = WhiteboardController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Whiteboard Demo'),
      ),
      body: Center(
        child: WhiteboardView(
          controller: _whiteboardController,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // 打开画笔工具
          _whiteboardController.openPenTool();
        },
        tooltip: 'Open Pen Tool',
        child: Icon(Icons.edit),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomAppBar(
        shape: CircularNotchedRectangle(),
        notchMargin: 4.0,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.clear),
              onPressed: () {
                // 清空白板内容
                _whiteboardController.clearBoard();
              },
            ),
            IconButton(
              icon: Icon(Icons.save),
              onPressed: () async {
                // 保存白板内容(这里假设保存为图片)
                Uint8List? imageBytes = await _whiteboardController.saveAsImage();
                if (imageBytes != null) {
                  // 这里可以处理保存逻辑,比如保存到本地或上传到服务器
                  print('Image saved successfully');
                }
              },
            ),
          ],
        ),
      ),
    );
  }

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

注意事项

  1. 依赖版本:确保在pubspec.yaml中使用的whiteboard_sdk_flutter版本是最新的,或者至少是兼容你当前Flutter SDK的版本。

  2. 权限:如果插件需要访问存储权限以保存白板内容(如图片),请确保在AndroidManifest.xmlInfo.plist中声明了必要的权限。

  3. 错误处理:在实际应用中,建议添加错误处理逻辑,例如处理保存失败的情况。

  4. 自定义功能whiteboard_sdk_flutter插件可能提供了更多的功能和配置选项,请参考官方文档进行进一步的开发和自定义。

这个示例展示了如何使用whiteboard_sdk_flutter插件在Flutter应用中创建一个基本的白板界面,包括打开画笔工具、清空白板和保存白板内容的功能。根据你的需求,你可以进一步扩展和自定义这个基础示例。

回到顶部