The SizedBox
is to create a box with a specified size.
The default constructor which creates a fixed size box.
SizedBox(
width: // If non-null, requires the child to have exactly this width.
height: // If non-null, requires the child to have exactly this height.
child: // child widget
)
It contains many input parameters which can be configured to change its behavior and appearance.
The width
and height
parameters can be null to indicate that the size of the box should not be constrained in the corresponding dimension.
The following code snippet shows how to use SizedBox
widget.
SizedBox(
width: 200.0,
height: 30.0,
child: RaisedButton(
onPressed: () {},
child: Text('Create Account'),
),
);
This example makes the RaisedButton
have the exact size 200x30.
import 'package:flutter/material.dart';
class SizedBoxDemo extends StatelessWidget {
Widget build(BuildContext context) {
return Center(
child: SizedBox(
width: 200.0,
height: 30.0,
child: RaisedButton(
onPressed: () {},
child: Text('Create Account'),
),
));
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('SizedBox'),
),
body: SizedBoxDemo(),
),
);
}
}
void main() => runApp(MyApp());