├── README.md ├── lib └── main.dart ├── pubspec.yaml └── switchtheme.iml /README.md: -------------------------------------------------------------------------------- 1 | # switchtheme 2 | 3 | A new Flutter project. 4 | 5 | ## Getting Started 6 | 7 | This project is a starting point for a Flutter application. 8 | 9 | A few resources to get you started if this is your first Flutter project: 10 | 11 | - [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) 12 | - [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) 13 | 14 | For help getting started with Flutter, view our 15 | [online documentation](https://flutter.dev/docs), which offers tutorials, 16 | samples, guidance on mobile development, and a full API reference. 17 | -------------------------------------------------------------------------------- /lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/material.dart'; 2 | 3 | void main() { 4 | runApp(MyApp()); 5 | } 6 | 7 | var theme = darkTheme; 8 | 9 | var darkTheme = ThemeData( 10 | primarySwatch: Colors.red, 11 | brightness: Brightness.dark, 12 | visualDensity: VisualDensity.adaptivePlatformDensity, 13 | ); 14 | 15 | var lightTheme = ThemeData( 16 | primarySwatch: Colors.green, 17 | brightness: Brightness.light, 18 | visualDensity: VisualDensity.adaptivePlatformDensity, 19 | ); 20 | 21 | class MyApp extends StatefulWidget { 22 | @override 23 | _MyAppState createState() => _MyAppState(); 24 | } 25 | 26 | class _MyAppState extends State with WidgetsBindingObserver { 27 | @override 28 | void initState() { 29 | super.initState(); 30 | WidgetsBinding.instance.addObserver(this); 31 | changeTheme(); 32 | } 33 | 34 | @override 35 | void dispose() { 36 | WidgetsBinding.instance.removeObserver(this); 37 | super.dispose(); 38 | } 39 | 40 | @override 41 | void didChangePlatformBrightness() { 42 | changeTheme(); 43 | } 44 | 45 | changeTheme() { 46 | var brightness = WidgetsBinding.instance.window.platformBrightness; 47 | brightness == Brightness.dark ? theme = darkTheme : theme = lightTheme; 48 | 49 | setState(() {}); 50 | } 51 | 52 | @override 53 | Widget build(BuildContext context) { 54 | return MaterialApp( 55 | title: 'Flutter Themes', 56 | debugShowCheckedModeBanner: false, 57 | theme: theme, 58 | home: HomePage(), 59 | ); 60 | } 61 | } 62 | 63 | class HomePage extends StatelessWidget { 64 | @override 65 | Widget build(BuildContext context) { 66 | return Scaffold( 67 | appBar: AppBar( 68 | title: Text("Flutter Themes"), 69 | ), 70 | body: Center( 71 | child: Text("Hello World"), 72 | ), 73 | ); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: switchtheme 2 | description: A new Flutter project. 3 | 4 | # The following line prevents the package from being accidentally published to 5 | # pub.dev using `pub publish`. This is preferred for private packages. 6 | publish_to: 'none' # Remove this line if you wish to publish to pub.dev 7 | 8 | # The following defines the version and build number for your application. 9 | # A version number is three numbers separated by dots, like 1.2.43 10 | # followed by an optional build number separated by a +. 11 | # Both the version and the builder number may be overridden in flutter 12 | # build by specifying --build-name and --build-number, respectively. 13 | # In Android, build-name is used as versionName while build-number used as versionCode. 14 | # Read more about Android versioning at https://developer.android.com/studio/publish/versioning 15 | # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. 16 | # Read more about iOS versioning at 17 | # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html 18 | version: 1.0.0+1 19 | 20 | environment: 21 | sdk: ">=2.7.0 <3.0.0" 22 | 23 | dependencies: 24 | flutter: 25 | sdk: flutter 26 | 27 | 28 | # The following adds the Cupertino Icons font to your application. 29 | # Use with the CupertinoIcons class for iOS style icons. 30 | cupertino_icons: ^0.1.3 31 | 32 | dev_dependencies: 33 | flutter_test: 34 | sdk: flutter 35 | 36 | # For information on the generic Dart part of this file, see the 37 | # following page: https://dart.dev/tools/pub/pubspec 38 | 39 | # The following section is specific to Flutter. 40 | flutter: 41 | 42 | # The following line ensures that the Material Icons font is 43 | # included with your application, so that you can use the icons in 44 | # the material Icons class. 45 | uses-material-design: true 46 | 47 | # To add assets to your application, add an assets section, like this: 48 | # assets: 49 | # - images/a_dot_burr.jpeg 50 | # - images/a_dot_ham.jpeg 51 | 52 | # An image asset can refer to one or more resolution-specific "variants", see 53 | # https://flutter.dev/assets-and-images/#resolution-aware. 54 | 55 | # For details regarding adding assets from package dependencies, see 56 | # https://flutter.dev/assets-and-images/#from-packages 57 | 58 | # To add custom fonts to your application, add a fonts section here, 59 | # in this "flutter" section. Each entry in this list should have a 60 | # "family" key with the font family name, and a "fonts" key with a 61 | # list giving the asset and other descriptors for the font. For 62 | # example: 63 | # fonts: 64 | # - family: Schyler 65 | # fonts: 66 | # - asset: fonts/Schyler-Regular.ttf 67 | # - asset: fonts/Schyler-Italic.ttf 68 | # style: italic 69 | # - family: Trajan Pro 70 | # fonts: 71 | # - asset: fonts/TrajanPro.ttf 72 | # - asset: fonts/TrajanPro_Bold.ttf 73 | # weight: 700 74 | # 75 | # For details regarding fonts from package dependencies, 76 | # see https://flutter.dev/custom-fonts/#from-packages 77 | -------------------------------------------------------------------------------- /switchtheme.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | --------------------------------------------------------------------------------