web-dev-qa-db-ja.com

Flutterアプリのテーマをデフォルトでダークに設定するにはどうすればよいですか?

フラッターでシンプルなログインUIを作成しましたが、アプリの全体的なテーマを暗くする方法がわかりません。つまり、将来、アプリに機能を追加した場合、すべて暗いテーマになるはずです。それを行う方法はありますか?

別のDartファイル(login.Dart)を使用しましたが、ログインUIで使用されるすべてのウィジェットはこのファイルにあります。メインDartファイル(main.Dart)でThemeDataを暗く設定しましたが、アプリはまだ明るいテーマで実行されています。

これが私のコードです:

main.Dart

import 'package:flutter/material.Dart';
import 'package:bidder_login/login.Dart';

void main(){
    runApp(
        MaterialApp(
      theme: ThemeData(),
      darkTheme: ThemeData.dark(),
            debugShowCheckedModeBanner: false,
            title: "Basic Login Demo",
            home: LoginPage(),
        ),
    );
}

login.Dart

import 'package:flutter/material.Dart';

class LoginPage extends StatefulWidget {
    @override
    _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
    @override
    Widget build(BuildContext context) {
        return Scaffold(
            body: SafeArea(
                child: ListView(
                    padding: EdgeInsets.symmetric(horizontal: 24.0),
                    children: <Widget>[
                        SizedBox(height: 80.0),
                        // Column(
                        //  children: <Widget>[
                        //      Image.asset('assets/login_app.png'),
                        //      SizedBox(height: 25.0),
                        //      Text("Material Login"),
                        //  ],
                        // ),

                        //*Username starts here
                        SizedBox(height: 120.0),
                        TextField(
                            decoration: InputDecoration(
                                labelText: 'Username',
                                filled: true,
                            ),
                        ),

                        //*Password starts here
                        SizedBox(height: 12.0),
                        TextField(
                            decoration: InputDecoration(
                                labelText: 'Password',
                                filled: true,
                            ),
                            obscureText: true,
                        ),
                        ButtonBar(
                            children: <Widget>[
                                FlatButton(
                                    child: Text('Cancel'),
                                    onPressed: () {

                                    },
                                ),
                                RaisedButton(
                                    child: Text('Next'),
                                    onPressed: () {

                                    },
                                )
                            ],
                        )

                    ],
                ),
            ),
        );
    }
}
2
CodeSadhu

ThemeMode を使用する必要があります

  • themeが使用するMaterialAppについて説明します。

サンプルコード

themeMode: ThemeMode.dark,//Always use the dark mode (if available) regardless of system preference.


themeMode: ThemeMode.light,//Always use the light mode regardless of system preference.


themeMode: ThemeMode.system,//Use either the light or dark theme based on what the user has selected in the system settings.


themeMode: ThemeMode.values,//A constant List of the values in this enum, in order of their declaration.

ThemeModeMaterialAppを使用する方法

MaterialApp(
      debugShowCheckedModeBanner: false,
      theme:
          ThemeData(primarySwatch: Colors.blue, brightness: Brightness.light),
      themeMode: ThemeMode.dark,
      darkTheme: ThemeData(brightness: Brightness.dark),
      home: SafeArea(
          child:Scaffold(

          ) ),
    );
4
Nilesh Rathod