作者:幸福抉择2502901973 | 来源:互联网 | 2022-12-10 14:00
刚刚创建了一个项目:
> flutter create simple
并添加了一个RichText
小部件,但结果并不是我所期望的:一个默认的样式文本,只有粗体字粗体.在我的Android设备上,我得到了:
完整的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 {
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: [
new RichText(
text: new TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: [
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),
),
);
}
}
我错过了什么?从哪里来的这个"红色"主题?
1> Hemanth Raj..:
DefaultTextStyle.of(context)
如果DefaultTextStyle
父级有父级和获取样式,则会显示该用法.因此,尝试将整个HomePage
小部件包装在DefaultTextStyle
具有某种样式的小部件中.或者制作RichText
一个单独的小部件并进行封装DefaultTextStyle
.
例:
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State {
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: [
new DefaultTextStyle(style: Theme.of(context).textTheme.title, child: new RichWidget()),
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),
),
);
}
}
class RichWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new RichText(
text: new TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: [
new TextSpan(
text: 'bold',
style: new TextStyle(fontWeight: FontWeight.bold)),
new TextSpan(text: ' world!'),
],
),
);
}
}
希望这可以帮助!
我想还没有合适的文件.你可以查看[this](https://github.com/flutter/flutter/issues/12841).不要解释默认情况下`DefaultTextStyle`会有一些样式.它只是另一个继承的小部件,如`Theme`或`Scaffold`,允许其子级使用`context`访问其属性.