netease_roomkit 0.0.1-rc.2 copy "netease_roomkit: ^0.0.1-rc.2" to clipboard
netease_roomkit: ^0.0.1-rc.2 copied to clipboard

outdated

A Flutter package for NetEase RoomKit.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.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 =
      'Basic MDEwNmFiZDViMWVkNDU5MjlhNTYyZjkxOTA3MWFiNDY6ZGE3Njc0YzgxNDdhNGRjMDg3ODEyZmM1NDQyOTM5YmU=';
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    NERoomKit.instance
        .initialize(
      NERoomKitOptions(
        appKey: 'ed73b42de1b66244337a4cd54a9534cc',
        extras: {'serverUrl': 'test'},
      ),
    )
        .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'),
                              ),
                            );
                          }
                          NERoomKit.instance.authService
                              .login(
                            account,
                            token,
                          )
                              .then((value) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                content: Text('login result: $value'),
                              ),
                            );
                            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: [
                          buildAnonymousLogin(),
                          const SizedBox(
                            height: 10,
                          ),
                          buildLogout(),
                          const SizedBox(
                            height: 10,
                          ),
                          buildLoginListener(),
                          const SizedBox(
                            height: 10,
                          ),
                        ],
                      ),
                      ElevatedButton(
                        child: const Text('下一页'),
                        onPressed: () {
                         if(NERoomKit.instance.authService.isLoggedIn){
                           Navigator.of(context)
                               .push(MaterialPageRoute(builder: (context) {
                             return const CreateRoomPage();
                           }));
                         }else{
                           ScaffoldMessenger.of(context).showSnackBar(
                             const SnackBar(
                               content: Text('请先登录'),
                             ),
                           );
                         }
                        },
                      )
                    ],
                  ),
                ),
              ),
            )));
  }

  /// 匿名登陆
  Widget buildAnonymousLogin() {
    return ElevatedButton(
      child: const Text('匿名登陆'),
      onPressed: () {
        NERoomKit.instance.authService
            .anonymousLogin(
          _controller.text,
        )
            .then((value) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('login result: $value'),
            ),
          );
        });
      },
    );
  }

  /// 登出
  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.onAuthStatusChanged.listen((value) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('状态 NEAuthEvent result: ${value.index}'),
            ),
          );
        });
      },
    );
  }
}