将App中的“颜色”设置为通用,然后每次都会在窗口小部件内提及主题

发现错误280猴子

我是新手,可以尝试一些东西。

我用中心小部件替换了脚手架小部件(只是在弄乱)。所有文字都有黄色下划线,为了克服这一点,我使用了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
   )

这是一个完整的工作示例,说明了以下内容

  1. 为所有“文本”小部件定义默认的文本样式
  2. 覆盖树的特定分支上的默认文本样式。

      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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在窗口小部件内居中

如何在Flutter中的小部件内获取主题颜色

如何在Windows中为小部件设置主题?

无法将ListView设置为LayoutBuilder窗口小部件(Flutter)

为什么某些视图在窗口小部件中显示为红色的禁止进入标志?

Flutter可选地在窗口小部件树中包含/排除父窗口小部件

Tkinter在窗格中动态调整窗口小部件的大小

图像视图未显示在窗口小部件中

Flutter:我收到一个错误,该状态为窗口小部件不再显示在窗口小部件树中

通过addPostFrameCallback的Flutter Provider访问说窗口小部件不在窗口小部件树中,但Flutter检查器显示为其他

在窗口小部件中将小部件封装在自己的类中?

如今,小部件每次刷新都会使用更多的内存,然后最终崩溃

在窗口外滑动小部件

为 Android 中的小部件启用小部件设置按钮?

如果将“鼠标事件”设置为“透明”,仍如何使用窗口小部件的功能?

如何在Flutter中的GridView中为窗口小部件设置自定义高度?

如何在Android Studio中为窗口小部件不设置高度?

如何在SplitLayoutPanel中为中央窗口小部件“ setWidgetSize”设置?

如何在代码中为Android小部件设置文本颜色?

从父窗口小部件设置 `MainAxisSize` 为 min

如何在窗口小部件扩展中添加5个以上的窗口小部件?WidgetBundle的小部件的最大数量不能超过5

如何使用 AnimatedBuilder 小部件为颜色设置动画?扑

为每一天设置不同的颜色(小部件)

在窗口中左右对齐窗口小部件

设置小部件背景颜色

每次退出时,Eclipse颜色主题都会恢复为默认值

CKEditor不允许在窗口小部件中嵌套<a>元素

是否有tkinter小部件可轻松在窗口(画布)中显示表(SQLite)?

Addtoany:防止共享按钮出现在窗口小部件中