how to implement dark mode in flutter

后端 未结 6 2273
终归单人心
终归单人心 2021-02-05 09:05

I want to create a flutter app that has 2 light and dark mode themes that change by a switch in-app and the default theme is default android theme.
I need to pass some custo

6条回答
  •  忘掉有多难
    2021-02-05 09:25

    MaterialApp(
      theme: ThemeData.light(),
      /// theme: ThemeData.dark(),
    )
    

    Down the widget tree, you can access ThemeData simply by writing Theme.of(context). If you want to access the current ThemeData and provide your own styling for certain field, you can do for an instance:

    Widget build(BuildContext context) {
      var themeData = Theme.of(context).copyWith(scaffoldBackgroundColor: darkBlue)
    
      return Scaffold(
        backgroundColor = themeData.scaffoldBackgroundColor,
      );
    }
    

    But to handle the ThemeData state (changing its value), you need to implement proper state management.

提交回复
热议问题