简体   繁体   English

如何在 flutter 中设置圆形背景颜色?

[英]How to set a backgroud color in flutter with a circle shape?

I want to achieve this:我想实现这一点:

在此处输入图像描述

and I got this:我得到了这个:

在此处输入图像描述

This is my code:这是我的代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(

          decoration:  BoxDecoration
            (color:  Color(0xffF24004),

          ),
          child: Container(
            width: 202,
            height: 196,
              margin: EdgeInsets.all(100.0),
              decoration: BoxDecoration(
                shape: BoxShape.circle, color : Color(0xffF2B749),

              )),

        ),
      ),

    );

  }
}

I tried fit:BoxFit.cover but it doesn't work我试过 fit:BoxFit.cover 但它不起作用

So, how t solve this issue?那么,如何解决这个问题呢? Thank you in advance先感谢您

You can give backgroundcolor to the Scaffold as well.您也可以为 Scaffold 提供背景颜色。 Try out this below code.试试下面的代码。

Scaffold(
    backgroundColor: Color(0xffF24004),
    body: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Container(
          width: 202,
          height: 196,
          margin: EdgeInsets.all(100.0),
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: Color(0xffF2B749),
          ),
        ),
        // Add Another Icon Here
      ],
    ),
  ),

Remove the Center widget, you can also set the background color of the Scaffold directly去掉Center widget,也可以直接设置Scaffold的背景颜色

You can also use a Column right under the scaffold and set main, and cross axis alignments to center您还可以在脚手架正下方使用 Column 并将主轴和交叉轴对齐设置为居中

But to achieve the design you posted you should probably use a Stack widget, coupled with Positioned widgets但是要实现您发布的设计,您可能应该使用Stack小部件,再加上Positioned小部件

Scaffold(
    backgroundColor: Color(0xffF24004),
    body: Center(
      child: Container(
        width: 202,
        height: 196,
        margin: EdgeInsets.all(100.0),
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          color: Color(0xffF2B749),
        ),
      ),
    ),
  ),

You have to give backgroundColor to Scaffold only:-您必须仅将 backgroundColor 提供给 Scaffold:-

backgroundColor: Color(0xffF24004)

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

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