Flutter实现直播间礼物收发

简介: 下面是一个简单的礼物发送系统的实现代码,包括支持连送和单次送等功能

下面是一个简单的礼物发送系统的实现代码,包括支持连送和单次送等功能:

import 'package:flutter/material.dart';
class Gift {
  final String name;
  final int count;
  Gift(this.name, this.count);
}
class GiftSendingScreen extends StatefulWidget {
  const GiftSendingScreen({Key? key}) : super(key: key);
  @override
  _GiftSendingScreenState createState() => _GiftSendingScreenState();
}
class _GiftSendingScreenState extends State<GiftSendingScreen> {
  List<Gift> _gifts = [
    Gift('Heart', 1),
    Gift('Rose', 1),
    Gift('Candy', 1),
    Gift('Teddy Bear', 5),
    Gift('Diamond Ring', 10),
  ];
  Gift? _selectedGift;
  int _sendingCount = 1;
  bool _isSendingContinuously = false;
  void _sendGift() {
    if (_selectedGift != null) {
      for (int i = 0; i < _sendingCount; i++) {
        // simulate sending gift to server
        print('Sending gift ${_selectedGift!.name}...');
      }
      if (_isSendingContinuously) {
        // continue sending gifts after a delay
        Future.delayed(Duration(seconds: 1), () {
          _sendGift();
        });
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Send Gift'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          DropdownButtonFormField<Gift>(
            decoration: InputDecoration(
              labelText: 'Select a gift',
            ),
            value: _selectedGift,
            onChanged: (gift) {
              setState(() {
                _selectedGift = gift;
              });
            },
            items: _gifts
                .map(
                  (gift) => DropdownMenuItem<Gift>(
                    value: gift,
                    child: Text('${gift.name} (${gift.count})'),
                  ),
                )
                .toList(),
          ),
          TextFormField(
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
              labelText: 'Sending count',
            ),
            initialValue: '1',
            onChanged: (value) {
              setState(() {
                _sendingCount = int.tryParse(value) ?? 0;
              });
            },
          ),
          Row(
            children: [
              Checkbox(
                value: _isSendingContinuously,
                onChanged: (value) {
                  setState(() {
                    _isSendingContinuously = value ?? false;
                  });
                },
              ),
              Text('Send continuously'),
            ],
          ),
          ElevatedButton(
            onPressed: _sendGift,
            child: Text('Send'),
          ),
        ],
      ),
    );
  }
}

在这个示例中,我们使用了 Flutter 的 DropdownButtonFormFieldTextFormField 等组件来获取用户选择的礼物和发送数量。我们还使用了 Checkbox 组件来允许用户选择是否连续发送礼物。在 _sendGift() 方法中,我们模拟将礼物发送到服务器,并且如果用户选择了连续发送,我们将延迟一秒钟后再次调用该方法以持续发送礼物。

以下是一个简单的收到礼物系统的实现代码,支持展示连送和单次送等数量:

import 'package:flutter/material.dart';
class ReceivedGift {
  final String name;
  final int count;
  ReceivedGift(this.name, this.count);
}
class GiftReceivingScreen extends StatefulWidget {
  const GiftReceivingScreen({Key? key}) : super(key: key);
  @override
  _GiftReceivingScreenState createState() => _GiftReceivingScreenState();
}
class _GiftReceivingScreenState extends State<GiftReceivingScreen> {
  List<ReceivedGift> _receivedGifts = [];
  void _receiveGift(ReceivedGift gift) {
    setState(() {
      // check if the same gift is already received
      var existingGift = _receivedGifts.firstWhere(
        (g) => g.name == gift.name,
        orElse: () => null,
      );
      if (existingGift != null) {
        // increment count of existing gift
        _receivedGifts[_receivedGifts.indexOf(existingGift)] =
            ReceivedGift(gift.name, existingGift.count + gift.count);
      } else {
        // add new gift to received gifts list
        _receivedGifts.add(gift);
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Received Gifts'),
      ),
      body: ListView.builder(
        itemCount: _receivedGifts.length,
        itemBuilder: (BuildContext context, int index) {
          var receivedGift = _receivedGifts[index];
          // build text based on number of gifts received
          var text = '${receivedGift.name} received';
          if (receivedGift.count > 1) {
            text += ' (${receivedGift.count}';
            text += receivedGift.count > 2 ? ' times)' : ' time)';
          }
          return ListTile(
            title: Text(text),
          );
        },
      ),
    );
  }
}

在这个示例中,我们使用了一个 _receivedGifts 列表来追踪用户收到的礼物。在 _receiveGift() 方法中,我们检查是否已经收到过相同的礼物,如果是,则增加现有礼物的数量;否则,将新礼物添加到列表中。

build() 方法中,我们使用 ListView.builder 来构建收到的礼物列表,并根据礼物数目构建展示文本。例如,如果用户收到多次相同礼物,则文本为“礼物名称接收(次数)”。

相关文章
|
11月前
|
编解码 JSON 网络协议
腾讯云直播开发日记(三) 聊天室-直播转码-连麦混流
腾讯云直播开发日记(三) 聊天室-直播转码-连麦混流
174 0
|
12月前
|
视频直播
视频直播源码技术知识分享:连麦功能(一)
我们开发视频直播平台就要去了解视频直播开发相关功能知识,这对我们开发平台有着重要的作用,连麦技术就是视频直播源码重要的技术功能之一,每一个功能技术都有自己的用武之地
视频直播源码技术知识分享:连麦功能(一)
直播源码搭建技术弹幕消息功能的实现
今天我要分享的这个直播源码技术功能也是大家非常常见的,这个功能不仅仅应用在直播源码平台中,在各大影视app中也一直被应用,那这个功能是什么那?
直播源码搭建技术弹幕消息功能的实现
请问 uniapp怎么接入直播互动消息
请问 uniapp怎么接入直播互动消息
332 2
请问 uniapp怎么接入直播互动消息
|
开发框架 开发工具
使用融云SDK在APICloud 平台实现单人多人音频通话
使用融云SDK在APICloud 平台实现单人多人音频通话,使用之前必须先获取 token、init、connect,同时需要到融云后台开通音视频通话功能(开通或者关闭 30 分钟后生效)。单人通话逻辑比较简单,主要会用到 didReceiveCall、didConnect、didDisconnect 等三个事件。
196 0
|
编解码
相亲app开发,关注延时问题优化连麦互动体验
相亲app开发,关注延时问题优化连麦互动体验
|
开发工具
使用融云SDK在APICloud平台实现单人多人音频通话
使用之前必须先获取token、init、connect,同时需要到融云后台开通音视频通话功能(开通或者关闭30分钟后生效)。单人通话逻辑比较简单,主要会用到didReceiveCall、didConnect、didDisconnect等三个事件。多人通话逻辑复杂一点,并且只能应用在群组或者讨论组,会用到didReceiveCall、didConnect、remoteUserDidJoin、remoteUserDidLeft、remoteUserDidInvite、didDisconnect等六个事件。
146 0
使用融云SDK在APICloud平台实现单人多人音频通话
Flutter:实现红包晃动效果
很多app打开会有红包悬浮在某个角落,然后为了吸引注意力,会将红包晃动起来,这个效果非常简单
294 0
|
编解码 UED
陪玩平台源码如何实现语音聊天室和连麦功能
陪玩平台源码的多人聊天室和直播功能中,都实现了语音聊天室功能,综合来看,语音聊天要满足三个主要条件,支持多人连麦、支持音频混流和多种连麦方式。
|
缓存 UED
聊一聊播放器在一对一直播系统源码中的作用
建立视频数据缓冲区,当网络不足以支持一对一直播系统源码用户流畅的观看直播时,系统会暂停播放,缓存一定的数据,支撑用户流畅观看。

热门文章

最新文章