SizedBox in Flutter

18-Sep-2024

Learn how to use SizedBox to control spacing and layout between widgets in Flutter

SizedBox widget is used to constrain the size of its child widget. It allows you to set specific dimensions, such as width and height, for the child widget.


There Are Some Common Attributes for Stack

SizedBox.expand
Widget ShowFull Screen
width
Widget width
height
Widget height


Here's a simple example of using a SizedBox in Flutter:

File open lib->main.dart file −




import 'package:flutter/material.dart';

void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('SizedBox Example'),
),
body: MyApp(),
),
)
);
}

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return

Center(
child: SizedBox(
width: 200,
height: 100,
child: Container(
alignment: Alignment.center,
color: Colors.yellow,
child: Text("Micro Tutorial",
style: TextStyle(
color: Colors.red,
fontSize: 20
),
),

),
),
);
}
}


Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:


Comments