【问题标题】:Flutter - How can I change the background color of LicensePage?Flutter - 如何更改 LicensePage 的背景颜色?
【发布时间】:2020-10-26 11:10:03
【问题描述】:

我想将除LicensePage 之外的每个屏幕的背景颜色设置为某种颜色,因此我通过MaterialApptheme 参数指定了scaffoldBackbroundColor,如下所示。

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(scaffoldBackgroundColor: Colors.blue.shade200),
      home: HomeScreen(),
    );
  }
}

这也改变了许可页面的背景颜色,所以为了将它改回白色,我尝试覆盖scaffoldBackbroundColor,但没有成功。

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Theme(
        data: Theme.of(context).copyWith(scaffoldBackgroundColor: Colors.white),
        child: Center(
          child: RaisedButton(
            child: const Text('Show licenses'),
            onPressed: () => showLicensePage(context: context),
          ),
        ),
      ),
    );
  }
}

我该怎么做?

【问题讨论】:

    标签: flutter flutter-theme


    【解决方案1】:

    在我的例子中,我发现 ThemeData(cardColor) 决定了 LicensePage 背景颜色。所以,

    showLicense(BuildContext context) {
      Navigator.of(context).push(
        MaterialPageRoute<void>(
          builder: (context) => Theme(
            data: ThemeData(
              cardColor: Colors.yellow,
            ),
            child: LicensePage(
              applicationVersion: '0.1',
              applicationIcon: Icon(Icons.person),
              applicationLegalese: 'Legal stuff',
            ),
          ),
        ),
      );
    }
    

    【讨论】:

    • 设置 cardColor 属性是唯一对我有用的方法。谢谢!
    【解决方案2】:

    我想出了这个,它成功了。

    Scaffold(
      body: Center(
        child: RaisedButton(
          child: const Text('Show licenses'),
          onPressed: () => Navigator.of(context).push(
            MaterialPageRoute<void>(
              builder: (context) => Theme(
                data: Theme.of(context).copyWith(
                  scaffoldBackgroundColor: Colors.white,
                ),
                child: LicensePage(...),
              ),
            ),
          ),
        ),
      ),
    )
    

    【讨论】:

    • LicensePage 已更改为使用 cardColor 而不是 scaffoldBackgroundColor 作为背景颜色。见this issuethe related PR
    【解决方案3】:

    这样你不能设置主题,使用Container设置颜色

     Scaffold(
            body: Container(
            color: Colors.white,
              child: Center(
                child: RaisedButton(
                  child: const Text('Show licenses'),
                  onPressed: () => showLicensePage(context: context),
                ),
              ),
            ),
          ),
    

    【讨论】:

    • 哪个脚手架?将其设置为 HomeScreen 的 Scaffold 不起作用,更糟糕的是,它将主屏幕的背景颜色更改为白色,这是我不想要的。
    • @kaboc:编辑了答案,试试吧
    • 你自己试过了吗?这是行不通的。仅供参考,ColoredBox 可以用来代替 Container 在 Flutter 的较新版本中更改小部件的颜色。
    • 我想知道它是如何工作的。 LicensePage 是从 HomeScreen 打开的另一个屏幕,所以我怀疑更改 HomeScreen 中内容的颜色会影响 LicensePage 的背景颜色。
    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 2020-02-27
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2011-11-11
    相关资源
    最近更新 更多