Search code examples
flutterscaffold

In Flutter, how do you set *all* the scaffold background color at once?


Currently, I set background color of each screen using this:

  @override
  Widget build(BuildContext context) => Scaffold(
    backgroundColor: Colors.white,
    body: ...
  );

Every time I create new screen, I always forgot to add this background color setter. This is a minor inconvenience, but I really appreciate if there's a method to set this background color once for all screens, unless overridden by backgroundColor property of specific Scaffold. I have tried to set the color on MaterialApp's color property, but it doesn't look like it has any effect.


Solution

  • You should pass custom ThemeData with background color parameter overwritten to you MaterialApp, so this will do the trick:

    return MaterialApp(
            // your other app initialization code
            theme: ThemeData(scaffoldBackgroundColor: Colors.white),
        );
    

    You can read more about ThemData and flutter app theming in the official documentation https://flutter.dev/docs/cookbook/design/themes