简体   繁体   English

我想更改 flutter 应用程序的背景颜色?

[英]I want to change the background color of a flutter app?

I just want to change the background color of this Flutter application.我只是想改变这个 Flutter 应用程序的背景颜色。

Please let me know how to add a picture as background.请让我知道如何添加图片作为背景。 And how to change the opacity of that image?以及如何改变该图像的不透明度?

import 'package:flutter/material.dart';

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

class Loginscreen extends StatelessWidget {
  static const routeName = '/login';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          //title: Text('LOG IN'),
          elevation: 0,
          leading: IconButton(
            icon: Icon(Icons.arrow_back_ios),
            onPressed: () {},
          ),
        ),
        body: Container(
            padding: const EdgeInsets.all(15),
            color: Theme.of(context).primaryColor,
            width: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                TextField(
                  style: TextStyle(fontSize: 18, color: Colors.black54),
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.white,
                    hintText: 'User Name',
                    contentPadding: const EdgeInsets.all(15),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.white),
                      borderRadius: BorderRadius.circular(5),
                    ),
                    enabledBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.white),
                      borderRadius: BorderRadius.circular(5),
                    ),
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                TextField(
                  obscureText: true,
                  style: TextStyle(fontSize: 18, color: Colors.black54),
                  decoration: InputDecoration(
                    filled: true,
                    fillColor: Colors.white,
                    hintText: 'Password',
                    contentPadding: const EdgeInsets.all(15),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.white),
                      borderRadius: BorderRadius.circular(5),
                    ),
                    enabledBorder: UnderlineInputBorder(
                      borderSide: BorderSide(color: Colors.white),
                      borderRadius: BorderRadius.circular(5),
                    ),
                  ),
                ),
                SizedBox(
                  height: 20,
                ),
                FlatButton(
                  onPressed: null,
                  child: Text(
                    'Log In',
                    style: TextStyle(
                      fontSize: 20,
                    ),
                  ),
                  shape: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.white, width: 2),
                    borderRadius: BorderRadius.circular(5),
                  ),
                  padding: const EdgeInsets.all(15),
                  textColor: Colors.white,
                ),
              ],
            )),
      ),
    );
  }
}

You can set the background color in your Scaffold您可以在Scaffold中设置背景颜色

For example例如

Scaffold(
   backgroundColor: Colors.pink,
   appBar: AppBar(),
)

Change background color更改背景颜色

Depends on the root, you can use for example:取决于根目录,您可以使用例如:

  • backgroundColor when Scaffold is parent Scaffold为父级时的backgroundColor颜色

  • color when Container is parent Container为父级时的color

So所以

Scaffold(
  backgroundColor: Colors.blueAccent,
);

or或者

Container(
  color: Colors.blueAccent,
);

depends where you need background color.取决于您需要背景颜色的位置。

Set image as background将图像设置为背景

To add "something" as background you have to wrap it in to Stack (which has list of childrens like Column or Row )要将“某物”添加为背景,您必须将其包装到Stack (其中包含ColumnRow等子项列表)

This class is useful if you want to overlap several children in a simple way, for example having some text and an image, overlaid with a gradient and a button attached to the bottom.如果您想以简单的方式重叠多个孩子,例如有一些文本和图像,覆盖有渐变和附加到底部的按钮,则此 class 非常有用。

Change from:更改自:

Container(
    child: Text("Example text"),
),

to:至:

Stack(
  children: <Widget>[
    Image.asset("assets/background_image.jpg"),
    Container(
      child: Text("Example text"),
    ),
  ],
),

Set opacity of the widget设置小部件的不透明度

When you need to change opacity for example this view:当您需要更改不透明度时,例如此视图:

Image.asset("assets/background_image.jpg")

you have to wrap it with Opacity widget:你必须用Opacity小部件包装它:

Opacity(
  opacity: 0.5,
  child: Image.asset("assets/background_image.jpg"),
),

And set opacity which is range from 0.0 to 1.0, where:并设置opacity ,范围从 0.0 到 1.0,其中:

An opacity of 1.0 is fully opaque (100% visibility)不透明度 1.0 完全不透明(100% 可见性)

An opacity of 0.5 is half opaque (50% visibility)不透明度 0.5 表示半不透明(50% 可见性)

An opacity of 0.0 is fully transparent (0% visibility = NOT visible) 0.0 的不透明度是完全透明的(0% 可见性 = 不可见)

Full example完整示例

Stack(
  children: <Widget>[
    Opacity(
      opacity: 0.5,
      child: Image.asset("assets/background_image.jpg"),
    ),
    Container(
      child: Text("Example text"),
    ),
  ],
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM