对齐 Stack Widget 下的子部件以正确排列

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

Aligning children widgets under Stack Widget properly

问题

我想询问如何正确地对齐堆叠下的小部件,我仍然对使用“Position”小部件感到困惑,所以我只是使用Container来放置我的Text小部件。
这是图像输出,“Name”位于顶部,我想将它放在圆形图像下方(请检查红色文本,这就是我想要的位置),依此类推。

以下是我上面示例图像的源代码..

return Scaffold(
  extendBodyBehindAppBar: true,
  body: SafeArea(
    child: SingleChildScrollView(
      child: Stack(
        children: <Widget>[
          Container(
            color: Colors.black,
            height: MediaQuery.of(context).size.height / 2,
            width: double.infinity,
          ),
          Padding(
            padding: const EdgeInsets.only(top: 15, left: 10, right: 5),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(),
                Container(
                  width: 28,
                  height: 28,
                  decoration: BoxDecoration(
                    color: Colors.grey.shade200, // border color
                    shape: BoxShape.circle,
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(1), // border width
                    child: Container(
                      // or ClipRRect if you need to clip the content
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: Colors.white
                            .withOpacity(0.25), // inner circle color
                      ),
                      child: InkWell(
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => const SecondPage()),
                            );
                          },
                          child: const Icon(
                              EvaIcons.settings_2_outline)), // inner content
                    ),
                  ),
                )
              ],
            ),
          ),
          Center(
            child: SizedBox(
              height: MediaQuery.of(context).size.height / 3,
              child: CircleAvatar(
                radius: 53.5,
                backgroundColor: Colors.grey.shade200,
                child: const CircleAvatar(
                  radius: 50,
                  backgroundImage: CachedNetworkImageProvider(
                      'https://images.unsplash.com/photo-1552058544-f2b08422138a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=699&q=80'),
                ),
              ),
            ),
          ),
          const Center(
            child: Text(
              "Name",
              style: TextStyle(color: Colors.white),
            ),
          )
        ],
      ),
    )),
);

我将不会回答问题,只提供翻译,如果您有其他问题,请随时提出。

英文:

I would like to ask how do I properly align the widgets under the stack, I am still confused using the Position Widget and so I am just using Container for my Text Widget.
Here is the image output, the Name is at the top, I wanted to position it under the circular image(check the red texts, that's how I wanted it to be positioned) and so on

Here is my source code for the sample image above..

return Scaffold(
      extendBodyBehindAppBar: true,
      body: SafeArea(
          child: SingleChildScrollView(
        child: Stack(
          children: &lt;Widget&gt;[
            Container(
              color: Colors.black,
              height: MediaQuery.of(context).size.height / 2,
              width: double.infinity,
            ),
            Padding(
              padding: const EdgeInsets.only(top: 15, left: 10, right: 5),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Container(),
                  Container(
                    width: 28,
                    height: 28,
                    decoration: BoxDecoration(
                      color: Colors.grey.shade200, // border color
                      shape: BoxShape.circle,
                    ),
                    child: Padding(
                      padding: const EdgeInsets.all(1), // border width
                      child: Container(
                        // or ClipRRect if you need to clip the content
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: Colors.white
                              .withOpacity(0.25), // inner circle color
                        ),
                        child: InkWell(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) =&gt; const SecondPage()),
                              );
                            },
                            child: const Icon(
                                EvaIcons.settings_2_outline)), // inner content
                      ),
                    ),
                  )
                ],
              ),
            ),
            Center(
              child: SizedBox(
                height: MediaQuery.of(context).size.height / 3,
                child: CircleAvatar(
                  radius: 53.5,
                  backgroundColor: Colors.grey.shade200,
                  child: const CircleAvatar(
                    radius: 50,
                    backgroundImage: CachedNetworkImageProvider(
                        &#39;https://images.unsplash.com/photo-1552058544-f2b08422138a?ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&amp;auto=format&amp;fit=crop&amp;w=699&amp;q=80&#39;),
                  ),
                ),
              ),
            ),
            const Center(
              child: Text(
                &quot;Name&quot;,
                style: TextStyle(color: Colors.white),
              ),
            )
          ],
        ),
      )),
    );

I would appreciate any help with an explanation as well how it should be achieved, I am just really confused with the Stack Widget now.

