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

Viewed 1634

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.

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.

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?

3 Answers

In my case I found ThemeData(cardColor) was dictating the LicensePage background color. 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

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