[英]Set rounded color background to text in TextField Flutter
我希望 Textfield 中的文本背景如下所示:
但是有了这个代码:
style: TextStyle(
background: Paint()..color = Colors.blue
..style = PaintingStyle.fill,
color: Colors.white,
),
我有这个结果:
两者之间没有填充,没有圆角和透明线......
我怎样才能做到这一点 ?
编辑 :
@Csaba Mihaly 提供了另一种使用TextStyle
方法,但这是我想避免的一种解决方法。 我正在寻找定制的油漆解决方案
编辑 :
根据提供的答案 PaintStyle.stroke 可以使用,但它不是 100% 匹配预期结果(第一个屏幕):
无论文本大小如何,为了填充空白空间,我可以看到笔画宽度必须更大。 它将渲染一个大的填充和拐角半径。 就我而言:
所以我找到了这篇文章https://medium.com/@pinkesh.earth/flutter-quick-tip-how-to-set-text-background-color-with-curve-d40a2f96a415它描述了如何使用 textstyle 看起来像你想要什么,但它并不完全像那样工作,我不知道为什么。 它在前一行之上绘制下一行的背景。
我设法通过堆叠两个文本字段(一个具有透明文本,另一个具有透明背景)来解决它(错误?)。
结果:
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,
),
)
],
))));
}
}
扩展@Csisanyi 的答案,您需要根据自己的喜好调整文本字段填充、文本样式和边框半径数。 在 dartpad 中尝试过,看起来与您想要的相似。
对于动态文本字段宽度缩小,您可能想尝试: 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),
),
),
),
只需提供所需的strokeWidth
值,它就会正常工作。 输出图像
style: TextStyle(
background: Paint()
..strokeWidth = 17
..color = Colors.blue
..strokeJoin = StrokeJoin.round
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke,
color: Colors.black,
)
这样做:
style: TextStyle(
background: Paint()..color = Colors.blue
..strokeJoin = StrokeJoin.round
..strokeCap = StrokeCap.round
..style = PaintingStyle.stroke
..strokeWidth = 30.0,
color: Colors.white,
),
如果您将..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,
))
描边宽度定义了文本周围区域的“粗细”。 虽然准确地说,内角(靠近“行业”一词)不是圆形的,我怀疑文本背景可以解决这个问题。
另外请注意,如果您的目标是 Web,则可能需要使用 Skia 渲染器而不是 HTML 渲染器( https://stackoverflow.com/a/69975665/440696 )以避免以下伪影:
我创建了一个名为rounded_background_text
的包来实现这一点
https://pub.dev/packages/rounded_background_text
用法:
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,
),
还支持文本字段
尝试这样的事情
TextField(
keyboardType: TextInputType.multiline
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(<value>),
),
您可以在此处找到其他示例https://stacksecrets.com/flutter/flutter-textfield-decoration-in-depth#How_To_Decorate_Border_Of_TextField
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.