我是新手,可以尝试一些东西。
我用中心小部件替换了脚手架小部件(只是在弄乱)。所有文字都有黄色下划线,为了克服这一点,我使用了TextDecoration
Text(
friend.name,
style: TextStyle(
decoration: TextDecoration.none
),
));
但这是所有Text小部件所必需的,因此我尝试通过在根MaterialApp中设置主题数据来为所有Text小部件统一设置它。
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Friends List',
theme: ThemeData(
primaryColor: Colors.black,
primarySwatch: Colors.teal,
primaryTextTheme: TextTheme(
headline1: TextStyle(color: Colors.green, decoration: TextDecoration.none),
headline2: TextStyle(color: Colors.green, decoration: TextDecoration.none),
headline3: TextStyle(color: Colors.green, decoration: TextDecoration.none),
headline4: TextStyle(color: Colors.green, decoration: TextDecoration.none),
headline5: TextStyle(color: Colors.green, decoration: TextDecoration.none),
headline6: TextStyle(color: Colors.green, decoration: TextDecoration.none),
bodyText1: TextStyle(color: Colors.green, decoration: TextDecoration.none),
bodyText2: TextStyle(color: Colors.green, decoration: TextDecoration.none),
),
textTheme: TextTheme(
headline1: TextStyle(decoration: TextDecoration.none),
headline2: TextStyle(decoration: TextDecoration.none),
headline3: TextStyle(decoration: TextDecoration.none),
headline4: TextStyle(decoration: TextDecoration.none),
headline5: TextStyle(decoration: TextDecoration.none),
headline6: TextStyle(decoration: TextDecoration.none),
bodyText1: TextStyle(decoration: TextDecoration.none),
bodyText2: TextStyle(decoration: TextDecoration.none)
),
),
home: MyHomePage(title: 'Friends list'),
);
}
}
但是“文本”小部件仍然是下划线。我正在尝试类似于在css中为标签设置样式,而不必每次都设置它。
p
{
universal style here
}
我做错什么了吗?颤振中是否有类似的支持。如果我要拧紧请纠正我
不要将静态常量用于全局样式。通过使用InheritedWidgets以流畅的方式进行操作。这样,您可以根据需要轻松覆盖树的特定分支的默认样式。
您需要为ThemeData定义TextTheme,以全局设置Text小部件的样式。此外,用户可以使用DefaultTextStyle小部件为小部件树的一部分设置主题。
根据文档,
适用于没有显式样式的后代Text窗口小部件的文本样式。
将其放在小部件树的根上。
DefaultTextStyle(
style: TextStyle(fontSize: 36, color: Colors.blue),
// child: other widgets
)
这是一个完整的工作示例,说明了以下内容
覆盖树的特定分支上的默认文本样式。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: Colors.white),
debugShowCheckedModeBanner: false,
home: Scaffold(
///This style will be applied to all the TextWidgets below.
body: DefaultTextStyle(
style:
Theme.of(context).textTheme.headline6.copyWith(color: Colors.red),
child: Center(
child: MyWidget(),
),
),
),
);
}
}
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
///This text will be in red color
Text('Hello, World!'),
DefaultTextStyle(
style:
DefaultTextStyle.of(context).style.copyWith(color: Colors.blue),
///We don't need to place Text widget as direct child of
///DefaultTextStyle. This is the proof.
child: Container(
///Just another widget
child: Container(
///This text will be in blue color
child: Text('Hello, World!'),
),
),
),
DefaultTextStyle(
style:
DefaultTextStyle.of(context).style.copyWith(color: Colors.green),
///This text will be in green color
child: Text('Hello, World!'),
),
],
);
}
}
在这里观看现场演示。
您可以在此处找到有关颜色和主题的详细答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句