【问题标题】:Background image not setting to white | adding styling to text背景图像未设置为白色 |为文本添加样式
【发布时间】:2021-02-15 19:55:36
【问题描述】:

这里将两个问题合二为一...对于扑腾和学习仍然很陌生。

  1. 我的背景颜色不会变为白色。 flutter.dev 上的所有资源似乎都不可行。目前,我使用的建议最多的答案是backgroundColor: Colors.white。关于为什么这不起作用的任何想法?

class App extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Onboarding1',
      theme: ThemeData(
        backgroundColor: Colors.white,
        fontFamily: 'fonts/Avenir-Bold',
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
    );
  }
}```
  1. 我希望能够为列中的文本设置样式,但 TextStle 抛出错误。在列中调整文本样式的最佳方法是什么?使用脚手架会更好吗?

@覆盖

  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Image.asset(
          "assets/Onboarding1_Photo.png",
          height: MediaQuery.of(context).size.height * 0.7,
          width: MediaQuery.of(context).size.width,
          fit: BoxFit.cover,
        ),
        Column(
          children: <Widget>[
            Text('Some Text'),
            style: TextStyle(
            )
          ],
        )
      ],
    );
  }
}

根据答案更新...我仍然做错了什么?

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Onboarding1',
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.white,
        fontFamily: 'fonts/Avenir-Bold',
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
    );
  }
}

class Onboarding1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Image.asset(
            "assets/Onboarding1_Photo.png",
            height: MediaQuery.of(context).size.height * 0.7,
            width: MediaQuery.of(context).size.width,
            fit: BoxFit.cover,
          ),
          Column(
            children: <Widget>[
              Text('Some Text', style: TextStyle(fontSize: 24)),
            ],
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    用示例更新答案:

    class Onboarding1 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              Image.asset(
                "assets/Onboarding1_Photo.png",
                height: MediaQuery.of(context).size.height * 0.7,
                width: MediaQuery.of(context).size.width,
                fit: BoxFit.cover,
              ),
              Column(
                children: <Widget>[
                  Text('Some Text', style: TextStyle(fontSize: 24)),
                ],
              ),
            ],
          ),
        );
      }
    }
    
    

    原答案: 您在 ThemeData 中查找的字段是 scaffoldBackgroundColor

    ThemeData(
       scaffoldBackgroundColor: Colors.white,
       fontFamily: 'fonts/Avenir-Bold',
       visualDensity: VisualDensity.adaptivePlatformDensity),
    

    然后将您的专栏包裹在脚手架中,它会起作用。

    至于您的文本样式,在您的代码中,style 在文本小部件之外,它需要在里面,您需要使用属性定义一个 TextStyle

    Text('Some Text',
     style: TextStyle(
      color: Colors.blue, 
      fontSize: 20),
    ),
    

    诚然,在 Flutter 中设置文本样式对我来说有点冗长。出于这个原因,我有自己的可重用自定义文本小部件,可以节省我最常用的文本属性的时间。

    class MyTextWidget extends StatelessWidget {
      final String text;
      final double fontSize;
      final Color color;
      final double spacing;
    
      const MyTextWidget(
          {Key key, this.text, this.fontSize, this.color, this.spacing})
          : super(key: key);
      @override
      Widget build(BuildContext context) {
        return Text(
          text != null ? text : ' ',
    
    // this is part of the google fonts package it won't work if you don't have it in your project but you get the idea
    
          style: kGoogleFontOpenSansCondensed.copyWith(
              fontSize: fontSize ?? 20,
              color: color ?? Colors.white70,
              letterSpacing: spacing ?? 1.0),
        );
      }
    }
    

    然后当我需要一个文本小部件时,它看起来像这样

    MyTextWidget(
       text: 'Some text', 
       fontSize: 25, 
       color: Colors.white54)
    

    【讨论】:

    • 你能检查一下我原来帖子的编辑吗?我还做错了什么?
    • 你把你的脚手架放在柱子里面,柱子需要在脚手架里面。一般来说,Scaffold 将是任何基本页面的根小部件。
    • 也不确定您要使用具有透明背景的容器来完成什么。但是根据我回答的您的另一篇文章,我建议从 Scaffold->Column->Image 开始,就像我在另一个线程中所做的那样,然后从那里开始。将该 Container 设置为无限高和宽也可能会导致问题。
    • 只是试图完成您回答的另一个问题中的设计。我将尝试脚手架、列、图像方法。谢谢!
    • 还要注意您在列中列出孩子的顺序。根据您的其他帖子,您希望将图像放在屏幕顶部,并且在这里您有图像上方的文本小部件。如果您仍希望图像位于顶部,则它必须是列中的第一个子元素。
    猜你喜欢
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    相关资源
    最近更新 更多