如何在Flutter中为必填字段设置红色星号(*)
How to set a red asterisk (*) for sign of is mandatory field in Flutter
我正在尝试在 RichText 小部件中获得一个红色星号 ();我可以使用样式 属性。但它会使整个文本变红。我只希望星号 () 为红色。任何线索,如何实现它?
这是我的 RichText 小工具,现在我可以看到一个星号 (*),颜色与其余文本相同。
RichText(
text: TextSpan(
text: '$labelText *',
style: TextStyle(
color: labelColor, fontWeight: fontWeight, fontSize: fontSize)),
textScaleFactor: labelTextScale,
maxLines: labelMaxLines,
overflow: overflow,
textAlign: textAlign,
);
我知道怎么做了。 TextSpan 有一个 children 属性,它以一个 List 作为值。
所以我为星号 (*) 分配了一个 TextSpan。
RichText(
text: TextSpan(
text: '$labelText',
style: TextStyle(
color: labelColor, fontWeight: fontWeight, fontSize: fontSize),
children: [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontWeight: fontWeight,
fontSize: fontSize))
]),
textScaleFactor: labelTextScale,
maxLines: labelMaxLines,
overflow: overflow,
textAlign: textAlign,
),
你可以使用这个:
class FormField extends StatelessWidget {
final String label;
final bool withAsterisk;
const FormField({Key key, @required this.label, @required this.withAsterisk})
: super(key: key);
@override
Widget build(BuildContext context) {
return Stack(children: [
Container(
padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
focusedBorder: OutlineInputBorder(),
errorBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.red, width: 1),
)),
),
),
Padding(
padding: const EdgeInsets.only(left: 32, top: 10),
child: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: ' $label',
style: TextStyle(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
color: DsColor.textColorDrkGrey,
fontWeight: FontWeight.w400,
fontSize: 12)),
TextSpan(
text: withAsterisk ? '* ' : ' ',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 12,
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
color: Colors.red)),
],
),
),
),
]);
}
}
用法 :
FormField(
label: "Name",
withAsterisk: true,
),
FormField(
label: "Phone",
withAsterisk: false,
)
You Can Use This even you can change the colors of all the characters in TextSpan via children property.
下面是我的示例代码你自己搞定吧:
Here is output you can view it
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
String labelText = "Ayush Kumar ";
@override
Widget build(BuildContext context) {
return Scaffold(
body:Center(
child:RichText(
text: TextSpan(
text: '$labelText',
style: TextStyle(
color: Colors.black,
fontSize:30.0,
fontWeight: FontWeight.bold,
),
children: [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontSize:30.0,
fontWeight: FontWeight.bold,
)),
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.blue,
fontSize:30.0,
fontWeight: FontWeight.bold,
)),
])),
),
);
}
}
升级 flutter 到 2.5.3,您将获得一个“标签”小部件来自定义您的标签。你可以随心所欲地改变。
decoration: InputDecoration(
label: Row(
children: [
CustomText(widget.label,
font: Font.NUNITO_REGULAR, color: ColorResource.COLOR_cacaca),
if (widget.isMandatory)
const CustomText(" *",
font: Font.NUNITO_REGULAR,
color: ColorResource.COLOR_DB4437)
],
),
我正在尝试在 RichText 小部件中获得一个红色星号 ();我可以使用样式 属性。但它会使整个文本变红。我只希望星号 () 为红色。任何线索,如何实现它?
这是我的 RichText 小工具,现在我可以看到一个星号 (*),颜色与其余文本相同。
RichText(
text: TextSpan(
text: '$labelText *',
style: TextStyle(
color: labelColor, fontWeight: fontWeight, fontSize: fontSize)),
textScaleFactor: labelTextScale,
maxLines: labelMaxLines,
overflow: overflow,
textAlign: textAlign,
);
我知道怎么做了。 TextSpan 有一个 children 属性,它以一个 List 作为值。
所以我为星号 (*) 分配了一个 TextSpan。
RichText(
text: TextSpan(
text: '$labelText',
style: TextStyle(
color: labelColor, fontWeight: fontWeight, fontSize: fontSize),
children: [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontWeight: fontWeight,
fontSize: fontSize))
]),
textScaleFactor: labelTextScale,
maxLines: labelMaxLines,
overflow: overflow,
textAlign: textAlign,
),
你可以使用这个:
class FormField extends StatelessWidget {
final String label;
final bool withAsterisk;
const FormField({Key key, @required this.label, @required this.withAsterisk})
: super(key: key);
@override
Widget build(BuildContext context) {
return Stack(children: [
Container(
padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
child: TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
focusedBorder: OutlineInputBorder(),
errorBorder: OutlineInputBorder(
borderSide: const BorderSide(color: Colors.red, width: 1),
)),
),
),
Padding(
padding: const EdgeInsets.only(left: 32, top: 10),
child: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: ' $label',
style: TextStyle(
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
color: DsColor.textColorDrkGrey,
fontWeight: FontWeight.w400,
fontSize: 12)),
TextSpan(
text: withAsterisk ? '* ' : ' ',
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 12,
backgroundColor:
Theme.of(context).scaffoldBackgroundColor,
color: Colors.red)),
],
),
),
),
]);
}
}
用法 :
FormField(
label: "Name",
withAsterisk: true,
),
FormField(
label: "Phone",
withAsterisk: false,
)
You Can Use This even you can change the colors of all the characters in TextSpan via children property.
下面是我的示例代码你自己搞定吧:
Here is output you can view it
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
String labelText = "Ayush Kumar ";
@override
Widget build(BuildContext context) {
return Scaffold(
body:Center(
child:RichText(
text: TextSpan(
text: '$labelText',
style: TextStyle(
color: Colors.black,
fontSize:30.0,
fontWeight: FontWeight.bold,
),
children: [
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.red,
fontSize:30.0,
fontWeight: FontWeight.bold,
)),
TextSpan(
text: ' *',
style: TextStyle(
color: Colors.blue,
fontSize:30.0,
fontWeight: FontWeight.bold,
)),
])),
),
);
}
}
升级 flutter 到 2.5.3,您将获得一个“标签”小部件来自定义您的标签。你可以随心所欲地改变。
decoration: InputDecoration(
label: Row(
children: [
CustomText(widget.label,
font: Font.NUNITO_REGULAR, color: ColorResource.COLOR_cacaca),
if (widget.isMandatory)
const CustomText(" *",
font: Font.NUNITO_REGULAR,
color: ColorResource.COLOR_DB4437)
],
),