如何删除图片后面的空白

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

How to remove white space behind the picture

问题

如何使图片周围的背景透明。

这是屏幕的代码:

body: Column(
  children: [
    Expanded(child: SingleChildScrollView(child: Text("长文本"))),
    TypeChallengeWidget(),
  ],
)

这是TypeChallengeWidget:

Widget build(BuildContext context) {
  final screenHeight = MediaQuery.of(context).size.height;
  final screenWidth = MediaQuery.of(context).size.width;
  return Stack(
    children: [
      Container(
        margin: EdgeInsets.only(top: screenHeight * 0.1 / 2),
        decoration: BoxDecoration(
          color: Colors.transparent.withOpacity(0.5),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(15),
            topRight: Radius.circular(15),
          ),
        ),
        child: Padding(
          padding: EdgeInsets.symmetric(
            vertical: screenHeight * 0.1 / 1.5,
            horizontal: 20,
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text("测试:"),
              Row(children: [
                Flexible(
                  child: TextField(
                    keyboardType: TextInputType.name,
                    autocorrect: false,
                  ),
                ),
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.green,
                    shape: CircleBorder(),
                    padding: EdgeInsets.all(20),
                  ),
                  onPressed: () {},
                  child: Text(">")
                )
              ],)
            ],
          ),
        ),
      ),
      Align(
        alignment: Alignment.topCenter,
        child: SvgPicture.asset("img", height: screenHeight * 0.1),
      ),
    ]
  );
}

我尝试将Stack放入Container并使其透明,但它不起作用。

英文:

如何删除图片后面的空白

How can I make that background around the picture transparent.

如何删除图片后面的空白

Here is the code of the screen:

body: Column(
          children: [
            Expanded(child: SingleChildScrollView(child: Text("loooong text"))),
            TypeChallengeWidget(),
          ],
        )

And this is the TypeChallengeWidget:

Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;
    return Stack(
      children: [
        Container(
          margin: EdgeInsets.only(top: screenHeight * 0.1 / 2),
          decoration: BoxDecoration(
            color: Colors.transparent.withOpacity(0.5),
            borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
          child: Padding(
            padding: EdgeInsets.symmetric(vertical: screenHeight * 0.1/1.5, horizontal: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("Test:"),
                Row(children: [
                  const Flexible(child: TextField(keyboardType: TextInputType.name, autocorrect: false,)),
                  ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.green,
                      shape: const CircleBorder(),
                      padding: const EdgeInsets.all(20),
                    ),
                    onPressed: (){}, 
                    child: const Text(">"))
                ],)
                
              ],
            ),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: SvgPicture.asset("img", height: screenHeight*0.1,)),
      ]
      );
  }

I tried to put Stack into container and make it transparent but it doesn't work.

答案1

得分: 1

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-html -->
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;
    return Column(
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: Wrap(
              children: List.generate(300, (index) => Text("loooong text")),
            ),
          ),
        ),
        Stack(
          alignment: Alignment.topCenter,
          fit: StackFit.passthrough,
          clipBehavior: Clip.none,
          children: [
            Container(
              // margin: EdgeInsets.only(top: screenHeight * 0.1 / 2),
              decoration: BoxDecoration(
                color: Colors.transparent.withOpacity(0.5),
                borderRadius: const BorderRadius.only(
                  topLeft: Radius.circular(15),
                  topRight: Radius.circular(15),
                ),
              ),
              child: Padding(
                padding: EdgeInsets.symmetric(
                    vertical: screenHeight * 0.1 / 1.5, horizontal: 20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text("Test:"),
                    Row(
                      children: [
                        const Flexible(
                            child: TextField(
                          keyboardType: TextInputType.name,
                          autocorrect: false,
                        )),
                        ElevatedButton(
                            style: ElevatedButton.styleFrom(
                              backgroundColor: Colors.green,
                              shape: const CircleBorder(),
                              padding: const EdgeInsets.all(20),
                            ),
                            onPressed: () {},
                            child: const Text(">"))
                      ],
                    )
                  ],
                ),
              ),
            ),
            Positioned(
              top: -(screenHeight * 0.1 / 2),
              child: SvgPicture.asset(
                "assets/report.svg",
                height: screenHeight * 0.1,
              ),
            ),
          ],
        ),
      ],
    );
  }
}

<!-- 结束代码片段 -->

你可以向堆叠小部件添加两个参数:
alignment: Alignment.topCenter,
clipBehavior: Clip.none,

并通过positioned重新构建图像小部件,并从容器中删除边距以适应顶部的定位小部件。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;
    return Column(
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: Wrap(
              children: List.generate(300, (index) =&gt; Text(&quot;loooong text&quot;)),
            ),
          ),
        ),
        Stack(
          alignment: Alignment.topCenter,
          fit: StackFit.passthrough,
          clipBehavior: Clip.none,
          children: [
            Container(
              // margin: EdgeInsets.only(top: screenHeight * 0.1 / 2),
              decoration: BoxDecoration(
                color: Colors.transparent.withOpacity(0.5),
                borderRadius: const BorderRadius.only(
                  topLeft: Radius.circular(15),
                  topRight: Radius.circular(15),
                ),
              ),
              child: Padding(
                padding: EdgeInsets.symmetric(
                    vertical: screenHeight * 0.1 / 1.5, horizontal: 20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(&quot;Test:&quot;),
                    Row(
                      children: [
                        const Flexible(
                            child: TextField(
                          keyboardType: TextInputType.name,
                          autocorrect: false,
                        )),
                        ElevatedButton(
                            style: ElevatedButton.styleFrom(
                              backgroundColor: Colors.green,
                              shape: const CircleBorder(),
                              padding: const EdgeInsets.all(20),
                            ),
                            onPressed: () {},
                            child: const Text(&quot;&gt;&quot;))
                      ],
                    )
                  ],
                ),
              ),
            ),
            Positioned(
              top: -(screenHeight * 0.1 / 2),
              child: SvgPicture.asset(
                &quot;assets/report.svg&quot;,
                height: screenHeight * 0.1,
              ),
            ),
          ],
        ),
      ],
    );
  }
}

<!-- end snippet -->

You can add 2 params to the stack widget
alignment: Alignment.topCenter,
clipBehavior: Clip.none,

and refactor the image widget with positioned and remove the margin from container to the mince top positioned widget

如何删除图片后面的空白

hope this will help

huangapple
  • 本文由 发表于 2023年2月10日 03:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403742.html
匿名

发表评论

匿名网友

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

确定