简体   繁体   English

Flutter - 如何更改 LicensePage 的背景颜色?

[英]Flutter - How can I change the background color of LicensePage?

I'd like to set the background colour of every screen except LicensePage to some colour, so I've specified the scaffoldBackbroundColor via the theme argument of MaterialApp as follows.我想将除LicensePage之外的每个屏幕的背景颜色设置为某种颜色,因此我通过MaterialApptheme参数指定了scaffoldBackbroundColor BackbroundColor,如下所示。

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

This changes the background colour of the licences page too, so in order to change it back to white, I tried overriding scaffoldBackbroundColor , but it did not work.这也改变了许可证页面的背景颜色,所以为了将它改回白色,我尝试覆盖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),
          ),
        ),
      ),
    );
  }
}

How can I do it?我该怎么做?

In my case I found ThemeData(cardColor) was dictating the LicensePage background color.就我而言,我发现 ThemeData(cardColor) 是在指定 LicensePage 背景颜色。 So,所以,

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',
        ),
      ),
    ),
  );
}

I came up with this and it worked successfully.我想出了这个,它成功地工作了。

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(...),
          ),
        ),
      ),
    ),
  ),
)

This way you cannot set the theme, set color using Container这样你就不能设置主题,使用Container设置颜色

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM