Flutter在Wrap小部件上居中和右对齐

huangapple go评论62阅读模式
英文:

Flutter align center and right on a wrap widget

问题

使用Wrap小部件来对齐小部件在中心和右侧,请参阅下面的图像,我正在尝试实现的效果。

使用Wrap小部件而不是Row小部件,因为小部件在调整屏幕大小时会根据可用空间进行调整。

Flutter在Wrap小部件上居中和右对齐

以下是尝试获取上述结果的代码,如图所示。

Wrap(
	alignment: WrapAlignment.center,
	crossAxisAlignment: WrapCrossAlignment.center,
	runAlignment: WrapAlignment.center,
	spacing: 30.0,
	children: [
	  Wrap(
		spacing: 20,
		children: const [
		  Text("关于我"),
		  Text("B"),
		  Text("C"),
		  Text("D"),
		],
	  ),
	  Wrap(
		alignment: WrapAlignment.end,
		children: [
		  Text("我的数据"),
		],
	  ),
	],
),

但小部件未正确对齐,上述代码的输出如下。

Flutter在Wrap小部件上居中和右对齐

英文:

I am using Wrap widget to align widgets on center and right, please see the below image what i am trying to achieve.

Using wrap widget instead of Row widget because widget get adjusted on available space when resizing the screen.

Flutter在Wrap小部件上居中和右对齐

Below is the code tried to get the above result as shown in the image.

Wrap(
	alignment: WrapAlignment.center,
	crossAxisAlignment: WrapCrossAlignment.center,
	runAlignment: WrapAlignment.center,
	spacing: 30.0,
	children: [
	  Wrap(
		spacing: 20,
		children: const [
		  Text("About Me"),
		  Text("B"),
		  Text("C"),
		  Text("D"),
		],
	  ),
	  Wrap(
		alignment: WrapAlignment.end,
		children: [
		  Text("My Data"),
		],
	  ),
	],
),

but widgets are not aligned properly, above code output as below.

Flutter在Wrap小部件上居中和右对齐

答案1

得分: 2

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topRight,
      child: Wrap(
        alignment: WrapAlignment.center,
        spacing: MediaQuery.of(context).size.width * 0.4,
        children: [
          Wrap(
            spacing: MediaQuery.of(context).size.width * 0.05,
            children: const [
              Text("About Me"),
              Text("B"),
              Text("C"),
              Text("D"),
            ],
          ),
          Wrap(
            alignment: WrapAlignment.end,
            children: [
              Text("My Data"),
            ],
          ),
        ],
      );
    }
  }
}

输出如下:

Flutter在Wrap小部件上居中和右对齐


<details>
<summary>英文:</summary>

    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Align(
          alignment: Alignment.topRight,
          child: Wrap(
            alignment: WrapAlignment.center,
            spacing: MediaQuery.of(context).size.width * 0.4,
            children: [
              Wrap(
                spacing: MediaQuery.of(context).size.width * 0.05,
                children: const [
                  Text(&quot;About Me&quot;),
                  Text(&quot;B&quot;),
                  Text(&quot;C&quot;),
                  Text(&quot;D&quot;),
                ],
              ),
              Wrap(
                alignment: WrapAlignment.end,
                children: [
                  Text(&quot;My Data&quot;),
                ],
              ),
            ],
          ),
        );
      }
    }


here is the output

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/rQ6pR.png

</details>



huangapple
  • 本文由 发表于 2023年2月6日 14:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定