我有一个按钮:

enter image description here

这只是一个简单的按钮,文本居中对齐.现在想象一下,我需要在横轴上的文本旁边添加一个小部件!

大概是这样的:

SizedBox(
  width: double.infinity,
  height: 56,
  child: TextButton(
    style: ButtonStyle(
      backgroundColor: MaterialStateProperty.all<Color>(
        const Color(0XFF00966D),
      ),
      foregroundColor: MaterialStateProperty.all<Color>(
        const Color(0XFFFAFAFA),
      ),
      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
        const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(
            Radius.circular(8),
          ),
        ),
      ),
    ),
    onPressed: () {
      Navigator.popAndPushNamed(context, "signupPhoneVerification");
    },
    child: const Text('Sign Up'),
  ),
),

我怎么能这样做呢?

推荐答案

要做到这一点,可能有很多方法:

SizedBox()-将这一排的子元素分成3个大小相同的部分:

SizedBox(
            width: double.infinity,
            height: 56,
            child: TextButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all<Color>(
                  const Color(0XFF00966D),
                ),
                foregroundColor: MaterialStateProperty.all<Color>(
                  const Color(0XFFFAFAFA),
                ),
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                  const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(8),
                    ),
                  ),
                ),
              ),
              onPressed: () {
                Navigator.popAndPushNamed(context, "signupPhoneVerification");
              },
              child: Row(
                children: [
                  SizedBox(
                    width: MediaQuery.of(context).size.width / 3.3,
                    child: Container(),
                  ),
                  SizedBox(
                      width: MediaQuery.of(context).size.width / 3.3,
                      child: const Text('Sign Up', textAlign: TextAlign.center,)
                  ),
                  SizedBox(
                      width: MediaQuery.of(context).size.width / 3.3,
                      child: const Icon(Icons.star)
                  ),
                ],
              ),
            ),
          ),

MainAxisAlignment.center-在左侧添加一个小部件以平衡它:

SizedBox(
            width: double.infinity,
            height: 56,
            child: TextButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all<Color>(
                  const Color(0XFF00966D),
                ),
                foregroundColor: MaterialStateProperty.all<Color>(
                  const Color(0XFFFAFAFA),
                ),
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                  const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(8),
                    ),
                  ),
                ),
              ),
              onPressed: () {
                Navigator.popAndPushNamed(context, "signupPhoneVerification");
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                  SizedBox(
                    width: 50
                  ),
                  Text('Sign Up', textAlign: TextAlign.center,),
                  Icon(Icons.star),
                ],
              ),
            ),
          ),

使用Stack:

SizedBox(
            width: double.infinity,
            height: 56,
            child: TextButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all<Color>(
                  const Color(0XFF00966D),
                ),
                foregroundColor: MaterialStateProperty.all<Color>(
                  const Color(0XFFFAFAFA),
                ),
                shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                  const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(8),
                    ),
                  ),
                ),
              ),
              onPressed: () {
                Navigator.popAndPushNamed(context, "signupPhoneVerification");
              },
              child: Stack(
                alignment: Alignment.center,
                clipBehavior: Clip.none,
                children: const [
                  Text('Sign Up', textAlign: TextAlign.center,),
                  Positioned(
                    right: -50,
                      child: Icon(Icons.star)
                  ),
                ],
              ),
            ),
          ),

Flutter相关问答推荐

飘动的文本用/字符绕转

Firebase Auth异常未被catch捕获

如何组合匹配器

如何删除使用isscllable时出现的左边距?

为什么PUT方法没有发出任何响应?

为什么用户界面不会在Flight中的复选框中更改?

播放特定持续时间Flutter 的音频

如何在flutter中更改showModalBottomSheet小部件中CheckboxListTile小部件的值?

Flutter:在 pubspec.yaml 中找不到assets资源

更改下拉按钮的背景 colored颜色 - flutter

将状态维护到 asyncNotifier

忽略异步函数上的 context.mounted 访问 linting 错误是否安全?

Flutter:为什么我的 listTile colored颜色 被容器 colored颜色 覆盖了?

底部工作表顶部的一块 Flutter

无效的 depfile: C:\xxx\xxx\Flutter\project_name\.dart_tool\flutter_build\df4b308df1ee4bce22c56c71751554d1\kernel_snapshot.d 无效

Flutter:制作自定义小部件的最佳做法是什么?

构建失败 - 无法解析 io.grpc:grpc-core:[1.28.0]. (是的,我已经升级到 mavenCentral())

Flutter - 检测手指何时进入容器

参数类型MaterialPageRoute不能分配给参数类型String

未处理的异常:FileSystemException:无法打开文件,路径 = 'assets/res.zip'(操作系统错误:没有这样的文件或目录,errno = 2)