dart 如何在Flutter中将背景色更改为黑色?

qeeaahzv  于 2023-09-28  发布在  Flutter
关注(0)|答案(2)|浏览(104)

我试图改变背景颜色为黑色的应用程序,现在它的黑暗,但不是漆黑。这是我的代码

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData.dark().copyWith(
        colorScheme: const ColorScheme.dark().copyWith(background: Colors.black),
      ),
      title: "Test",
      home: Scaffold(body: Center(child: Text("Test"))),
    );
  }
}

我做错了什么?

3pvhb19x

3pvhb19x1#

将背景颜色更改为黑色,则需要修改ThemeData中scaffoldBackgroundColor的backgroundColor属性。你可以这样做:

class MyApp extends StatelessWidget {
  const MyApp({Key? key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: Colors.black, // Change background color to black
      ),
      title: "Test",
      home: Scaffold(body: Center(child: Text("Test"))),
    );
  }
}
lc8prwob

lc8prwob2#

你正在做的是设置应用程序的配色方案。要更改整个应用程序的背景颜色,您需要编写如下代码。

@override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: const  Color(0xFF000000),
        colorScheme: const ColorScheme.dark().copyWith( background: const Color(0xFF000000)),
      ),
      title: 'Button Types',
      home: const Scaffold(
        body: ButtonTypesExample(),
      ),
    );
  }

相关问题