Search code examples
flutterflutter-layout

Flutter : align center a specific child in a Row Widget


I'm a newbie in flutter and I'm trying to code a UI Instagram clone, How can I align a carousel indicator to the center with a Row() parent like this

Stack(
 alignment: Alignment.center,
children:[
buildIndicator(),
//Icon section
Row(
children:[
buildLeftIcons(),
buildRightIcon(),
],
),
],
),

Result I got: ![final result][this]


Solution

  • Hey you can use Spacer() between icons and dot in row children . Spacer widget auto take extra width like below -

    Row(
      children: [
         Icon(),   
         Icon(),   
         Icon(),  
         Spacer(),
         DotsIndicator(),
         Spacer(), 
      ],
    ),
    

    Here is another example with Expanded widget and row, Expanded will automatically take rest of width other then icons

    Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
             Icon(),   
             Icon(),   
             Icon(),  
             Expanded(
               child: Center(
                 child: DotsIndicator(),
               )
             ),
          ],
        ),
    

    // UI with left and right icons

    Row(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
             Icon(),   
             Icon(),   
             Icon(),  
             Expanded(
               child: Center(
                 child: DotsIndicator(),
               )
             ),
            Icon(),  
          ],
        ),
     
    

    For you reference - Spacer and Expanded