简体   繁体   English

Flutter 文本颜色主题在 ListTile 标题下不起作用

[英]Flutter Text Color Theme doesn't work under ListTile title

I am getting started in Flutter and just trying out stuff.我从 Flutter 开始,只是尝试一些东西。 I set a custom theme, but Text Widgets under the title property of ListTile don't get the right color.我设置了自定义主题,但 ListTile 的 title 属性下的文本小部件没有获得正确的颜色。 Also Icons under the leading property don't get the right color as well.此外,领先属性下的图标也没有获得正确的颜色。

I tried setting some other colors, and sorted out, that the Problem only exists within that element.我尝试设置其他一些 colors,并整理出问题仅存在于该元素中。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      theme: ThemeData(
          primaryColor: Colors.black,
          scaffoldBackgroundColor: Color(0xff202020),
          cardTheme: CardTheme(color: Colors.black),
          textTheme: TextTheme(
              body1: TextStyle(color: Colors.white),
              subtitle: TextStyle(color: Colors.white),
              headline: TextStyle(color: Colors.white)),
          iconTheme: IconThemeData(color: Colors.white)),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("HomePage"),
          leading: IconButton(
              icon: Icon(Icons.arrow_back_ios),
              tooltip: "back to the last page.",
              onPressed: () {
                Navigator.pop(context);
              })
        ),
        body: Card(
          child: ListTile(
            title: Text("Test"),
            leading: new Icon(Icons.devices)
            ),
        ));
  }
}

The Text for the title should appear white as well as the icon, instead it is black.标题的文本应该和图标一样显示为白色,而不是黑色。 All other Text is white.所有其他文本都是白色的。

In order to make use of you're theme you need to use Theme.of(context). 为了利用您的主题,您需要使用Theme.of(context)。

Container(
color: Theme.of(context).accentColor,
child: Text(
'Text with a background color',
style: Theme.of(context).textTheme.title,
 ),
);

Read more about it here in the cookbook. 在食谱中阅读有关此内容的更多信息。 You are on the right track https://flutter.dev/docs/cookbook/design/themes 您处在正确的轨道上https://flutter.dev/docs/cookbook/design/themes

The title on ListTile is using subhead textStyle of Theme. ListTile 标题是使用主题的小标题 文本样式 So if you want config color of ListTile on ThemeData you need change subhead . 所以,如果你想在ThemeData ListTile的配置颜色,你需要改变小标题

textTheme: TextTheme(
          subhead: TextStyle(color: Colors.white),
          ...)

Just change body1 to bodyText1 in the following location:只需在以下位置将 body1 更改为 bodyText1 即可:

C:\\src\\flutter.pub-cache\\hosted\\pub.dartlang.org\\charts_flutter-0.9.0\\lib\\src\\behaviors\\legend\\legend_entry_layout C:\\src\\flutter.pub-cache\\hosted\\pub.dartlang.org\\charts_flutter-0.9.0\\lib\\src\\behaviors\\legend\\legend_entry_layout

This will solve the issue.这将解决问题。

In Flutter 3 which I'm currently using, titleMedium defines the text style for title of ListTile s.在我当前使用的 Flutter 3 中, titleMedium定义了ListTiletitle的文本样式。

MaterialApp(
  theme: ThemeData(
    textTheme: Typography().black.copyWith(
      titleMedium: const TextStyle(
        fontSize: 32,
      ),
  ),
)

For example the above theme makes theme relatively large.例如上面的主题使主题比较大。

Flutter team should provide developers with a reference for these styles. For the moment you can find out which style corresponds to which widget by trial and error. Flutter 团队应该为开发者提供这些styles 的参考。目前您可以通过反复试验找出哪种样式对应于哪个widget。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM