facebook twitter hatena line email

Flutter/UI/ダークモード

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

公式

https://github.com/Norbert515/dynamic_theme

インストール

pubspec.yaml

dependencies:
  dynamic_theme: ^1.0.1

サンプル

import 'package:dynamic_theme/dynamic_theme.dart';class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new DynamicTheme(
      defaultBrightness: Brightness.light,
      data: (brightness) => new ThemeData(
        primarySwatch: Colors.indigo,
        brightness: brightness,
      ),
      themedWidgetBuilder: (context, theme) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: theme,
          home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    );
  }
}

設定切り替え

  void changeBrightness() {
    DynamicTheme.of(context).setBrightness(Theme.of(context).brightness == Brightness.dark? Brightness.light: Brightness.dark);
  }