1

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

4 Answers 4

7

You can wrap your SizedBox with ColoredBox

ColoredBox(
  color: Colors.cyanAccent,
  child: SizedBox(
      width: 200,
      height: 100,),
),
Sign up to request clarification or add additional context in comments.

Comments

4

You can try ColoredBox widget

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

1 Comment

It doesn't work @md-mehedi-hasan.
2

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.

1 Comment

Thanks for introducing the concept of 'debugPaintSizeEnabled'. Though this was not the solution of changing color of SizedBox, it really helped me to check the scope of the components of widgets. Also, other solutions you gave me was really helpful too. Thanks again for answering my question, and have a nice day!!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.