繁体   English   中英

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

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

我想实现这一点:

在此处输入图像描述

我得到了这个:

在此处输入图像描述

这是我的代码:

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),

              )),

        ),
      ),

    );

  }
}

我试过 fit:BoxFit.cover 但它不起作用

那么,如何解决这个问题呢? 先感谢您

您也可以为 Scaffold 提供背景颜色。 试试下面的代码。

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
      ],
    ),
  ),

去掉Center widget,也可以直接设置Scaffold的背景颜色

您还可以在脚手架正下方使用 Column 并将主轴和交叉轴对齐设置为居中

但是要实现您发布的设计,您可能应该使用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),
        ),
      ),
    ),
  ),

您必须仅将 backgroundColor 提供给 Scaffold:-

backgroundColor: Color(0xffF24004)

暂无
暂无

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

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