简体   繁体   English

如何在 Flutter 中设置 TextSpan 的圆角

[英]How to set rounded corner of TextSpan in Flutter

I want to set rounded corner of Textspan in flutter, I think class Paint is needed, but I cannot figure out how to do that.我想在颤动中设置 Textspan 的圆角,我认为需要类Paint ,但我不知道如何做到这一点。

图片

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: new AppBar(),
        body: new RichText(
          text: new TextSpan(
            text: null,
            style: TextStyle(fontSize: 20.0, color: Colors.black),
            children: <TextSpan>[
              new TextSpan(
                text: 'inactive ',),
              new TextSpan(
                  text: 'active',
                  style: new TextStyle(
                    color: Colors.white,
                    background: Paint()
                      ..color = Colors.redAccent,
                  )),
            ],
          ),
        ),
      ),
    );
  }
}

Is there a way to use Textspan to achieve that instead of using a Container wrapping Text ?有没有办法使用 Textspan 来实现这一点,而不是使用Container wrapping Text

Without using Container or something else - I can see only one way to make corners rounded不使用Container或其他东西 - 我只能看到一种使角落变圆的方法

TextSpan(
    text: 'active',
    style: TextStyle(
        fontSize: 20.0,
        color: Colors.white,
        background: Paint()
          ..strokeWidth = 24.0
          ..color = Colors.red
          ..style = PaintingStyle.stroke
          ..strokeJoin = StrokeJoin.round))

But in this case there are paddings around text, so I doubt this is proper way但在这种情况下,文本周围有填充,所以我怀疑这是正确的方法

You can use RichText with WidgetSpan , then combine it with line height您可以将RichTextWidgetSpan一起WidgetSpan ,然后将其与行高结合使用

RichText(
  text: TextSpan(
    children: [
      WidgetSpan(
        child: Container(
          child: Text(
            addressItem.deliveryAddressType == "home" ? "Nhà" : "Văn phòng",
            style: TextStyle(
              fontFamily: AppFonts.SFUITextMedium,
              color: AppColors.wram_grey,
              fontSize: AppFonts.textSizeSmall,
            ),
          ),
          decoration: BoxDecoration(
              color: AppColors.header_grey, borderRadius: BorderRadius.all(Radius.circular(20))),
          padding: EdgeInsets.fromLTRB(6, 2, 6, 2),
          margin: EdgeInsets.only(right: 5),
        ),
      ),
      TextSpan(
        text: '${addressItem.street}, ${addressItem.ward}, ${addressItem.city}, ${addressItem.region}',
        style: TextStyle(
            fontFamily: AppFonts.SFUITextMedium,
            color: AppColors.wram_grey,
            fontSize: AppFonts.textSizeMedium,
            height: 1.5),
      ),
    ],
  ),
),

在此处输入图片说明

if the highlighted text is short enough (so you do not want to break line in it), you can just use WidgetSpan insted of TextSpan.如果突出显示的文本足够短(因此您不想在其中断行),则可以仅使用 TextSpan 的 WidgetSpan 插入。

For example:例如:

RichText(
            textAlign: TextAlign.center,
            text: TextSpan(
              children: [
                TextSpan(
                  text: "some text",
                ),
                WidgetSpan(
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 1),
                    decoration: BoxDecoration(
                      color: Colors.red,
                      borderRadius: BorderRadius.circular(999),
                    ),
                    child: IntrinsicWidth(
                      child: Text(
                        "some text",
                      ),
                    ),
                  )
                ),
                TextSpan(text: "some text"),
              ],
            ),
          ),

If you want to set the rounded corner of the text field in Flutter You have to use Container only, If you want to avoid Container then there is another way, you have to avoid using ListView as a body in your app.如果你想在 Flutter 中设置文本字段的圆角你必须只使用 Container,如果你想避免 Container 那么还有另一种方法,你必须避免在你的应用程序中使用 ListView 作为主体。 If you want to add an only single item on screen then you can try without container but if there is more than one item then you have no choice without implementing ListView and add multiple containers for a different purpose.如果您只想在屏幕上添加一个项目,那么您可以尝试不使用容器,但如果有多个项目,那么您别无选择,而无需实现 ListView 并为不同目的添加多个容器。

new Container(
              height: 80.0,
              color: Colors.transparent,
              child: new Container(
                  decoration: new BoxDecoration(
                      color: Colors.green,
                      borderRadius: new BorderRadius.only(
                          topLeft: const Radius.circular(30.0),
                          topRight: const Radius.circular(30.0),
                          bottomLeft: const Radius.circular(30.0),
                          bottomRight: const Radius.circular(30.0))),
                  child: new Center(
                    child: new Text("Active text"),
                  )),
            ),

I have created a package which is able to customize the background of the text --> you can change the radius, padding and color for the text background.我创建了一个可以自定义文本背景的包 --> 您可以更改文本背景的半径、填充和颜色。

It's a fork from the AutoSizeText widget so you could even autosize the text if you want to.它是 AutoSizeText 小部件的一个分支,因此您甚至可以根据需要自动调整文本大小。

Here is an example image of the text with background, radius and padding.这是带有背景、半径和填充的文本示例图像。 在此处输入图片说明

Hope this can help anyone who needs it.希望这可以帮助任何需要它的人。

The link to the github repo is following: https://github.com/Letalus/auto_size_text_background github repo 的链接如下: https : //github.com/Letalus/auto_size_text_background

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM