【问题标题】:Flutter how to get maximum possible lines in a Text widget with given size?Flutter 如何在给定大小的文本小部件中获得最大可能的行?
【发布时间】:2021-04-15 10:49:12
【问题描述】:

我必须知道给定大小和 TextStyle 的文本中有多少行。 我玩过TextPainter,但这仅在超出最大行数时提供信息,但不提供适合它的字符串的最后一个字符索引。

用例: 我必须将一个字符串拆分为多个具有固定大小和文本样式的文本小部件。

【问题讨论】:

    标签: flutter dart flutter-layout flutter-android


    【解决方案1】:

    我不确定这是否是您想要的,但您可以使用 TextSpan 为您的文本设置不同的样式。 这是一个例子:

    import 'package:flutter/material.dart';
    
    class TextSpanExample extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: RichText(
              text: TextSpan(
                children: [
                  TextSpan(
                    text: "TextSpan number 1",
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 14,
                    ),
                  ),
                  TextSpan(
                    text: "TextSpan number 2",
                    style: TextStyle(
                      color: Colors.red,
                      fontSize: 18,
                      fontWeight: FontWeight.bold
                    ),
                  ),
                  TextSpan(
                    text: "TextSpan number 3",
                    style: TextStyle(
                      color: Colors.green,
                      fontSize: 16,
                    ),
                  ),
                ]
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2019-07-11
    • 2021-06-15
    • 1970-01-01
    • 2013-05-02
    • 2022-11-06
    • 1970-01-01
    • 2019-03-10
    • 2011-05-07
    • 1970-01-01
    相关资源
    最近更新 更多