【问题标题】:How to set a text background with Flutter?如何使用 Flutter 设置文本背景?
【发布时间】:2019-05-17 09:24:40
【问题描述】:

我对 Flutter 很陌生,但我对从头开始学习它很感兴趣。

现在我正在尝试改变一些文本的背景颜色这样基本的事情,但我被卡住了。

import 'package:flutter/material.dart';

void main() {

  final barColor = const Color(0xFFD63031);

  var app = MaterialApp(    
    home: Scaffold(    
      backgroundColor: barColor,    
    ),    
  );

  Center(    
    child: Text('My Text',      
      textDirection: TextDirection.ltr,    
    ),    
  );
      runApp(app);
}

我确实理解为什么文本没有显示,但我已经为此工作了好几天,并且我尝试了很多不同的事情但没有成功,所以非常感谢任何帮助。

谢谢

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    TL;DR -(2019 年 7 月 8 日更新)

    使用样式属性 (backgroundColor)

    Text(
      'Some text...',
      style: TextStyle(backgroundColor: Colors.blue),
    )
    

    使用样式属性 (background)

    Text(
      'Some text...',
      style: TextStyle(background: Paint()..Colors.blue),
    )
    

    使用DecoratedBox

    const DecoratedBox(
      decoration: const BoxDecoration(color: Colors.blue),
      child: const Text('Some text...'),
    );
    

    长答案

    首先,欢迎使用 Flutter 和 StackOverflow :)

    这是因为误解了您应该使用 Flutter 进行开发的方式。 与您从 main() 函数开始的其他架构发生的情况相反,将您的 vars/objects 实例化并从那里开发您的流程,使用 Flutter 您也可以从您的 main() 函数开始您的小部件树,通常使用 @ 987654332@ 或 CupertinoApp 并适合其所有子项来创建您的应用程序。

    因此,作为示例,要获得所需的内容,您必须将 Center 小部件添加为 Scaffold 的主体,然后将 TextStyle 提供给 Text 小部件,提供属性 color。我给了它蓝色,但你可以给任何你想要的东西。因此,这是您重构的代码

    void main() => runApp(
          MaterialApp(
            home: Scaffold(
              backgroundColor: const Color(0xFFD63031),
              body: Center(
                child: Text(
                  'MyText',
                  textDirection: TextDirection.ltr,
                  style: TextStyle(
                    background: Paint()..color = Colors.blue,
                  ),
                ),
              ),
            ),
          ),
        );
    

    这将提供以下结果

    我建议你看看Awesome Flutter repo,那里有很多很好的 Flutter 内容可以帮助你。

    【讨论】:

    • 太棒了!非常感谢您的完整回答。非常感谢:)
    • TextStyle 有一个 backgroundColor 属性 - 您可以简单地设置它,而不是创建 new Paint()
    • @AlexSemeniuk 谢谢,这可能是同时添加的。我会相应地更新我的答案。
    【解决方案2】:

    很简单,你可以在style属性中设置..

    Text(
      'My Text...',
      style: TextStyle(backgroundColor: Colors.grey),
    )
    

    您可以在 style: TextStyle()

    中将这么多属性设置为 text
    { bool inherit = true, 
      Color color, 
      Color backgroundColor, 
      double fontSize, 
      FontWeight fontWeight, 
      FontStyle fontStyle, 
      double letterSpacing, 
      double wordSpacing, 
      TextBaseline textBaseline, 
      double height, 
      Locale locale, 
      Paint foreground, 
      Paint background, 
      List<Shadow> shadows, 
      List<FontFeature> fontFeatures, 
      TextDecoration decoration, 
      Color decorationColor, 
      TextDecorationStyle decorationStyle, 
      double decorationThickness, 
      String debugLabel, 
      String fontFamily, 
      List<String> fontFamilyFallback, 
      String package
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 2017-10-03
      • 2021-11-17
      • 2021-08-27
      • 1970-01-01
      相关资源
      最近更新 更多