Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Flutter Layout Basics

Flutter Layout Basics

Layout is a core concept in Flutter UI development.

This section will introduce the most commonly used layout Widgets, including Row, Column, Stack, and Flex.


Layout Widget Overview

Flutter provides a rich set of layout Widgets to meet various interface design needs.

Widget Purpose
Row Arrange child Widgets horizontally
Column Arrange child Widgets vertically
Stack Arrange child Widgets in a stack
Flex Flexible linear layout (base class of Row/Column)
GridView Grid Layout
ListView List layout

Row - Horizontal Layout

Row is used to arrange child Widgets horizontally.

Example: Basic Usage of Row

// Arrange three icons horizontally

Row

(

  children

:

[

    Icon

(

Icons.

home

,

color

:

Colors.

blue

)

,

    Icon

(

Icons.

search

,

color

:

Colors.

green

)

,

    Icon

(

Icons.

settings

,

color

:

Colors.

grey

)

,

 

]

,

)

// Row with spacing

Row

(

  mainAxisAlignment

:

MainAxisAlignment.

spaceEvenly

,

 

// Main axis alignment

  children

:

[

    Icon

(

Icons.

home

,

color

:

Colors.

blue

)

,

    Icon

(

Icons.

search

,

color

:

Colors.

green

)

,

    Icon

(

Icons.

settings

,

color

:

Colors.

grey

)

,

 

]

,

)

mainAxisAlignment - Main Axis Alignment

Value Description
MainAxisAlignment.start Align to the left (default)
MainAxisAlignment.center Center Alignment
MainAxisAlignment.end Align to the right
MainAxisAlignment.spaceBetween Align at both ends, with equal spacing between items
MainAxisAlignment.spaceEvenly Equal spacing between items and at both ends
MainAxisAlignment.spaceAround Equal spacing between items, greater than the spacing at both ends

Column - Vertical Layout

Column is used to arrange child Widgets vertically.

Example: Basic Usage of Column

// Arrange three text widgets vertically

Column

(

  children

:

[

    Text

(

'First line'

,

style

:

TextStyle

(

fontSize

:

20

)

)

,

    Text

(

'Second line'

,

style

:

TextStyle

(

fontSize

:

20

)

)

,

    Text

(

'Third line'

,

style

:

TextStyle

(

fontSize

:

20

)

)

,

 

]

,

)

// Column with spacing and alignment

Column

(

  mainAxisAlignment

:

MainAxisAlignment.

center

,

 

// Center vertically

  crossAxisAlignment

:

CrossAxisAlignment.

start

,

 

// Align to the left horizontally

  children

:

[

    Text

(

'Title'

,

style

:

TextStyle

(

fontSize

:

24

,

fontWeight

:

FontWeight.

bold

)

)

,

   

const

SizedBox

(

height

:

8

)

,

    Text

(

'Content Content Content'

)

,

   

const

SizedBox

(

height

:

8

)

,

    Text

(

'Bottom Description'

)

,

 

]

,

)

crossAxisAlignment - Cross Axis Alignment

Value Description
CrossAxisAlignment.start Align to the top/left
CrossAxisAlignment.center Center Alignment
CrossAxisAlignment.end Align to the bottom/right
CrossAxisAlignment.stretch Stretch to fill the parent

Expanded and Flexible

Expanded and Flexible are used to let child Widgets fill the remaining space.

Example: Usage of Expanded

// Fixed width + adaptive width

Row

(

  children

:

[

   

// Fixed width 100

    Container

(

width

:

100

,

height

:

50

,

color

:

Colors.

red

)

,

   

// Occupy the remaining space

    Expanded

(

      child

:

Container

(

height

:

50

,

color

:

Colors.

green

)

,

   

)

,

   

// Fixed width 50

    Container

(

width

:

50

,

height

:

50

,

color

:

Colors.

blue

)

,

 

]

,

)

// Distribution ratio

Row

(

  children

:

[

   

// Take 1 part

    Expanded

(

      flex

:

1

,

      child

:

Container

(

height

:

50

,

color

:

Colors.

red

)

,

   

)

,

   

// Take 2 parts

    Expanded

(

      flex

:

2

,

      child

:

Container

(

height

:

50

,

color

:

Colors.

green

)

,

   

)

,

   

// Take 1 part

    Expanded

(

      flex

:

1

,

      child

:

Container

(

height

:

50

,

color

:

Colors.

blue

)

,

   

)

,

 

]

,

)


