【问题标题】:title widget to be centered and icon widgets at the right side in one row标题小部件居中,图标小部件在一行的右侧
【发布时间】:2022-11-22 17:17:31
【问题描述】:

这个问题和this post完全一样

但我没有从帖子中找到答案。

我的目标是在中心设置标题小部件

和右侧的其他两个按钮。 链接中的解决方案都将标题小部件稍微放在左侧。

有谁知道如何在不使用 Stack 的情况下做到这一点?当标题太长时,我担心标题小部件会覆盖按钮小部件。

编码:

SizedBox(
  height: 40,
  width: MediaQuery.of(context).size.width,
  child: Row(
      children: [
        const SizedBox(width: 17),
        Text(
          'TITLE',
          style: const TextStyle(
              color: Colors.white, fontSize: 30, fontWeight: FontWeight.w500),
        ),
        Spacer(),
        Row(
            children: [
              GestureDetector(
                onTap: (){null;},
                child: SizedBox(
                  child: Icon(Icons.wallet_membership, color: Colors.white),
                ),
              ),
              const SizedBox(width: 17),
              GestureDetector(
                onTap: (){Get.toNamed('/wallet_setting');},
                child: const SizedBox(
                  child: Icon(Icons.settings, color: Colors.white),
                ),
              ),
            ]
        )

      ]
  ),
)

【问题讨论】:

  • 你能包括你目前的 sn-p 吗?
  • 当前状态的代码或屏幕截图?我只需要一个合适的结构来让它工作。我也没有包含代码,因为它无论如何都不起作用
  • 你在专栏上使用它吗?
  • 是的,它在 Column 小部件上

标签: flutter dart


【解决方案1】:

试试这个

Row(
  mainAxisAlignment: MainAxisAlignment.start,
  children: [
    Expanded(
      child: Container(
        child: Center(
          child: Text(
            'TITLE',
            style: const TextStyle(color: Colors.white, fontSize: 30, fontWeight: FontWeight.w500),
          ),
        )
      )
    ),
    Align(
      alignment: Alignment.centerRight,
      child: Row(
        children: [
          GestureDetector(
            onTap: (){null;},
            child: SizedBox(
              child: Icon(Icons.wallet_membership, color: Colors.white),
            ),
          ),
          const SizedBox(width: 17),
          GestureDetector(
            onTap: (){Get.toNamed('/wallet_setting');},
            child: const SizedBox(
              child: Icon(Icons.settings, color: Colors.white),
            ),
          ),
        ]
      )
    )
  ],
)

输出:

【讨论】:

  • 我已经试过了这个,如果你仔细看的话,标题在宽度的中间,不包括按钮。不正好在整个设备屏幕宽度的中间
【解决方案2】:

你可以在Appbar上使用centerTitle:true

appBar: AppBar(
  centerTitle: true,
  title: Text("title"),
  actions: [

你可以implements PreferredSizeWidget创建自定义Appbar。玩转颜色和其他装饰。

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const MyAppBar({super.key});

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Align(
          alignment: Alignment.bottomCenter,
          child: Text("title"),
        ),
        Align(
          alignment: Alignment.bottomRight,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              IconButton(
                //you can use callback method to get onpressed
                onPressed: () {},
                icon: Icon(Icons.settings),
              ),
              IconButton(
                onPressed: () {},
                icon: Icon(Icons.settings),
              ),
            ],
          ),
        )
      ],
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight * 2);
}

并像appBar: MyAppBar(),一样使用

【讨论】:

  • 那不是应用栏区域。应用栏位于小部件上方(带箭​​头的行),有问题的小部件是下面带有字母“标题”的小部件
  • 你可以在appBar: AppBar(bottom: AppBar(上使用bottom。如果包含当前代码会更容易,更好implements PreferredSizeWidget
  • @YeasinSheikh 你能看看this question吗?
【解决方案3】:

我认为你应该用中心包裹它

【讨论】:

  • 是的,它会使它居中但位置错误
【解决方案4】:

试试这个方法:

首先从你正确的小部件制作小部件:

Widget _rightIcons() {
    return Row(children: [
      GestureDetector(
        onTap: () {
          null;
        },
        child: SizedBox(
          child: Icon(Icons.wallet_membership, color: Colors.white),
        ),
      ),
      const SizedBox(width: 17),
      GestureDetector(
        onTap: () {
           Get.toNamed('/wallet_setting');
        },
        child: const SizedBox(
          child: Icon(Icons.settings, color: Colors.white),
        ),
      ),
    ]);
  }

然后像这样构建标题:

Row(
    children: [
      _rightIcons(),
      Expanded(
        child: Center(
          child: Text(
            'TITLE',
            style: const TextStyle(
                color: Colors.white,
                fontSize: 30,
                fontWeight: FontWeight.w500),
          ),
        ),
      ),
      Opacity(
        opacity: 0,
        child: _rightIcons(),
      )
    ],
  ),

【讨论】:

  • @dfassf 是你想要的吗?
  • 这里的技巧是在右边到左边设置完全相同的小部件,使它们不可见,这样你就有了标题中心。
  • 我实际上也想到了,这就像一个把戏所以我想知道是否有其他方法可以在不使用“技巧”的情况下解决
  • @eamirho3ein 你能看看this question吗?
  • @eamirho3ein 好的
猜你喜欢
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
相关资源
最近更新 更多