Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create colour box with fixed width and height in flutter?

I am trying to create a color box with fixed width and height in a flutter. How to achieve this?

like image 629
Tree Avatar asked Feb 18 '18 23:02

Tree


People also ask

How do I set the box size in flutter?

SizedBox is a built-in widget in flutter SDK. It is a simple box with a specified size. It can be used to set size constraints to the child widget, put an empty SizedBox between the two widgets to get some space in between, or something else. It is somewhat similar to a Container widget with fewer properties.

How do you change the box color in flutter?

Here's how you do it: Step 1: Locate the MaterialApp widget. Step 2: Inside the MaterialApp, add the theme parameter with ThemeData class assigned. Step 3: Inside the ThemeData add the inputDecorationTheme parameter and then assign the InputDecorationTheme .

Can we give color to column in flutter?

You can provide the color to your widget based on the column name in the buildRow method with the help of datagridRowAdapter.

Which of the following helps you to set a specific width and or height between widgets in flutter?

A better approach is using Container widget. When you need to change width, height or add padding/margin to any widget you should wrap the target widget into a container. The container widget is for this kind of job.


2 Answers

Wrap any widget in a SizedBox to force it to match a fixed size.

As for background colors or border, use DecoratedBox.

You can then combine both, which leads to

const SizedBox(
  width: 42.0,
  height: 42.0,
  child: const DecoratedBox(
    decoration: const BoxDecoration(
      color: Colors.red
    ),
  ),
),

You may as well use Container which is a composition of many widgets including those two from above. Which leads to :

new Container(
  height: 42.0,
  width: 42.0,
  color: Colors.red,
)

I tend to prefer the first option. Because Container prevents the use of 'const' constructor. But both works and do the same.

like image 100
Rémi Rousselet Avatar answered Oct 14 '22 03:10

Rémi Rousselet


Taking forward the issue arising in @Rémi Rousselet answer

Flutter’s layout engine has a few important limitations:

  • A widget can decide its own size only within the constraints given to it by its parent. This means a widget usually can’t have any size it wants.

  • A widget can’t know and doesn’t decide its own position in the screen, since it’s the widget’s parent who decides the position of the widget.

  • Since the parent’s size and position, in its turn, also depends on its own parent, it’s impossible to precisely define the size and position of any widget without taking into consideration the tree as a whole.

  • If a child wants a different size from its parent and the parent doesn’t have enough information to align it, then the child’s size might be ignored. Be specific when defining alignment.

The examples are explained in the following sections.

Container(width: 100, height: 100, color: Colors.red) 

The red Container wants to be 100 × 100, but it can’t, because the screen forces it to be exactly the same size as the screen.

So the Container fills the screen.

Solution

wrapped your fixed-sized box with Center

Center(    child: Container(width: 100, height: 100, color: Colors.red) ) 

The screen forces the Center to be exactly the same size as the screen, so the Center fills the screen.

The Center tells the Container that it can be any size it wants, but not bigger than the screen. Now the Container can indeed be 100 × 100.

like image 32
Paresh Mangukiya Avatar answered Sep 16 '22 13:09

Paresh Mangukiya