首页 > 解决方案 > 将图标与左按钮对齐展开

问题描述

我在颤动中展开按钮时遇到问题,我需要将图标对齐到按钮的左侧,我使用小部件 Row 制作它,因为这是我找到的最佳方式。但是,文本没有正确居中。

   SizedBox(
    width: double.infinity,
    child: RaisedButton(
    onPressed: () => {},
    color: Colors.blue,
    textColor: Colors.white,
    child: Row(
     children: <Widget>[
      Icon(Icons.send),
      Expanded(
       child: Text('Enviar', textAlign: TextAlign.center)
      )
     ],
    )
   ),
  ),

结果是

在此处输入图像描述

有没有更好的方法来制作具有这种样式的按钮(文本在所有按钮中居中,不仅在它的空间中)?

标签: flutterflutter-layout

解决方案


您可以尝试两种常见的方法,我没有实现它们,但我只是为您提供核心思想:

  • 使用ListTile并将您Icon的设置为leading属性,并与Text属性对齐title。这种方法可能看起来更干净、更容易,但我怀疑您可能对对齐的Text.
  • 使用StackIcon并用一个小部件包装你Align,设置它的alignment属性以满足你的需要。然后,用您的小部件替换您ExpandedCenterText您将不需要该textAlign: TextAlign.center属性。我认为这可能对你更有效。

推荐阅读