📄
FlutterCheatSheet
  • About
  • Flutter web
    • Download in flutter web
    • Fluter run web selain chrome
    • Mengatasi CORS
  • My Note
    • Cara mudah setting Firebase
    • solusi text overflow
    • Setting Firebase Mobile
    • Setting MinSDK Android
    • Run build runner
    • Load Json FromAsset
    • Set Height TabBar
    • WillPopScope
    • Dropdown with null
    • Interview Flutter
    • shortcut android studio
    • Build with different main.dart
    • Validating email
    • search delay
    • Hive
    • penggunaan sort maupun func lain
    • incompatible version of Kotlin.
    • Google Signin error
    • add Map to Map
    • Cors Anywhere
    • run scrcpy simple
    • android studio adb server version (41) doesn't match this client (39); killing
    • Connect adb with hotspot
    • Bloc , intinya...
    • Restorable state
    • Custom Theme DatePicker Flutter
    • membungkus widget dengan tema
    • All About Extention
    • Provider watch read
    • Provider , App works in debug mode but does not work in release mode
    • make extention
    • Sliver app bar
    • Custom Error screen
    • Rename Package
    • error build on AndroidStudio from vscode
    • add tooltip to all widget
    • Textformfield validator
    • Membuat String Rupiah
    • Sort List Dart
    • chart
    • membuat Date time lokal
    • Change default PageTransition
    • Show Hide Password
    • Error UserFriendly
    • Responsive adaptive layout
    • Tips SingleScrollingView
    • Build context
    • FittedBox
    • validation
      • Dropdown validation
    • alertDialog
      • Mixin
      • FocusNode
      • WebView url listener
      • tips statefull pada alertdialog
      • run-code-after-showdialog-is-dismissed
    • Set package in VS Code when creating a project
  • Usefull source
    • Setting JAVA_HOME
    • Tools for Flutter
    • WEBVIEW
    • membuat Marque
    • LazyLoad
    • Tips
      • Penggunaan Const
      • 3 Dots
      • How Disable all Widget
    • List article
    • List Web Complete Tutorial
    • List library
    • Parsing nested Json
    • Future
      • Tips Clean Code
      • Custom Future
      • Chaining Future
      • Catch error
    • Flutter Sample HTTP Request Github with https://reqres.in/
    • Const vs Final
    • Constructor
    • Function
    • Font
    • CRUD
    • Key
    • Free bootcamp
    • State Manajement
      • Provider
  • Widgets
    • Top 5 Box widget
    • Framework library
      • GetFlutter
      • FLUI
    • kumpulan widget
      • Spacer
      • ClipRRect
      • Switch
      • FittedBox
      • List dari Model
      • Dropdown
        • DropDown Multi
    • Deep Dive
      • Button
      • Card
      • Flexible and Expanded
      • Layout Builder
  • Interview Question
    • Kumpulan Pertannyaan Interview
      • Interview Question link Flutter
      • Interview Question With Answer
  • kasus dari stackoverflow
    • Text Red in Hero
    • firebase issued
    • Untitled
  • library
    • Rapido
    • autoscroll text
Powered by GitBook
On this page
  • The solution -
  • 2. FittedBox
  • When to use
  • 3. FractionallySizedBox
  • When to use
  • 4. LimitedBox
  • When to use
  • 5. SizedBox
  • When to use

Was this helpful?

  1. Widgets

Top 5 Box widget

PreviousProviderNextFramework library

Last updated 5 years ago

Was this helpful?

sumber


  height: 50,
  child: Icon(
    Icons.add,
    size: 80,
    color: Colors.red,
  ),
)

The solution -

Container(
  child: ConstrainedBox(
    constraints: BoxConstraints(minHeight: 50),
    child: Icon(
      Icons.add,
      size: 80,
      color: Colors.red,
    ),
  ),
)

Even though the minimum height is 50, The container will try to be as big as its child since we have not given maxHeight property which is by default double.infinity

2. FittedBox

It allows us to scale and fit the child inside.

When to use

Whenever you want to show child inside parent and for you showing a full child is more important (even though it gets scales down) than the child being clipped off.

Problem

Let's say we have to show a full long text in a row.

The below code does not match our expectations.

Row(children: [
  Expanded(
    child: Text(
      "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.",
      maxLines: 1,
      style: TextStyle(fontSize: 23),
    ),
  ),
])

The solution -

Row(children: [
  Expanded(
    child: FittedBox(
      child: Text(
        "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.",
        maxLines: 1,
        style: TextStyle(fontSize: 23),
      ),
    ),
  ),
])

Even though the font size is given as 23, It is ignored since it is inside FittedBox now.

3. FractionallySizedBox

It allows us to set the size of the child in percentage by setting a fraction of the total available space to width and height.

When to use

Whenever you want to use a container and want to set its size in percentage then go for this. I know that it can also be set using Mediaquery but this is also a dedicated widget just for this.

Basic syntax —

FractionallySizedBox(
          widthFactor: 0.5,
          heightFactor: 0.1,
          child: Container(
            decoration: BoxDecoration(color: Colors.greenAccent),
          ),
        )

This would create a box with a width as 50% of horizontal space and height as 10% of vertical space.

4. LimitedBox

It allows us to give the size of the child in an unconstrained environment.

When to use

Whenever you have any child inside any scrollable widget and its size is not specified.

Problem

The below code won’t show anything on the screen because the container inside ListView doesn’t have its size specified.

ListView.builder(
    padding: const EdgeInsets.all(8),
    itemCount: 10,
    itemBuilder: (BuildContext context, int index) {
      return Container(
        color: Colors.greenAccent,
      );
    })

The solution

ListView.builder(
    padding: const EdgeInsets.all(8),
    itemCount: 10,
    itemBuilder: (BuildContext context, int index) {
      return LimitedBox(
        maxHeight: 50,
        child: Container(
          color: Colors.greenAccent,
        ),
      );
    })

Wrapping the container inside LimitedBox with maxHeight will start showing it.

5. SizedBox

It allows us to create a box with a fixed size. Pretty simple.

When to use

It is mostly used when we need to create some empty space between two widgets.

Basic syntax —

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    Container(
      height: 20,
      color: Colors.greenAccent,
    ),
    SizedBox(
      height: 10,
    ),
    Container(
      height: 20,
      color: Colors.greenAccent,
    )
  ],
)

That’s it. Thanks for reading.

Portrait layout —

Landscape layout —

Top 5 Flutter Box Widgets You Should KnowMedium
Logo