Stack - Stacked Layout

Stack is used to layer child Widgets together, commonly used to achieve effects such as floating elements and layer overlays.

Example: Usage of Stack

// Basic Stack

Stack

(

  children

:

[

   

// Bottom layer: background image

    Container

(

      width

:

200

,

      height

:

200

,

      color

:

Colors.

blue

,

   

)

,

   

// Middle layer: semi-transparent overlay

    Container

(

      width

:

180

,

      height

:

180

,

      color

:

Colors.

white

.

withOpacity

(

0.5

)

,

   

)

,

   

// Top layer: text

   

const

Center

(

      child

:

Text

(

       

'Overlaid Content'

,

        style

:

TextStyle

(

color

:

Colors.

white

,

fontSize

:

24

)

,

     

)

,

   

)

,

 

]

,

)

// Stack with positioning

Stack

(

  children

:

[

    Container

(

color

:

Colors.

grey

[

300

]

)

,

   

// Position to the top-right corner

    Positioned

(

      right

:

10

,

      top

:

10

,

      child

:

Icon

(

Icons.

close

,

color

:

Colors.

red

)

,

   

)

,

   

// Position to the bottom-left corner

    Positioned

(

      left

:

10

,

      bottom

:

10

,

      child

:

Text

(

'Bottom-Left Text'

)

,

   

)

,

 

]

,

)


Padding and Margin

Use Padding and Margin to control the inner and outer spacing of elements.

Example: Padding and Margin

// Use Padding to set inner spacing

Padding

(

  padding

:

const

EdgeInsets.

all

(

16.0

)

,

 

// 16 pixels on all four sides

  child

:

Text

(

'Text with padding'

)

,

)

// Set each side separately

Padding

(

  padding

:

EdgeInsets.

only

(

    left

:

10

,

    right

:

20

,

    top

:

5

,

    bottom

:

5

,

 

)

,

  child

:

Text

(

'Custom padding'

)

,

)

// Use EdgeInsets.symmetric for symmetric spacing

Padding

(

  padding

:

EdgeInsets.

symmetric

(

horizontal

:

16

,

vertical

:

8

)

,

  child

:

Text

(

'Horizontal 16, vertical 8'

)

,

)

// Container combining margin and padding

Container

(

  margin

:

const

EdgeInsets.

all

(

10

)

,

 

// Outer margin

  padding

:

const

EdgeInsets.

all

(

16

)

,

 

// Inner padding

  decoration

:

BoxDecoration

(

    color

:

Colors.

blue

[

100

]

,

    borderRadius

:

BorderRadius.

circular

(

8

)

,

 

)

,

  child

:

Text

(

'Container Content'

)

,

)


Scaffold and AppBar

Scaffold provides the basic page structure of Material Design, and AppBar is the top app bar.

Example: Basic Scaffold Structure

// Material Design page scaffold

Scaffold

(

 

// Top app bar

  appBar

:

AppBar

(

    title

:

const

Text

(

'My App'

)

,

    backgroundColor

:

Colors.

blue

,

    elevation

:

4

,

 

// Shadow depth

    actions

:

[

      IconButton

(

        icon

:

const

Icon

(

Icons.

search

)

,

        onPressed

:

(

)

{

}

,

     

)

,

      IconButton

(

        icon

:

const

Icon

(

Icons.

settings

)

,

        onPressed

:

(

)

{

}

,

     

)

,

   

]

,

 

)

,

 

// Page body

  body

:

const

Center

(

    child

:

Text

(

'Page body content'

)

,

 

)

,

 

// Floating button

  floatingActionButton

:

FloatingActionButton

(

    onPressed

:

(

)

{

}

,

    child

:

const

Icon

(

Icons.

add

)

,

 

)

,

 

// Bottom navigation bar

  bottomNavigationBar

:

BottomNavigationBar

(

    items

:

const

[

      BottomNavigationBarItem

(

icon

:

Icon

(

Icons.

home

)

,

label

:

'Home'

)

,

      BottomNavigationBarItem

(

icon

:

Icon

(

Icons.

search

)

,

label

:

'Search'

)

,

      BottomNavigationBarItem

(

icon

:

Icon

(

Icons.

person

)

,

label

:

'Mine'

)

,

   

]

,

 

)

,

)