flutter
/

Flutter Flexible Widget Tutorial for Beginners

Last Sync: Today

On this page

7
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

flutter

Flutter Flexible Widget Tutorial for Beginners

What is Flexible in Flutter?

Flexible is a widget that controls how a child of a Row, Column, or Flex flexes. It allows the child to share the available space along the main axis, but unlike Expanded, it does not force the child to fill the space. Instead, the child can be smaller than the available space (if fit is FlexFit.loose). Flexible is the base class for Expanded.

Basic Usage

Wrap any child with Flexible inside a Row or Column. Provide a flex factor to control the proportion of space, and optionally set fit to FlexFit.tight (forces fill) or FlexFit.loose (allows the child to be smaller).

DARTRead-only
1
Row(
  children: [
    Flexible(
      flex: 1,
      fit: FlexFit.loose,
      child: Container(color: Colors.red, height: 50),
    ),
    Flexible(
      flex: 2,
      fit: FlexFit.tight,
      child: Container(color: Colors.green, height: 50),
    ),
  ],
)

Flexible vs Expanded

Expanded is simply Flexible(fit: FlexFit.tight). The key difference:

  • Expanded forces its child to fill the remaining space.
  • Flexible (with FlexFit.loose) allows the child to be its natural size, but still gives it a share of the space.
DARTRead-only
1
Row(
  children: [
    Flexible(
      fit: FlexFit.loose,
      child: Container(color: Colors.yellow, child: Text('Small')),
    ),
    Expanded(
      child: Container(color: Colors.blue),
    ),
  ],
)

Key Properties

  • flex (default 1): The flex factor to use when distributing space.
  • fit: Either FlexFit.tight (force fill) or FlexFit.loose (allow smaller).
  • child: The widget being flexed.

Common Use Cases

  • Allowing a widget to take available space but not stretch if its content is small.
  • Creating responsive layouts where some items have natural widths.
  • Combining with Expanded to mix fixed and flexible sizing.

Key Points to Remember

  • Flexible must be a direct child of a flex widget (Row, Column, Flex).
  • Use FlexFit.loose when you want the child to have a say in its size.
  • Use FlexFit.tight (or Expanded) when you want the child to fill the space.
  • The flex property distributes space proportionally among all flex children.

Common Interview Questions

  1. What is the difference between Flexible and Expanded?
  2. How does FlexFit.loose behave? Give an example.
  3. Can you use Flexible with a flex of 0? What happens?
  4. What happens if you put two Flexible widgets with different fit values in the same Row?

Try it yourself

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Flexible Example')),
        body: Center(
          child: Container(
            height: 100,
            color: Colors.grey[300],
            child: Row(
              children: [
                Flexible(
                  flex: 1,
                  fit: FlexFit.loose,
                  child: Container(color: Colors.red, child: Center(child: Text('A'))),
                ),
                Flexible(
                  flex: 2,
                  fit: FlexFit.tight,
                  child: Container(color: Colors.green, child: Center(child: Text('B'))),
                ),
                Flexible(
                  flex: 1,
                  fit: FlexFit.loose,
                  child: Container(color: Colors.blue, child: Center(child: Text('C'))),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Test Your Knowledge

Q1
of 2

What does `FlexFit.loose` do?

A
Forces the child to fill all available space
B
Allows the child to be smaller than the available space
C
Makes the child take exactly its intrinsic size
D
Ignores the flex factor
Q2
of 2

Which widget is equivalent to `Flexible(fit: FlexFit.tight)`?

A
Container
B
Expanded
C
SizedBox
D
Spacer

Previous

flutter card

Next

flutter sizedbox widget

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.