答案1

得分: 0

以下是翻译好的部分:

return   Scaffold(
                extendBodyBehindAppBar: true,
                body: SafeArea(
                    child: SingleChildScrollView(
                  child: Stack(
                    alignment: Alignment.center, //&lt;----- align the stack center
                    children: &lt;Widget&gt;[
                      Container(
                        color: Colors.black,
                        height: MediaQuery.of(context).size.height / 2,
                        width: double.infinity,
                      ),
                      Positioned(//&lt;---- Change padding to postioned
                        top: 15,
                        left: 10,
                        right: 5,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Container(),
                            Container(
                              width: 28,
                              height: 28,
                              decoration: BoxDecoration(
                                color: Colors.grey.shade200, // 边框颜色
                                shape: BoxShape.circle,
                              ),
                              child: Padding(
                                padding: const EdgeInsets.all(1), // 边框宽度
                                child: Container(
                                  // 或者使用 ClipRRect 如果需要裁剪内容
                                  decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    color: Colors.white
                                        .withOpacity(0.25), // 内部圆圈颜色
                                  ),
                                  child: InkWell(
                                      onTap: () {
                                        Navigator.push(
                                          context,
                                          MaterialPageRoute(
                                              builder: (context) =&gt; const SecondPage()),
                                        );
                                      },
                                      child: const Icon(EvaIcons
                                          .settings_2_outline)), // 内部内容
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                      Align(
                        alignment: Alignment.center,
                        child: Column(
                          //&lt;------将图像和文本放在列中
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            SizedBox(
                              // height: MediaQuery.of(context).size.height / 3,
                              child: CircleAvatar(
                                radius: 53.5,
                                backgroundColor: Colors.grey.shade200,
                                child: const CircleAvatar(
                                  radius: 50,
                                  backgroundImage: CachedNetworkImageProvider(
                                      &#39;https://images.unsplash.com/photo-1552058544-f2b08422138a?ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&amp;auto=format&amp;fit=crop&amp;w=699&amp;q=80&#39;),
                                ),
                              ),
                            ),
                            const Text(
                              &quot;Name&quot;,
                              style: TextStyle(color: Colors.white),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                )),
              );

希望这对你有所帮助!如果你有任何其他问题,欢迎提出。

英文:

what i would do is

  1. center align the stack
  2. put the image and the name text in a column
  3. change the padding for the settings icon to a Positioned with the same properties.

so the code will look something like this:

return   Scaffold(
extendBodyBehindAppBar: true,
body: SafeArea(
child: SingleChildScrollView(
child: Stack(
alignment: Alignment.center, //&lt;----- align the stack center
children: &lt;Widget&gt;[
Container(
color: Colors.black,
height: MediaQuery.of(context).size.height / 2,
width: double.infinity,
),
Positioned(//&lt;---- Change padding to postioned
top: 15,
left: 10,
right: 5,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(),
Container(
width: 28,
height: 28,
decoration: BoxDecoration(
color: Colors.grey.shade200, // border color
shape: BoxShape.circle,
),
child: Padding(
padding: const EdgeInsets.all(1), // border width
child: Container(
// or ClipRRect if you need to clip the content
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white
.withOpacity(0.25), // inner circle color
),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =&gt; const SecondPage()),
);
},
child: const Icon(EvaIcons
.settings_2_outline)), // inner content
),
),
)
],
),
),
Align(
alignment: Alignment.center,
child: Column(
//&lt;------Put image and text in Column
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
// height: MediaQuery.of(context).size.height / 3,
child: CircleAvatar(
radius: 53.5,
backgroundColor: Colors.grey.shade200,
child: const CircleAvatar(
radius: 50,
backgroundImage: CachedNetworkImageProvider(
&#39;https://images.unsplash.com/photo-1552058544-f2b08422138a?ixlib=rb-4.0.3&amp;ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&amp;auto=format&amp;fit=crop&amp;w=699&amp;q=80&#39;),
),
),
),
const Text(
&quot;Name&quot;,
style: TextStyle(color: Colors.white),
),
],
),
),
],
),
)),
);

huangapple
  • 本文由 发表于 2023年7月5日 01:17:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614735.html
匿名

发表评论

匿名网友

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

确定