如何将Flutter应用程序主题默认设置为黑暗?

时间:2019-12-24 06:12:51

标签: android flutter dart flutter-theme

我已经在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 个答案:

答案 0 :(得分:3)

您需要使用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.
  

如何在ThemeMode中使用MaterialApp

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

          ) ),
    );

答案 1 :(得分:0)

推荐的方法是使用ColorScheme

var mode = ThemeMode.light; // or ThemeMode.dark

MaterialApp(
  theme: ThemeData.from(colorScheme: ColorScheme.light()),
  darkTheme: ThemeData.from(colorScheme: ColorScheme.dark()),
  themeMode: mode,
  home: //...
)