DEV Community

Willane Paiva
Willane Paiva

Posted on

Flutter: Essential widgets

Gostaria de ler em português? Leia aqui

Flutter builds layout combining widgets, just like lego blocks. In this article, I'm going to present you a few widgets that can build various screens when combined.

Essential Widgets

Text

Text is a widget used to render text(string) in a screen, it can be constant or dynamic. It can customize style, with properties like font, font weight, handle overflow and wrap, and many others.

Constant Text Example

Text('Hello, World');
Enter fullscreen mode Exit fullscreen mode

Dynamic Text

const greeting = (name) => 'Hello, $name';

Text(greeting('Maria'));
Enter fullscreen mode Exit fullscreen mode

Flutter text render with text

Container

Container is a widget with a single child and has multiple properties, like color that can be used to change background color and decoration that allows us to create borders, which can only be used one at time.

Exemple using color property to change background color:

Container(
    color: Colors.red,
  child: Text('Container'),
);
Enter fullscreen mode Exit fullscreen mode

Example using decoration to define background color:

Container(
        decoration: BoxDecoration(
          color: Colors.red,
        ),
        child: const Text('Container'),
);
Enter fullscreen mode Exit fullscreen mode

Render of a red container with text Container

SizedBox

SizedBox is a widget that make it possible to create a widget with fixed dimensions, like width, height or both. Has a optional single child.

SizedBox(height: 6);
Enter fullscreen mode Exit fullscreen mode

Flutter SizedBox Render

Padding

Padding is a widget that creates space inside a widget, just like CSS Padding.

Padding(
    padding: EdgeInsets.all(16),
    child: Text('Widget A'),
);
Enter fullscreen mode Exit fullscreen mode

Flutter Padding Usage Example

Column

Column is a widget that stacks itens vertically. It's a flex widget, so it will take all available space.

Usage Example:

Column(
  children: [
        Container(color: Colors.red),
        Container(color: Colors.blue),
    ],
);
Enter fullscreen mode Exit fullscreen mode

Complete Example:

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 620,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            height: 150,
            alignment: Alignment.center,
            child: const Text('Widget A'),
          ),
          const Divider(
            height: 1,
            color: Colors.black,
          ),
          Container(
            height: 150,
            alignment: Alignment.center,
            child: const Text('Widget B'),
          ),
          const Divider(
            height: 1,
            color: Colors.black,
          ),
          Container(
            height: 150,
            alignment: Alignment.center,
            child: const Text('Widget C'),
          ),
          const Divider(
            height: 1,
            color: Colors.black,
          ),
          Container(
            height: 150,
            alignment: Alignment.center,
            child: const Text('Widget D'),
          ),
          const Divider(
            height: 1,
            color: Colors.black,
          ),
        ],
      ),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Renderização de widget de column

Row

Similar to Column, render itens horizontally.

Row(
  children: [
        Container(color: Colors.red),
        Container(color: Colors.blue),
    ],
);
Enter fullscreen mode Exit fullscreen mode

Complete Example:

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 300,
      width: 600,
      decoration: BoxDecoration(border: Border.all()),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Container(
            width: 150,
            alignment: Alignment.center,
            child: const Text('Widget A'),
          ),
          Container(
            width: 1,
            color: Colors.black,
          ),
          Container(
            width: 150,
            alignment: Alignment.center,
            child: const Text('Widget B'),
          ),
          Container(
            width: 1,
            color: Colors.black,
          ),
          Container(
            width: 150,
            alignment: Alignment.center,
            child: const Text('Widget C'),
          ),
          Container(
            width: 1,
            color: Colors.black,
          ),
        ],
      ),
    );
  }
}


Enter fullscreen mode Exit fullscreen mode

Row render

SingleChildScrolView

SingleChildScrollView creates a scrollable widget with single child, useful when child size is dynamic.

Usage Example:

SingleChildScrollView(
    child: Text('long text here')
);
Enter fullscreen mode Exit fullscreen mode

Complete Example:

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: SizedBox(
        height: MediaQuery.of(context).size.height * 2,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          mainAxisSize: MainAxisSize.min,
          children: [
            Flexible(
              child: Container(
                color: Colors.blue,
                alignment: Alignment.center,
                child: const Text('Widget A'),
              ),
            ),
            Flexible(
              child: Container(
                color: Colors.green,
                alignment: Alignment.center,
                child: const Text('Widget B'),
              ),
            ),
            Flexible(
              child: Container(
                color: Colors.yellow,
                alignment: Alignment.center,
                child: const Text('Widget C'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Enter fullscreen mode Exit fullscreen mode

Flutter SingleChildScrollView render

References

Flutter Documentation

Top comments (0)