首页 > 解决方案 > Flutter文本小部件将中间的单词分解到下一行如何停止

问题描述

找到了解决方案,它是使用三引号。

Text("""Loooong text ... 放在这里。""")

这就是我的项目中所做的,以防止单词在行中间被打断。 在此处输入图像描述

没有它。其中一些词在新行中被分成两半。 在此处输入图像描述

标签: flutterdarttextformatlongtext

解决方案


下面的代码可能会有所帮助。关键是使用不间断的零宽度字符“\u200d”。更多选项请参考本站

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: Container(
            padding:EdgeInsets.only(left: 30, right:30),
            child:RichText(
              text: TextSpan(children: [
            TextSpan(
                text: "这是一趟十分令人",
                style: TextStyle(color: Colors.green, fontSize: 30.0)),
            TextSpan(
              text: "愉\u200d悦",
              style: TextStyle(
                  color: Colors.red,
                  fontWeight: FontWeight.bold,
                  fontSize: 30.0),
            ),
            TextSpan(
                text: "的旅程",
                style: TextStyle(color: Colors.green, fontSize: 30.0)),
          ])),
          )
        ),
      ),
    );
  }
}

推荐阅读