简体   繁体   English

在 TextField Flutter 中为文本设置圆角颜色背景

[英]Set rounded color background to text in TextField Flutter

I would like my text background in Textfield looks like this :我希望 Textfield 中的文本背景如下所示:

在此处输入图片说明

But with this code :但是有了这个代码:

     style: TextStyle(
              background: Paint()..color = Colors.blue
                ..style = PaintingStyle.fill,
              color: Colors.white,
            ),

I have this result :我有这个结果:

在此处输入图片说明

There is no padding , no rounded corners and a transparent line between both...两者之间没有填充,没有圆角透明线......

How can I do this ?我怎样才能做到这一点 ?

EDIT :编辑 :

Another way with TextStyle was provided by @Csaba Mihaly but this is a workaround I want to avoid. @Csaba Mihaly 提供了另一种使用TextStyle方法,但这是我想避免的一种解决方法。 I'm looking for a custom paint solution我正在寻找定制的油漆解决方案

EDIT :编辑 :

According answers provided PaintStyle.stroke can be used but it's not 100% matching the expected result (first screen) :根据提供的答案 PaintStyle.stroke 可以使用,但它不是 100% 匹配预期结果(第一个屏幕):

No matter the text size is, in order to fill the empty space the stroke width must be bigger as I can see.无论文本大小如何,为了填充空白空间,我可以看到笔画宽度必须更大。 It will render a large padding and corner radius.它将渲染一个大的填充和拐角半径。 In my case :就我而言:

在此处输入图片说明 在此处输入图片说明

So I found this article https://medium.com/@pinkesh.earth/flutter-quick-tip-how-to-set-text-background-color-with-curve-d40a2f96a415 It describes how to use textstyle to look like what you want, but it doesn't exactly work like that, I don't know why.所以我找到了这篇文章https://medium.com/@pinkesh.earth/flutter-quick-tip-how-to-set-text-background-color-with-curve-d40a2f96a415它描述了如何使用 textstyle 看起来像你想要什么,但它并不完全像那样工作,我不知道为什么。 It draws the next line's background above the previous.它在前一行之上绘制下一行的背景。

I managed to work around it (bug?) by stacking two textfields(one has transparent text, the other transparent background).我设法通过堆叠两个文本字段(一个具有透明文本,另一个具有透明背景)来解决它(错误?)。

The result:结果:

在此处输入图片说明

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  final myControllerName = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
            inputDecorationTheme: const InputDecorationTheme(
          fillColor: Colors.transparent,
          filled: true,
          focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.transparent)),
          enabledBorder: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.transparent),
          ),
          border: UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.transparent),
          ),
        )),
        debugShowCheckedModeBanner: false,
        home: Scaffold(
            body: Center(
                child: Stack(
          children: [
            IntrinsicWidth(
              child: TextField(
                controller: myControllerName,
                style: TextStyle(
                    color: Colors.transparent,
                    fontWeight: FontWeight.w600,
                    fontSize: 20,
                    background: Paint()
                      ..strokeWidth = 25
                      ..color = Colors.blue
                      ..style = PaintingStyle.stroke
                      ..strokeJoin = StrokeJoin.round),
                keyboardType: TextInputType.multiline,
                maxLines: null,
                textAlign: TextAlign.center,
              ),
            ),
            IntrinsicWidth(
              child: TextField(
                controller: myControllerName,
                style: const TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w600,
                    fontSize: 20,
                    backgroundColor: Colors.transparent),
                keyboardType: TextInputType.multiline,
                maxLines: null,
                textAlign: TextAlign.center,
              ),
            )
          ],
        ))));
  }
}

Expandin on the @Csisanyi answer, you need to adjust the textfield padding, textstyle and borderradius number for your preferences.扩展@Csisanyi 的答案,您需要根据自己的喜好调整文本字段填充、文本样式和边框半径数。 Tried it in dartpad, looks similar to what you want.在 dartpad 中尝试过,看起来与您想要的相似。

For the dynamic textfield width shrink you might want to try: https://pub.dev/packages/auto_size_text_field对于动态文本字段宽度缩小,您可能想尝试: https : //pub.dev/packages/auto_size_text_field

TextField(
        style: const TextStyle(color: Colors.white),
        keyboardType: TextInputType.multiline,
        maxLines: null,
        decoration: InputDecoration(
          fillColor: Colors.blue,
          contentPadding:
              const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
          filled: true,
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),

Just give the desired strokeWidth value and it will work fine.只需提供所需的strokeWidth值,它就会正常工作。 Output Image输出图像

style: TextStyle(
        background: Paint()
          ..strokeWidth = 17
          ..color = Colors.blue
          ..strokeJoin = StrokeJoin.round
          ..strokeCap = StrokeCap.round
          ..style = PaintingStyle.stroke,

        color: Colors.black,
      )

Do like this:这样做:

style: TextStyle(
              background: Paint()..color = Colors.blue
                ..strokeJoin = StrokeJoin.round
                ..strokeCap = StrokeCap.round
                ..style = PaintingStyle.stroke
                ..strokeWidth = 30.0,
              color: Colors.white,
            ),

You code snippet will work if you add ..strokeWidth = 20 to the style, eg:如果您将..strokeWidth = 20添加到样式中,您的代码片段将起作用,例如:

Text(
    'Lorem Ipsum is simply dummy text of the printing and typesetting industry. ',
    textAlign: TextAlign.center,
    style: TextStyle(
     background: Paint()
       ..color = Colors.blue
       ..strokeWidth = 20
       ..strokeJoin = StrokeJoin.round
       ..strokeCap = StrokeCap.round
       ..style = PaintingStyle.stroke,
     color: Colors.white,
    ))

在此处输入图片说明

Stroke width defines the 'fatness' of the area around the text.描边宽度定义了文本周围区域的“粗细”。 Though to be accurate, the inside corners (look near word 'industry') are not rounded, I doubt text background can fix that.虽然准确地说,内角(靠近“行业”一词)不是圆形的,我怀疑文本背景可以解决这个问题。

Also please not that if you're targeting Web you might need to use Skia renderer instead of HTML renderer( https://stackoverflow.com/a/69975665/440696 ) in order to avoid the following artefact:另外请注意,如果您的目标是 Web,则可能需要使用 Skia 渲染器而不是 HTML 渲染器( https://stackoverflow.com/a/69975665/440696 )以避免以下伪影: 在此处输入图片说明

I created a package called rounded_background_text to achive this我创建了一个名为rounded_background_text的包来实现这一点

https://pub.dev/packages/rounded_background_text https://pub.dev/packages/rounded_background_text

展示柜

Usage:用法:

import 'package:rounded_background_text/rounded_background_text.dart';

RoundedBackgroundText(
  'A cool text to be highlighted',
  style: const TextStyle(fontWeight: FontWeight.bold),
  backgroundColor: Colors.white,
),

TextFields are also supported还支持文本字段

Try something like this尝试这样的事情

TextField(
  keyboardType: TextInputType.multiline
  decoration: InputDecoration(
      border: OutlineInputBorder(
        borderRadius: BorderRadius.all(<value>),
      ),

You can find other examples here https://stacksecrets.com/flutter/flutter-textfield-decoration-in-depth#How_To_Decorate_Border_Of_TextField您可以在此处找到其他示例https://stacksecrets.com/flutter/flutter-textfield-decoration-in-depth#How_To_Decorate_Border_Of_TextField

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

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