Giter Site home page Giter Site logo

Comments (3)

bawahakim avatar bawahakim commented on June 13, 2024 1

As a workaround, if you get a stream, you can use something like this when you receive the stream.

  void _updateFirstMessageText(String newText, {bool replace = false}) {
    assert(
      _messages.first is types.TextMessage,
      'First message must be a text message',
    );

    final firstMessage = _messages.first as types.TextMessage;
    final updatedText = replace ? newText : firstMessage.text + newText;
    final updatedMessage = firstMessage.copyWith(
      text: updatedText,
    ) as types.TextMessage;

    setState(() {
      _messages.removeAt(0);
      _addMessage(updatedMessage);
    });
  }

You can also emulate streamed text with something like this

bool _isStreaming = false;
final List<String> _messageQueue = [];

  void _onTextReceived(String newText) {
    _messageQueue.add(newText);
    if (!_isStreaming) {
    setState(() {
      final updatedMessage = _messages.first.copyWith(
        text: _messages.first.text + newText,
      ) as types.TextMessage;
      _messages.removeAt(0);
      _addMessage(updatedMessage);
    });
    }
  }

Future<void> _startStreamingMessages() async {
    _isStreaming = true;
    while (_messageQueue.isNotEmpty) {
      final nextMessage = _messageQueue.removeAt(0);
      await _streamText(nextMessage);
    }
    _isStreaming = false;
  }

  Future<void> _streamText(String message) async {
    var currentIndex = 0;
    final completer = Completer<void>();

    Timer.periodic(const Duration(milliseconds: 15), (timer) { // adjust this for the "speed" of the stream
      if (currentIndex < message.length) {
        setState(() {
          final updatedMessage = _messages.first.copyWith(
            text: _messages.first.text + message[currentIndex],
          ) as types.TextMessage;
          _messages.removeAt(0);
          _addMessage(updatedMessage);
          currentIndex++;
        });
      } else {
        timer.cancel();
        completer.complete();
      }
    });

from flutter_chat_ui.

demchenkoalex avatar demchenkoalex commented on June 13, 2024

no possible with v1. Will try to do in v2. (but please do not wait for me cause it might take lots of time - find an alternative if possible)

from flutter_chat_ui.

lumpidu avatar lumpidu commented on June 13, 2024

When do you plan to release v2 ?

from flutter_chat_ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.