string_uid.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:developer';
  2. import 'package:agora_rtc_engine/rtc_engine.dart';
  3. import 'package:agora_rtc_engine_example/config/agora.config.dart' as config;
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:permission_handler/permission_handler.dart';
  8. /// MultiChannel Example
  9. class StringUid extends StatefulWidget {
  10. @override
  11. State<StatefulWidget> createState() => _State();
  12. }
  13. class _State extends State<StringUid> {
  14. late final RtcEngine _engine;
  15. String channelId = config.channelId;// 频道id
  16. String stringUid = config.stringUid;// 用户id
  17. bool isJoined = false; // user 是否加入 频道
  18. TextEditingController? _controller0, _controller1;
  19. @override
  20. void initState() {
  21. super.initState();
  22. _controller0 = TextEditingController(text: channelId);
  23. _controller1 = TextEditingController(text: stringUid);
  24. this._initEngine();
  25. }
  26. @override
  27. void dispose() {
  28. super.dispose();
  29. _engine.destroy();
  30. }
  31. /// 初始化IM
  32. _initEngine() async {
  33. _engine = await RtcEngine.createWithContext(RtcEngineContext(config.appId));
  34. this._addListeners();
  35. await _engine.setChannelProfile(ChannelProfile.LiveBroadcasting);
  36. await _engine.setClientRole(ClientRole.Broadcaster);
  37. }
  38. _addListeners() {
  39. _engine.setEventHandler(RtcEngineEventHandler(
  40. joinChannelSuccess: (channel, uid, elapsed) {
  41. log('joinChannelSuccess ${channel} ${uid} ${elapsed}');
  42. setState(() {
  43. isJoined = true;
  44. });
  45. },
  46. leaveChannel: (stats) {
  47. log('leaveChannel ${stats.toJson()}');
  48. setState(() {
  49. isJoined = false;
  50. });
  51. },
  52. ));
  53. }
  54. _joinChannel() async {
  55. if (defaultTargetPlatform == TargetPlatform.android) {
  56. await Permission.microphone.request();
  57. }
  58. await _engine.joinChannelWithUserAccount(
  59. config.token, channelId, stringUid);
  60. }
  61. _leaveChannel() async {
  62. await _engine.leaveChannel();
  63. }
  64. /// 获取当前用户信息
  65. _getUserInfo() {
  66. _engine.getUserInfoByUserAccount(stringUid).then((userInfo) {
  67. log('getUserInfoByUserAccount ${userInfo.toJson()}');
  68. ScaffoldMessenger.of(context).showSnackBar(SnackBar(
  69. content: Text('${userInfo.toJson()}'),
  70. ));
  71. }).catchError((err) {
  72. log('getUserInfoByUserAccount ${err}');
  73. });
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. return Stack(
  78. children: [
  79. Column(
  80. children: [
  81. TextField(
  82. controller: _controller0,
  83. decoration: InputDecoration(hintText: '聊天室'),
  84. onChanged: (text) {
  85. setState(() {
  86. channelId = text;
  87. });
  88. },
  89. ),
  90. TextField(
  91. controller: _controller1,
  92. decoration: InputDecoration(hintText: '用户名'),
  93. onChanged: (text) {
  94. setState(() {
  95. stringUid = text;
  96. });
  97. },
  98. ),
  99. Row(
  100. children: [
  101. Expanded(
  102. flex: 1,
  103. child: ElevatedButton(
  104. onPressed:
  105. isJoined ? this._leaveChannel : this._joinChannel,
  106. child: Text('${isJoined ? 'Leave' : 'Join'} channel'),
  107. ),
  108. )
  109. ],
  110. ),
  111. ],
  112. ),
  113. Align(
  114. alignment: Alignment.bottomRight,
  115. child: Column(
  116. mainAxisSize: MainAxisSize.min,
  117. children: [
  118. ElevatedButton(
  119. onPressed: this._getUserInfo,
  120. child: Text('Get userInfo'),
  121. ),
  122. ],
  123. ),
  124. )
  125. ],
  126. );
  127. }
  128. }