I wanted to make a carousel with some images along with some texts.So I put the images inside a container .But when I added a Text widget as the child of the container, the container shrinks to minimum width . Why ?
CarouselSlider(
items: [
//1st Image of Slider
Container(
child: Text("Diverse stories you'll love"),
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: AssetImage("images/welcomepage1.jpg"),
fit: BoxFit.cover,
),
),
),
//2nd Image of Slider
Container(
child: Text("Make friends and share your voice"),
margin: EdgeInsets.all(6.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
image: DecorationImage(
image: AssetImage("images/welcomepage2.jpg"),
fit: BoxFit.cover,
),
),
),
],
//Slider Container properties
options: CarouselOptions(
height: 300.0,
enlargeCenterPage: true,
autoPlay: true,
aspectRatio: 16 / 9,
autoPlayCurve: Curves.fastOutSlowIn,
enableInfiniteScroll: true,
autoPlayAnimationDuration: Duration(milliseconds: 800),
viewportFraction: 0.8,
),
),
>Solution :
Containers with no children try to be as big as possible unless the incoming constraints are unbounded, in which case they try to be as small as possible. Containers with children size themselves to their children. The width, height, and constraints arguments to the constructor override this.
From the Flutter documentation.