[英]How do I set the background color of my main screen in Flutter?
I'm learning Flutter, and I'm starting from the very basics.我正在学习 Flutter,我是从最基础的开始。 I'm not using MaterialApp.
我没有使用 MaterialApp。 What's a good way to set the background color of the whole screen?
设置整个屏幕的背景颜色有什么好的方法吗?
Here's what I have so far:这是我到目前为止所拥有的:
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!"));
}
}
Some of my questions are:我的一些问题是:
Thanks for the help!谢谢您的帮助!
The code above generates a black screen with white text:上面的代码生成一个带有白色文本的黑屏:
You can set background color to All Scaffolds in application at once.您可以一次将背景颜色设置为应用程序中的所有脚手架。
Just set scaffoldBackgroundColor:
in ThemeData
:只需在
ThemeData
中设置scaffoldBackgroundColor:
:
MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(scaffoldBackgroundColor: const Color(0xFFEFEFEF)),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
I think you can also use a scaffold to do the white background.我认为您也可以使用脚手架来做白色背景。 Here's some piece of code that may help.
这是一些可能有帮助的代码。
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,
),
);
}
}
Hope this helps 😊.希望这会有所帮助😊。
Here's one way that I found to do it.这是我发现的一种方法。 I don't know if there are better ways, or what the trade-offs are.
我不知道是否有更好的方法,或者取舍是什么。
Container "tries to be as big as possible", according to https://flutter.io/layout/ .根据https://flutter.io/layout/的说法,容器“试图尽可能大”。 Also, Container can take a
decoration
, which can be a BoxDecoration , which can have a color
(which, is the background color).此外, Container 可以接受一个
decoration
,它可以是一个BoxDecoration ,它可以有一个color
(也就是背景颜色)。
Here's a sample that does indeed fill the screen with red, and puts "Hello, World!"这是一个确实用红色填充屏幕并显示“Hello, World!”的示例。 into the center:
进入中心:
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!"),
),
);
}
}
Note, the Container is returned by the MyApp build().请注意,容器由 MyApp build() 返回。 The Container has a decoration and a child, which is the centered text.
Container 有一个装饰和一个子元素,它是居中的文本。
See it in action here:在这里查看它的实际效果:
There are many ways of doing it, I am listing few here.有很多方法,我在这里列出几个。
Using backgroundColor
使用
backgroundColor
Scaffold( backgroundColor: Colors.black, body: Center(...), )
Using Container
in SizedBox.expand
在
SizedBox.expand
中使用Container
Scaffold( body: SizedBox.expand( child: Container( color: Colors.black, child: Center(...) ), ), )
Using Theme
使用
Theme
Theme( data: Theme.of(context).copyWith(scaffoldBackgroundColor: Colors.black), child: Scaffold( body: Center(...), ), )
Scaffold(
backgroundColor: Constants.defaulBackground,
body: new Container(
child: Center(yourtext)
)
)
You should return Scaffold widget and add your widget inside Scaffold您应该返回Scaffold小部件并将您的小部件添加到Scaffold
Such as this code:比如这段代码:
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!"));
);
}
}
It's another approach to change the color of background:这是改变背景颜色的另一种方法:
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,),);
}
}
On the basic example of Flutter you can set with backgroundColor: Colors.X
of Scaffold在 Flutter 的基本示例中,您可以使用
backgroundColor: Colors.X
of Scaffold 进行设置
@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.
);
}
I think you need to use MaterialApp
widget and use theme
and set primarySwatch
with color that you want.我认为您需要使用
MaterialApp
小部件并使用theme
并将primarySwatch
设置为您想要的颜色。 look like below code,看起来像下面的代码,
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'),
);
}
}
home: Scaffold(
backgroundColor: Color(
0xBF453F3F),
and done :)并做了 :)
You can just put the six digit hexa after (0xFF**......**):您可以将六位十六进制数放在 (0xFF**......**) 之后:
return Scaffold(
backgroundColor: const Color(0xFFE9ECEF),
.....) } )
Sample code:示例代码:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Sample App'),
backgroundColor: Colors.amber, // changing Appbar back color
),
backgroundColor: Colors.blue, // changing body back color
),
),
);
}
As sirelon suggested, add scaffold color in the theme like this,正如 sirelon 建议的那样,在主题中添加脚手架颜色,如下所示,
theme: new ThemeData(scaffoldBackgroundColor: const Color(0xFFEFEFEF)),
or can give color to individual scaffold like this或者可以像这样给单个脚手架上色
Scaffold(
backgroundColor: Color(0xFFF1F1F1),
...
);
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Your App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.black,
),
home HomeScreen(),
);
}
}
Try the following code:试试下面的代码:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
scaffoldBackgroundColor: Colors.white, // Change the background color of all Scaffold widgets of your app here
),
home: const Scaffold(
body: Center(child: Text("Hello, World!")),
backgroundColor: Colors.white, // Change the background color of this Scaffold widget here
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.