String flatter:在传递到文本小部件之前,是否可以在字符串中设置格式(粗体、斜体等)?

String flatter:在传递到文本小部件之前,是否可以在字符串中设置格式(粗体、斜体等)?,string,dart,flutter,String,Dart,Flutter,例如: String desc = "<bold>Hello<bold> World"; new Text(desc); String desc=“Hello World”; 新文本(desc); 您可以使用该软件包 stringhtml='helloworld' newhtmltextview(数据:html) 如果您只需要不同的样式,可以使用RichText小部件和textspands类似 new RichText(文本:new TextSpan(文本:“Hello

例如:

String desc = "<bold>Hello<bold> World";
new Text(desc);
String desc=“Hello World”;
新文本(desc);
您可以使用该软件包

stringhtml='helloworld'

newhtmltextview(数据:html)

如果您只需要不同的样式,可以使用
RichText
小部件和
textspands
类似

new RichText(文本:new TextSpan(文本:“Hello”,样式:DefaultTextStyle.of(上下文)。样式,子项:
[
新文本span(文本:“粗体”,样式:新文本样式(fontWeight:fontWeight.bold)),
新文本span(文本:“世界!”),
], ), )

您可以拥有一个RichText小部件,它可以将TextSpan作为其子级。然后,TextSpan可以将多个TextSpan作为其子项,每个TextSpan都可以有自己的样式和手势检测器。下面是如何使用onTap手势检测器包装“阅读更多”隐私声明的示例。这将给出如下所示的输出,当点击“读取”时,应用程序将被路由到“/隐私”页面

class\u LoginViewState扩展状态{
TapGestureRecognitor _routeToPrivacy;
@凌驾
void initState(){
super.initState();
_routeToPrivacy=TapGestureRecognitizer()…onTap=routeToPrivacy;
}
void routeToPrivacy(){
Navigator.pushNamed(上下文“/privacy”);
}
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.white,
正文:中(
子:容器(
宽度:200,
子栏(
RichText(
text:TextSpan(
样式:TextStyle(
颜色:Colors.blue.shade900,
fontFamily:GoogleFonts.lato().fontFamily),
儿童:[
TextSpan(
文本:“通过注册,您接受我们的隐私政策。”,
),
TextSpan(
识别器:_routeToPrivacy,
案文:“读”,
样式:TextStyle(
颜色:Colors.red.shade500,
字体权重:字体权重。粗体)
],
),
),
],
),
),
),
);
}

StyledText小部件可用于此目的


你不应该再使用那些标签了。它是过时的,我相信已经贬值了。这就是说……当你把一个字符串传递到文本构造函数时会发生什么?我打赌……它会显示文本,但不会呈现HTML。原因是:它是一个文本对象。不涉及任何HTML。你也可以使用,你可以制作您可以使用富文本小部件,并按照下面的答案进行格式化
new RichText( text: new TextSpan(text: 'Hello ', style: DefaultTextStyle.of(context).style, children:          
<TextSpan>[
new TextSpan(text: 'bold', style: new TextStyle(fontWeight: FontWeight.bold)), 
new TextSpan(text: ' world!'), 
], ), )
class _LoginViewState extends State<LoginView> {
  TapGestureRecognizer _routeToPrivacy;

  @override
  void initState() {
    super.initState();
    _routeToPrivacy= TapGestureRecognizer()..onTap = routeToPrivacy;
  }

  void routeToPrivacy() {
    Navigator.pushNamed(context, '/privacy');
  }

  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Container(
          width: 200,
          child: Column(  
              RichText(
                text: TextSpan(
                  style: TextStyle(
                      color: Colors.blue.shade900,
                      fontFamily: GoogleFonts.lato().fontFamily),
                  children: [
                    TextSpan(
                      text: 'By signing up you accepts our privacy policy. ',
                    ),
                    TextSpan(
                        recognizer: _routeToPrivacy,
                        text: "Read",
                        style: TextStyle(
                            color: Colors.red.shade500,
                            fontWeight: FontWeight.bold))
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
StyledText(text:desc, 
           styles: { 'bold': ActionTextStyle(fontWeight: FontWeight.bold)},
          );