简体   繁体   English

如何在 flutter 中制作响应式容器

[英]how to make responsive container in flutter

i want to make my container responsive of image size, but somehow it is not working.我想让我的容器响应图像大小,但不知何故它不起作用。

below is the container that I have made-:下面是我制作的容器-:

Container(
              height: size.height * 0.7,
              width: size.width * 0.95,
              decoration: BoxDecoration(
                // borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)),
                image: DecorationImage(
                  image: AssetImage('assets/vir.gif'),//widget.user.imgUrl),
                  fit: BoxFit.contain,
                ),
              ),
              child: Container(
                decoration: BoxDecoration(
                  // borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(color: Colors.black12, spreadRadius: 0.5),
                  ],
                ),
              ),
            ),

here if the height of image is same as the container then it is good but if the height is less then I am able to see the background that I don't want.在这里,如果图像的高度与容器相同,则很好,但如果高度较小,则我可以看到我不想要的背景。

even if I add any information in the container the height remains fixed and doesn't adjust to the content.即使我在容器中添加任何信息,高度仍然是固定的并且不会根据内容进行调整。

below is another example for this -:下面是另一个例子-:

 Container(
              height: size.height * 0.2,
              width: size.width * 0.95,
              decoration: BoxDecoration(
                color: Colors.pink[100],
              ),
              child: Column(
                children: [

                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 15),
                    child: Wrap(
                        spacing: 10,
                        children: [

                          Chip(
                            elevation: 5,
                            label: Text('Height'),
                            labelStyle: TextStyle(
                              color: Colors.white,
                              fontSize: 17,
                            ),
                            backgroundColor: Colors.pinkAccent,
                            deleteIcon: Icon(Icons.height),
                            // labelPadding: EdgeInsets.symmetric(horizontal: 10),
                          ),

                          Chip(
                            elevation: 5,
                            label: Text('Active'),
                            labelStyle: TextStyle(
                              color: Colors.white,
                              fontSize: 17,
                            ),
                            backgroundColor: Colors.pinkAccent,
                            labelPadding: EdgeInsets.symmetric(horizontal: 10),
                          ),

                          Chip(
                            elevation: 5,
                            label: Text('Start Sign'),
                            labelStyle: TextStyle(
                              color: Colors.white,
                              fontSize: 17,
                            ),
                            backgroundColor: Colors.pinkAccent,
                            labelPadding: EdgeInsets.symmetric(horizontal: 10),
                          ),


                        ]
                    ),
                  )



                ],
              ),
            ),

here if I add or delete any content then I have to increase or decrease the size manually.在这里,如果我添加或删除任何内容,那么我必须手动增加或减少大小。 can anyone help me with this?谁能帮我这个?

You don't need to provide a hight or a with.你不需要提供一个高度或一个。 The container adapts to the size of the child.容器适应孩子的大小。

Docs :文档

Otherwise, the widget has a child but no height, no width, no constraints, and no alignment, and the Container passes the constraints from the parent to the child and sizes itself to match the child.否则,小部件有一个孩子,但没有高度、没有宽度、没有约束,也没有 alignment,并且容器将约束从父级传递给子级,并调整自身的大小以匹配子级。

Flutter Widget of the week Flutter 本周小工具

import 'package:flutter/material.dart';

void main() {
  runApp(
  MaterialApp(
  home: Scaffold(
    appBar: AppBar(
      title: Text('Hello'),
      centerTitle: true,
    ),
    body: Container(
      alignment: Alignment.center,
      child: Container(
        height: 200,
        width: 200,
        child: Image(
          image: NetworkImage(
              'https://images.unsplash.com/photo-1593642634402-b0eb5e2eebc9? 
               ixid=MXwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb- 
               1.2.1&auto=format&fit=crop&w=1050&q=80'),
          fit: BoxFit.cover,
        ),
      ),
    ),
  ),
 ),
 );
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM