颤音:如何彩色盒子(不使用容器)

发布于 2025-01-23 14:41:09 字数 1272 浏览 0 评论 0原文

1。说明

我想更改“ sizedbox”的完整颜色,以查看“ sizedbox”的位置和范围。我想知道有什么方法可以在不用容器或装饰盒填充的情况下更改Sizedbox的颜色。如果没有,我想知道如何用装饰盒填充大小写。

2。代码

这是我尝试的代码。

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home : Scaffold(
        appBar: AppBar(
          title : const Text('this is title'),
        ),
        body : const SizedBox(
          width : 200,
          child: DecoratedBox(
              decoration: BoxDecoration(
                color : Colors.red,
              ),
          ),
        ),
        bottomNavigationBar: (
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: const [
              Icon(Icons.favorite),
              Icon(Icons.home),
              Icon(Icons.settings)
            ],
          )
        ),
      ),
    );
  }
}

** 3。结果**

这是我从代码中得到的。

1. Explanation

I want to change full color of 'SizedBox' to see the location and scope of 'SizedBox'. I wonder is there any way to change SizedBox's color without filling it with Container or Decoration box. If not, I want to know how to fill SizedBox with decoration box.

2. Code

Here is a code that I tried.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home : Scaffold(
        appBar: AppBar(
          title : const Text('this is title'),
        ),
        body : const SizedBox(
          width : 200,
          child: DecoratedBox(
              decoration: BoxDecoration(
                color : Colors.red,
              ),
          ),
        ),
        bottomNavigationBar: (
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: const [
              Icon(Icons.favorite),
              Icon(Icons.home),
              Icon(Icons.settings)
            ],
          )
        ),
      ),
    );
  }
}

**3. Result **

Here is what I got from the code.

result of the code above

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧梦荧光笔 2025-01-30 14:41:09

您可以用coloredbox包装sizedbox

ColoredBox(
  color: Colors.cyanAccent,
  child: SizedBox(
      width: 200,
      height: 100,),
),

You can wrap your SizedBox with ColoredBox

ColoredBox(
  color: Colors.cyanAccent,
  child: SizedBox(
      width: 200,
      height: 100,),
),
哑剧 2025-01-30 14:41:09

您可以尝试 coloredBox widget

SizedBox(
  width : 200,
  height: 20,
  child: ColoredBox(color: Colors.amber),
),

You can try ColoredBox widget

SizedBox(
  width : 200,
  height: 20,
  child: ColoredBox(color: Colors.amber),
),
莫言歌 2025-01-30 14:41:09

如果您想装饰sizedbox以查看widget仅出于调试目的的位置和范围,我们可以启用debugpaintsizeEnabled仅仅通过在启动flutter Run命令时,在CLI上按p

否则,使用具有显式大小参数的容器,它与使用sizedbox 的主要相同,并且它可以使用背景颜色或边框装饰。

另一个选项是将coloredbox用作sizedbox的孩子,反之亦然。

If you'd like to decorate the SizedBox to see the location and scope of the Widget just for debugging purposes, we can enable the debugPaintSizeEnabled just by pressing p on the CLI upon launching the flutter run command.

Otherwise, using a Container with explicit size parameters it's mostly the same as using SizedBox, and it would allow you to use have a background color or border decoration.

Another option would be to use a ColoredBox as the child of the SizedBox, or vice versa.

浅听莫相离 2025-01-30 14:41:09

我不确定您为什么要使用它,但是您可以让Sizidbox的孩子成为带有所需颜色的容器。容器还将延伸到提供的尺寸,

SizedBox(
        height: 10,
        width: 30,
        child: Container(color: Colors.red), 
     ),

您还可以使用Flutter提供的小部件检查器:

https://docs.flutter.dev/develiment/toolment/tools/tools/devtools/devtools/devtools/inspector#:teptor#:text:text= the %20flutter%20widget%20 Inspector%20IS,%2C%20ROWS%2C%20和%20columns )。

I'm not sure why you'd use it but you could have the child of the SizedBox be a container with the color you want. The container will stretch to the sizes provided

SizedBox(
        height: 10,
        width: 30,
        child: Container(color: Colors.red), 
     ),

Also you could use the widget inspector provided by flutter:

https://docs.flutter.dev/development/tools/devtools/inspector#:~:text=The%20Flutter%20widget%20inspector%20is,%2C%20rows%2C%20and%20columns).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文