Flutter 行间隔因语言而异

Flutter line intervals differ depending on language

如何通过 Text 小部件强制 Flutter 遵循给定的行间隔?

我注意到,根据语言的不同,间隔可能会有所不同,这使得小部件高度无法预测。例如,在阿拉伯语中间隔更大。

我比较了 Flutter 和 Android 并注意到 Android 的 TextView 以相同的行间隔呈现阿拉伯文本(它还会自动从右到左呈现它,而 Flutter 没有,这也很可疑)。

示例。

颤振 Android

颤振代码

class IneterlineExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final style = TextStyle(
        fontSize: 32.0,
      height: 1.0,
        );
    return Scaffold(
        appBar: AppBar(
          title: Text('Interline example'),

        ),
        body: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              width: 200.0,
              child: Text("This is a multiline text with English letters",
                  maxLines: 4,
                  overflow: TextOverflow.ellipsis,
                style: style,
              ),
            ),
            Container(
              width: 200.0,
              child: Text("آموزش کامل طرز تهیه پودینگ شکلاتی موز دسر مجلسی",
                  maxLines: 4,
                  overflow: TextOverflow.ellipsis,
                style: style,
              ),
            ),
            Container(
              width: 200.0,
              child: Text("В русском языке интервалы тоже ок Russian",
                  maxLines: 4,
                  overflow: TextOverflow.ellipsis,
                style: style,
              ),
            ),
          ],
        ));
  }
}

Android代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ExampleActivity">

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      >
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="This is a multiline text with English letters"
        android:textSize="32dp"
        android:maxLines="4"
        android:ellipsize="end"
        />
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="آموزش کامل طرز تهیه پودینگ شکلاتی موز دسر مجلسی"
        android:textSize="32dp"
        android:maxLines="4"
        android:ellipsize="end"
        />
    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="В русском языке интервалы тоже ок Russian"
        android:textSize="32dp"
        android:maxLines="4"
        android:ellipsize="end"
        />

  </LinearLayout>


</android.support.constraint.ConstraintLayout>

我试图解决的最初问题是让一些文本(语言未知)适合给定的高度。

即我有一些文本,可能很长,还有一个框,WxH 固定大小。我已经计算出,根据我的字体大小和 height 比例,我可以容纳 L 行文本。所以我放了 maxLinesellipsize 并且它在 Android 应用程序中工作(因为行高是可预测的),但在 Flutter 应用程序中不起作用。 如果 Flutter 文本在溢出给定框时可以省略,我也可以,但不确定如何实现。

我还在 github 上发布了这个问题: https://github.com/flutter/flutter/issues/23875

现在已经修复了(https://github.com/flutter/flutter/commit/c6cc3cdedafee41822ce832d7d73000f2e77b183),解决方案是使用strutStyle 属性 of a Text

Text("Some Arabic text",
  strutStyle: StrutStyle(
    fontFamily: 'FontName',
    forceStrutHeight: true,
  ),
)

https://docs.flutter.io/flutter/painting/StrutStyle/forceStrutHeight.html