简体   繁体   English

在应用程序中将颜色设置为文本小部件在颤动中普遍适用,而无需每次都提及小部件内部的主题

[英]Set Color to Text Widget in App universally in flutter without mentioning the theme inside the Widget everytime

I am new to flutter and trying out things.我是新来的颤振和尝试的东西。

I replaced the Scaffold Widget with a Center Widget (Just messing around).我用一个中心小部件替换了脚手架小部件(只是乱搞)。 All text had a Yellow underline, to overcome this I used TextDecoration所有文本都有一个黄色下划线,为了克服这个问题,我使用了TextDecoration

Text(
    friend.name,
    style: TextStyle(
        decoration: TextDecoration.none
    ),
));

But this was required for all Text widget hence I tried to set it universally for all Text widgets by setting Theme data in the root MaterialApp.但这对于所有 Text 小部件都是必需的,因此我尝试通过在根 MaterialApp 中设置 Theme 数据来为所有 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'),
    );
  }
}

But the Text widget still as the underline.但是 Text 小部件仍然作为下划线。 What I'm trying is similar to setting a style to a tag in css not having to set it every time.我正在尝试类似于在 css 中为标签设置样式而不必每次都设置它。

p
{
    universal style here
}

Am I doing anything wrong?我做错了什么吗? Is there a similar support in flutter.颤振中是否有类似的支持。 Please correct me if I'm wring如果我绞尽脑汁,请纠正我

Don't use static constants for global styling.不要将静态常量用于全局样式。 Do it in the flutter way by using InheritedWidgets .使用InheritedWidgets以颤动的方式完成。 That way you can easily override the Default style for a particular branch of the tree if needed.这样,您可以根据需要轻松覆盖树的特定分支的默认样式。

Well then, what to use for styling text widgets globally?那么,在全局范围内使用什么来设置文本小部件的样式?

You need to define TextTheme for the ThemeData to style Text widgets globally.您需要定义TextThemeThemeData到样式的文本控件全球。 Additionally, use can use the DefaultTextStyle widget to theme a portion of the widget tree.此外,用户可以使用DefaultTextStyle小部件来主题化小部件树的一部分。

As per documentation,根据文档,

The text style to apply to descendant Text widgets without explicit style.应用于没有明确样式的后代 Text 小部件的文本样式。

Place it on the root of your widget tree.将它放在小部件树的根部。

Example:例子:

DefaultTextStyle(
          style: TextStyle(fontSize: 36, color: Colors.blue),
         // child: other widgets
   )

Here is a complete working example that illustrate the following这是一个完整的工作示例,说明了以下内容

  1. Defines a default text style for all Text widgets为所有文本小部件定义默认文本样式
  2. Override the default text style on a particular branch of the tree.覆盖树的特定分支上的默认文本样式。

     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!'), ), ], ); } }

See the live demo here .此处查看现场演示。


You can find a detailed answer here about colors and Theme.您可以在此处找到有关颜色和主题的详细答案。

Create one class for All text style like this :为所有文本样式创建一个类,如下所示:

class AppTheme {

static final primaryFontFamily = "GT Walsheim";

static TextStyle homeScreenBoldStyle() {
    return TextStyle(
        fontFamily: AppTheme.primaryFontFamily,
        fontSize: 16,
        fontWeight: FontWeight.bold,
        color: Colors.white);
  }
}

and Call this class like this :并像这样调用这个类:

Text(trainingType.name,
                      style: AppTheme.homeScreenBoldStyle()),

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

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