gpt4 book ai didi

flutter - RichText 未按预期设置文本样式

转载 作者:IT老高 更新时间:2023-10-28 12:44:09 29 4
gpt4 key购买 nike

刚刚创建了一个项目:

> flutter create simple

并添加了一个 RichText 小部件,但结果不是我所期望的:默认样式的文本,只有粗体字粗体。在我的安卓设备上,我得到了:

enter image description here

完整的 main.dart:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
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!'),
],
),
),
new Text(
'You have pushed the button this many times:',
),
new Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
),
);
}
}

我错过了什么?这个“红色”主题是从哪里来的?

最佳答案

您现在可以做的事情要简单得多。

如果你想用默认的TextStyle创建一个RichText,只需使用Text.rich:

Text.rich(
TextSpan(
children: [
TextSpan(text: 'Hello '),
TextSpan(
text: 'bold',
style: TextStyle(fontWeight: FontWeight.bold),
),
TextSpan(text: ' world!'),
],
),
)

关于flutter - RichText 未按预期设置文本样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51379194/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com