首页 > 解决方案 > 固定小部件在一行中的位置

问题描述

我的行中有两个小部件,一个文本小部件和一个计数器小部件。文本小部件包含在计数器增加/减少时增加和减少的值。现在,当值增加时,它会将计数器向右推。我该如何防止这种情况发生?我希望柜台保持在固定位置。

Widget build(BuildContext context) {
     var loyaltyPrice =  Provider.of<Cart>(context, listen: false).loyaltyPrice;
    return  
       Card(
        color:Colors.red[100],
        margin: EdgeInsets.symmetric(
          horizontal: 15,
          vertical: 4,
        ),
        child: Padding(
         padding: const EdgeInsets.all(5),
          child:Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Row(
                  children: [
                      Text('- \Tsh. $loyaltyPrice ',style:TextStyle(color:Colors.red)),
                      SizedBox(width:120),
                     LoyaltyCounterView(),

                  ]),                 
                  ],
                )
              ),         
       );
        
  }
}

在此处输入图像描述

标签: flutterdart

解决方案


而不是在您的小部件之间使用大小框,用容器包装您的文本小部件并为其设置宽度

Row(
  children: [
       Container(width:your width,child:  Text('- \Tsh. $loyaltyPrice                   ',style:TextStyle(color:Colors.red)),)
      LoyaltyCounterView(),

  ]),     

还有另一种方法是MainAxisAlignment.spaceBetween用于您的行小部件。我建议使用第二种方法来解决您的问题


推荐阅读