英文:
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<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,
              ),
            ),
          ],
        ),
      ],
    );
  }
}
<!-- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。





评论