netease_roomkit 1.30.1 copy "netease_roomkit: ^1.30.1" to clipboard
netease_roomkit: ^1.30.1 copied to clipboard

A Flutter package for NetEase RoomKit.

example/lib/main.dart

// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:roomkit_example/create_room_page.dart';
import 'package:netease_roomkit/netease_roomkit.dart';

void main() {
  runApp(Builder(builder: (context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('RoomKit-Sample'),
        ),
        body: const MyApp(),
      ),
    );
  }));
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _initializeResult = 'Unknown';
  String account = '';
  String token = '';
  String anonymousArgs = '';
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    NERoomKit.instance
        .initialize(
      NERoomKitOptions(
        appKey: 'Your_App_Key',
      ),
    )
        .then((value) {
      print("initialize roomkit: $value");
      setState(() {
        _initializeResult = value.toString();
      });
    });
    _controller = TextEditingController(text: anonymousArgs);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(primarySwatch: Colors.blue),
        home: Scaffold(
            resizeToAvoidBottomInset: false,
            body: GestureDetector(
              onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
              child: SingleChildScrollView(
                child: Container(
                  padding: const EdgeInsets.only(left: 16, right: 16),
                  child: Column(
                    children: [
                      Center(
                        child: Text(
                            'roomkit initialize result: $_initializeResult\n'),
                      ),
                      TextField(
                        autofocus: true,
                        decoration: const InputDecoration(
                          labelText: "Account",
                        ),
                        onChanged: (value) {
                          account = value;
                        },
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      TextField(
                        autofocus: true,
                        decoration: const InputDecoration(
                          labelText: "Token",
                        ),
                        onChanged: (value) {
                          token = value;
                        },
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      ElevatedButton(
                        child: const Text('登录'),
                        onPressed: () {
                          if (account.isEmpty || token.isEmpty) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              const SnackBar(
                                content: Text('请输入账号和token'),
                              ),
                            );
                            return;
                          }
                          NERoomKit.instance.authService
                              .login(
                            account,
                            token,
                          )
                              .then((value) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                content: Text('login result: $value'),
                              ),
                            );
                            if (!value.isSuccess()) return;
                            Navigator.of(context)
                                .push(MaterialPageRoute(builder: (context) {
                              return const CreateRoomPage();
                            }));
                          });
                        },
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      const Text('下方输入框给匿名登录使用'),
                      TextField(
                        decoration: const InputDecoration(
                          labelText: "Token",
                        ),
                        keyboardType: TextInputType.multiline,
                        maxLines: 4,
                        onChanged: (value) {
                          anonymousArgs = value;
                        },
                        controller: _controller,
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          buildLogout(),
                          const SizedBox(
                            height: 10,
                          ),
                          buildLoginListener(),
                          const SizedBox(
                            height: 10,
                          ),
                        ],
                      ),
                      ElevatedButton(
                        child: const Text('下一页'),
                        onPressed: () async {
                          if (await NERoomKit.instance.authService.isLoggedIn) {
                            Navigator.of(context)
                                .push(MaterialPageRoute(builder: (context) {
                              return const CreateRoomPage();
                            }));
                          } else {
                            ScaffoldMessenger.of(context).showSnackBar(
                              const SnackBar(
                                content: Text('请先登录'),
                              ),
                            );
                          }
                        },
                      )
                    ],
                  ),
                ),
              ),
            )));
  }

  /// 登出
  Widget buildLogout() {
    return ElevatedButton(
      child: const Text('登出'),
      onPressed: () {
        NERoomKit.instance.authService.logout().then((value) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('logout result: $value'),
            ),
          );
        });
      },
    );
  }

  /// 登出
  Widget buildLoginListener() {
    return ElevatedButton(
      child: const Text('添加登录监听'),
      onPressed: () {
        NERoomKit.instance.authService.onAuthEvent.listen((value) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('状态 NEAuthEvent result: ${value.index}'),
            ),
          );
        });
      },
    );
  }
}