【问题标题】:scaffoldBackgroundColor does not seem to set color properly in MaterialApp in Flutter在 Flutter 的 MaterialApp 中,scaffoldBackgroundColor 似乎没有正确设置颜色
【发布时间】:2018-10-18 21:57:49
【问题描述】:

我有一个材质应用程序,它使用基于浅色主题的主题。我正在将scaffoldBackgroundColor 更改为似乎不需要的特定颜色。页面的背景是黑色而不是灰色。

应用程序的build() 方法将这个应用程序标识为MaterialApp:

Widget build(BuildContext context) {
    return new MaterialApp(
      title: '???',
      theme: appTheme,
      routes: {
        AppRoutes.login: (context) {
          return new LoginPage(
            authenticator: authenticate,
          );
        },
        AppRoutes.home: (context) {
          return new HomePage(
            appState: appState,
            accountRepository: widget.accountRepository,
            authRepository: widget.authRepository,
          );
        },
      },
    );
  }

主题 - 在这里我重用浅色主题作为我的新主题的基础,将脚手架背景颜色更改为自定义颜色:

const coolGreyHi = const Color.fromARGB(40,30,20,66);

final ThemeData appTheme = _buildAppTheme();

ThemeData _buildAppTheme() {
  final ThemeData base = new ThemeData.light();
  return base.copyWith(
    scaffoldBackgroundColor: coolGreyHi,
    inputDecorationTheme: new InputDecorationTheme(
      border: new OutlineInputBorder(),
    )
  );
}

背景为灰色的页面的构建方法:

Widget build(BuildContext context) {
    return new Scaffold(          
      body: new SafeArea(
        child: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              new Padding(
                padding: const EdgeInsets.fromLTRB(40.0, 0.0, 40.0, 0.0),
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    new Image.asset('images/image.png'),
                    new SizedBox(height: 40.0),
                    new TextField(
                      controller: _uidTextController, decoration: new InputDecoration(
                        hintText: "you@yourdomain.com",
                        labelText: "Email Address",
                      ),

                    ),
                    new SizedBox(height: 12.0),
                    new TextField(
                      controller: _pwdTextController, decoration: new InputDecoration(
                        hintText: "Password",
                        labelText: "Password",
                      ),
                    ),
                    new SizedBox(height: 12.0),
                    new ButtonBar(
                      children: <Widget>[
                        new FlatButton(
                          child: new Text("SIGN UP"),
                          onPressed: () {},
                        ),
                        new RaisedButton(
                          child: new Text("SIGN IN"),
                          elevation: 8.0,
                          onPressed: () {
                            authenticator(_uidTextController.text, _pwdTextController.text).then((bool isAuthenticated) {
                              if (isAuthenticated)
                                Navigator.of(context).pushNamedAndRemoveUntil(AppRoutes.home, (Route<dynamic> route) => false);
                            });
                          },
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ), 
    );
  }

【问题讨论】:

  • 这个问题可以按照文档说明解决
  • @DiegoMarquina,我以为我是,但我没有得到想要的结果。除非我遗漏了什么,否则您提到的文档 sn-ps 我还没有看到,或者这是另一种情况。您指的是哪些特定的 sn-ps 文档?

标签: dart flutter flutter-layout


【解决方案1】:

请使用最新的 Flutter 版本(例如 betadev)频道。然后,您可以在Scaffold 上使用backgroundColor 属性,正如文档在此处概述的那样:https://docs.flutter.io/flutter/material/Scaffold/backgroundColor.html

【讨论】:

  • 我在 Channel beta 上,v0.9.4。 Dart 版本 2.1.0-dev.5.0.flutter-a2eb050044。在 Scaffold 上尝试了 backgroundColor - 仍然没有显示正确的颜色。在离开背景颜色的同时从材料应用程序中删除了主题 - 仍然没有正确设置它。但是我尝试了一种内置颜色,比如 Colors.cyan,它工作正常,所以我认为问题可能出在我的 const coolGreyHi = const Color.fromARGB(40,30,20,66) 的颜色定义上。想发表评论吗?
  • 检查文档的颜色:docs.flutter.io/flutter/dart-ui/Color-class.html。也许这只是一个不正确的颜色定义。
【解决方案2】:

我做了一些进一步的检查,似乎有些颜色,即使是 Flutter 调色板中的颜色,如 Colors.black54 或 Colros.black45 在我的模拟器上确实显示为黑色。我没有在物理设备上测试过它们。我不确定问题出在显示器上还是模拟器上,以及它是如何设置的,但是像 Colros.grey 这样的颜色确实会正常显示。 总而言之,在 ThemeData 上设置 scaffoldBackgroundColor 可以按设计工作,但某些颜色可能无法准确显示(即在我的实例中显示为黑色)。不确定是显示器问题还是模拟器问题。

【讨论】:

  • 如果您使用模拟器并遇到图形错误,请尝试flutter run --enable-software-rendering
猜你喜欢
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 2015-05-13
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多