如何在Flutter中设置主屏幕的背景色?


88

我正在学习Flutter,并且从最基础的东西开始。我没有使用MaterialApp。设置整个屏幕背景色的好方法是什么?

这是我到目前为止的内容:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new Center(child: new Text("Hello, World!"));
  }
}

我的一些问题是:

  • 设置背景颜色的基本方法是什么?
  • 我在屏幕上到底在看什么?哪个代码是“背景”?有设置背景颜色的东西吗?如果不是,那么什么是简单且适当的“简单背景”(以便绘制背景颜色)。

谢谢您的帮助!

上面的代码生成带有白色文本的黑屏: 在此处输入图片说明

Answers:


76

我认为您也可以使用脚手架来做白色背景。这是一些可能有用的代码。

import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
  @override
    Widget build(BuildContext context) {

      return new MaterialApp(
        title: 'Testing',
        home: new Scaffold(
        //Here you can set what ever background color you need.
          backgroundColor: Colors.white,
        ),
      );
    }
}

希望对您有帮助。


我想为边框提供蓝色,为容器背景颜色提供琥珀色,我该怎么办?
Kamlesh

73

您可以在应用程序中一次将背景色设置为“所有支架”。

只需在ThemeData中设置scaffoldBackgroundColor:

 MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(scaffoldBackgroundColor: const Color(0xFFEFEFEF)),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );

4
这是在所有页面(大多数是脚手架)上具有相同背景颜色的要求。谢谢。
iCrus

1
很好的答案,尤其是当您使用路由和导航时(比从Skaffold创建高阶小部件并在所有顶级小部件上使用它要好得多)。
Nader Ghanbari

42

这是我发现的一种方法。我不知道是否有更好的方法,或者要权衡些什么。

根据https://flutter.io/layout/容器“尝试尽可能大” 。此外,Container可以采用decoration,可以是BoxDecoration,可以采用color(背景颜色)。

这是一个确实在屏幕上用红色填充的示例,并显示“ Hello,World!”。进入中心:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new Container(
      decoration: new BoxDecoration(color: Colors.red),
      child: new Center(
        child: new Text("Hello, World!"),
      ),
    );
  }
}

注意,容器由MyApp build()返回。容器有一个装饰和一个孩子,这是居中的文本。

在这里查看实际操作:

在此处输入图片说明


3
如果您要构建简单的应用程序或不使用Material Design的应用程序,容器是一个不错的选择。如果要构建“材质”应用程序,如果要在所有画布和卡片上使用深色背景,请考虑使用ThemeData.dark()。您还可以使用ThemeData构造函数的cardColor和canvasColor参数来对卡片和画布背景颜色进行精细控制。docs.flutter.io/flutter/material/ThemeData/ThemeData.html
科林·杰克逊

1
如何设置自定义RGB?
Nirav Madariya'4

我想为边框提供蓝色,为容器背景颜色提供琥珀色,我该怎么办?
Kamlesh

我没有使用脚手架,这种解决方案令人难以置信。谢谢。
埃德加·弗洛斯

27

这样做的方法有很多,这里我列出的很少。

  1. 使用 backgroundColor

    Scaffold(
      backgroundColor: Colors.black,
      body: Center(...),
    )
    
  2. Container在中使用SizedBox.expand

    Scaffold(
      body: SizedBox.expand(
        child: Container(
          color: Colors.black,
          child: Center(...)
        ),
      ),
    )
    
  3. 使用 Theme

    Theme(
      data: Theme.of(context).copyWith(scaffoldBackgroundColor: Colors.black),
      child: Scaffold(
        body: Center(...),
      ),
    )
    


4

在Flutter的基本示例中,您可以设置backgroundColor: Colors.XScaffold

  @override
 Widget build(BuildContext context) {
   // This method is rerun every time setState is called, for instance as done
  // by the _incrementCounter method above.
   //
  // The Flutter framework has been optimized to make rerunning build methods
   // fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
  backgroundColor: Colors.blue,
  body: Center(
    // Center is a layout widget. It takes a single child and positions it
    // in the middle of the parent.
    child: Column(
      // Column is also layout widget. It takes a list of children and
      // arranges them vertically. By default, it sizes itself to fit its
      // children horizontally, and tries to be as tall as its parent.
      //
      // Invoke "debug painting" (press "p" in the console, choose the
      // "Toggle Debug Paint" action from the Flutter Inspector in Android
      // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
      // to see the wireframe for each widget.
      //
      // Column has various properties to control how it sizes itself and
      // how it positions its children. Here we use mainAxisAlignment to
      // center the children vertically; the main axis here is the vertical
      // axis because Columns are vertical (the cross axis would be
      // horizontal).
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
          'You have pushed the button this many times:',
        ),
        Text(
          '$_counter',
          style: Theme.of(context).textTheme.display1,
        ),
      ],
    ),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: Icon(Icons.add_circle),
  ), // This trailing comma makes auto-formatting nicer for build methods.
);
}

4

您应该返回 Scaffold小部件并将其添加到Scaffold中

像这样的代码很烂:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Scaffold(
          backgroundColor: Colors.white,
          body: Center(child: new Text("Hello, World!"));
    );
  }
}

2

我认为您需要使用MaterialApp小部件并使用所需的颜色进行theme设置 primarySwatch。看起来像下面的代码,

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

1

这是更改背景颜色的另一种方法:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(backgroundColor: Colors.pink,),);
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.