fluttercandies / extended_text

A powerful extended official text for Flutter, which supports Speical Text(Image,@somebody), Custom Background, Custom overFlow, Text Selection.
MIT License
664 stars 134 forks source link

当TextOverflowWidget和emoji同时使用时报错 #131

Closed ma125125t closed 2 years ago

ma125125t commented 2 years ago
════════ Exception caught by painting library ══════════════════════════════════
Invalid argument(s): string is not well-formed UTF-16
════════════════════════════════════════════════════════════════════════════════
import 'package:extended_text/extended_text.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  _HomePageState();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          color: Colors.red,
          constraints: BoxConstraints(maxWidth: 240),
          child: ExtendedText(
            '呆呆很长呆呆很长rrr🌧rw..呆呆很长呆呆rrrrrrr',
            joinZeroWidthSpace: true,
            maxLines: 1,
            style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
            overflowWidget: TextOverflowWidget(
              position: TextOverflowPosition.middle,
              align: TextOverflowAlign.center,
              child: Text(
                '...',
              ),
            ),
          ),
        ),
      ),
    );
  }
}
zmtzawqlp commented 2 years ago

demo 移除无用代码和引用

ma125125t commented 2 years ago

demo 移除无用代码和引用

已修改好了

zmtzawqlp commented 2 years ago

emoji 2个字节的。没法做计算。来处理溢出效果。没法知道这个emoji是否是一个整体。

zmtzawqlp commented 2 years ago

https://github.com/dart-lang/sdk/issues/35798

试了下。就算不报错,textpainter 也找不到这个只有一半字节文字,就是说算不出来这个文字的文字。。你选的middle,而刚好这个emoji,去掉一半就不会溢出了。

ma125125t commented 2 years ago

我这里布局比较复杂,没法直接贴出来,但是带emoji使用时,这里可能会出现无限循环(导致页面假死,可以看到_layoutCount变量无限升高),简单布局则不会。建议这里做一些保护措施。

while (_hasVisualOverflow) {
      testTextPainter = _tryToFindNoOverflow1(range, hideWidgets);
      // try to find no overflow

      if (_hasVisualOverflow) {
        // not find
        assert(range.end != maxOffset, 'can\' find no overflow');
        range.end = math.min(
            range.end + math.max((maxEnd - range.end) ~/ 2, 1), maxOffset);
        hideWidgets.clear();
      } else {
        // see pre one whether overflow
        range.end = math.min(range.end - 1, maxOffset);
        _tryToFindNoOverflow1(range, <int>[]);
        if (_hasVisualOverflow) {
          // fix end
          range.end = math.min(range.end + 1, maxOffset);
          // find the one
          _hasVisualOverflow = false;
        } else {
          maxEnd = range.end;
          range.end = math.max(
              range.start,
              math.min(
                  maxEnd - math.max((maxEnd - range.start) ~/ 2, 1), maxEnd));
          // continue
          _hasVisualOverflow = true;
        }
      }
    }
zmtzawqlp commented 2 years ago

嗯。这个后面优化下。我看看能处理点这种情况不

zmtzawqlp commented 2 years ago

demo里面的问题解决了。你试试引用下github 的版本,看看你项目里面是否还有其他问题。 1.有新问题,请提供下新demo 2.没问题,请告诉下。我会发新包

ma125125t commented 2 years ago

👍🏻 最近有点忙,晚些时候会试一下哈。

ma125125t commented 2 years ago

@zmtzawqlp 嗯,这个好了。

zmtzawqlp commented 2 years ago

好的。版本已经发了