• Flutter高仿微信-第51篇-群聊-修改群名


     Flutter高仿微信系列共59篇,从Flutter客户端、Kotlin客户端、Web服务器、数据库表结构、Xmpp即时通讯服务器、视频通话服务器、腾讯云服务器全面讲解。

     详情请查看

    效果图:

    实现代码:

    //修改群名
    void _updateGroupName(){
      bool isOwner = false;
      for(int i = 0; i< _groupUserList.length; i++){
        GroupUserBean groupUserBean = _groupUserList[i];
        if(account == groupUserBean.account &&
            (groupUserBean.accountType == GroupUserBean.ACCOUNT_TYPE_OWNER||
                groupUserBean.accountType == GroupUserBean.ACCOUNT_TYPE_ADMIN)){
          isOwner = true;
          break;
        }
      }
      if(isOwner){
        Navigator.push(context, MaterialPageRoute(builder: (context) => GroupUpdateName(groupId: widget.groupId)));
      } else {
        LoadingDialogUtils.showTipDialog(context, msg : "当前群聊仅群主/群管理员可以修改群聊名称");
      }
    
    }

    /**
     * Author : wangning
     * Email : maoning20080809@163.com
     * Date : 2022/11/15 14:13
     * Description : 修改群名
     */
    
    class GroupUpdateName extends StatefulWidget {
    
      String groupId;
    
      GroupUpdateName({required this.groupId});
    
      @override
      _GroupUpdateNameState createState() => _GroupUpdateNameState();
    
    }
    
    class _GroupUpdateNameState extends State {
    
      TextEditingController? _groupNameController;
    
      //修改群名
      void _processUpdateName(String groupName) async {
        bool isNetwork = await CommonNetwork.isNetwork();
        if(!isNetwork) {
          CommonUtils.showNetworkError(context);
          return;
        }
    
        if(_groupBean!= null){
          _groupBean?.groupName = groupName;
          bool isSuccess = await GroupRepository.getInstance().updateGroupServer(_groupBean!);
          if(isSuccess){
            CommonToast.show(context, "修改成功!");
            GroupRepository.getInstance().updateGroup(_groupBean!);
            eventBus.emit(BaseEvent(BaseEvent.TYPE_REFRESH_GROUP, result: HashMap()));
            Navigator.pop(context);
          } else {
            CommonToast.show(context, "修改失败!");
          }
        }
    
      }
    
    
      GroupBean? _groupBean;
    
      //初始化群名
      void _initGroup() async{
        _groupBean = await GroupRepository.getInstance().findGroupByGroupId(widget.groupId);
        _groupNameController = TextEditingController(text: "${_groupBean?.groupName}");
        setState(() {
        });
    
      }
    
      @override
      void initState() {
        super.initState();
        _initGroup();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: WnAppBar.getAppBar(context, Text("修改群名")),
          body: Container(
            height: 200,
            child: Column(
              children: [
                SizedBox(height: 30,),
                Expanded(
                    child: Container(
                      margin: EdgeInsets.only(left: 12, right: 12),
                      width: double.infinity,
                      child: Row(
                        children: [
                          Text("群名:"),
                          Expanded(
                            child:customTextField(defaultValue: '${_groupBean?.groupName}',textEditController: _groupNameController,),
                          ),
                        ],
                      ),
                    )
                ),
                SizedBox(height: 30,),
                Container(
                  alignment: AlignmentDirectional.center,
                  child: _updateNameWidget(),
                ),
    
              ],
            ),
          ),
        );
      }
    
      Widget customTextField({
        TextEditingController? textEditController,
        String? hintText,
        String? defaultValue,
      }) {
        return TextField(
          controller: textEditController,
          cursorColor: Colors.black,
          decoration: InputDecoration(
            //labelText: defaultValue,
            //hintText: hintText,
            hintStyle: TextStyle(
              fontSize: 16,
              color: Colors.grey.withOpacity(0.8),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: const BorderSide(color: Colors.black),
              borderRadius: BorderRadius.circular(5.0),
            ),
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(5.0),
              borderSide: BorderSide(
                color: Colors.grey,
              ),
            ),
          ),
          style: TextStyle(
            fontSize: 16,
            color: Colors.black,
            fontWeight: FontWeight.w500,
          ),
        );
      }
    
      Widget _updateNameWidget() {
        return MaterialButton(
          color: Colors.blue,
          textColor: Colors.white,
          padding: EdgeInsets.only(left: 28, top: 8, right: 28, bottom: 8),
          child: Text('修改', style: TextStyle(fontSize: 16),),
          onPressed: () {
            var nickName = _groupNameController?.text??"";
            _processUpdateName(nickName);
          },
        );
      }
    }

  • 相关阅读:
    js字符串的删除和修改
    智慧排水监测系统:实时监测城市排水情况
    Android开发之NDK 编译Pjsip
    超详细-Vivado配置Sublime+Sublime实现VHDL语法实时检查
    Python——requests模块全解
    自学 Java 需要具备哪些基本条件或技能?
    Python 基础变量声明
    vue路径中“@/“代表什么
    《向量数据库指南》——提高向量数据库Milvus Cloud 2.3的运行效率
    Chaos Mesh网络延迟原理探索
  • 原文地址:https://blog.csdn.net/maoning20080808/article/details/128049328