Container and SizedBox
Container and SizedBox
containers and sized box hold children and hold blocks.nothing but they hold
placeholders.
child:Sizedbox()
here by typing the ctrl+spacebar to know the parameters available in the sized box.
(in sized box we can not able to add color to it).idth
we can add height to it as
body:Center(
child:Sizedbox(
height:50,
width:50,
child:Text('hello'))
here without the color we cannot define the position of aa sized box in the body.
so that's the reason we are using the text to define the postion in the body.
if you remove the center in the above syntax you can see the text in left corner.
*Drawbacks*
1) could no add color to it.
2) could add border radius to it.
3) you can't change the shape of box.
for now let's replace the sized box with the container.
body:Container(
height:50,
width:50,
color:Colors.green,
child:Center(child: Text('hello',style:Textstyle(fontsize:20 )))
(by using this the colour of the box is created and the fontsize changed in the
output)
here by using the center in the text so we can see the text in center in
thecontainer.
body:Container(
height:50,
width:50,
decoration:BoxDecoration(color:Colors.green,Shape:Boxshape.Circle,
borderRadius:BorderRadius.circular(20)
child:Center(child: Text('hello',style:Textstyle(fontsize:20 )))
by using the decoration parameter we can see the adding of color in the box.
we cannot color parameter two times like one is near the width and another one is
box decoration we can only use in the decoration parameter that you can see in the
above syntax.
by using the borderradius we can define the box shape as if you give the
circular(20) in the decorationbox you can see the side curves in the box.
borderRadius=BorderRadius.only(topleft:Radius circular(20)))
here by using the above term we can define the border radius to only one corner.
borderRadius=BorderRadius.only(topleft:Radius
circular(20),Bottomright:Radius.circular(20))),
body:Container(
padding:EdgeInsets.all(10),
here the padding gives the space inside the box we created.
margin:Edgeinsets.all(10)
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// backgroundColor: Colors.black,
title: Text('Container and SizedBox'),
),
body: Center(
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
color: Colors.blue,
// shape: BoxShape.circle,
// borderRadius: BorderRadius.circular(20),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
bottomRight: Radius.circular(20)),
boxShadow: [
BoxShadow(
blurRadius: 20, spreadRadius: 5, color: Colors.black)
]),
child: Center(
child:
Container(margin: EdgeInsets.all(10), color: Colors.red)
// Text('Hello', style: TextStyle(fontSize: 20))
)),
)
// SizedBox(height: 100, width: 50, child: Text('Hello')),
);
}
}