简体   繁体   English

如何根据 flutter 中的文字高度调整容器高度?

[英]How to Adjust container height according to the text height in flutter?

I am building a flutter application where I am trying to add a container and it's child text.我正在构建一个 flutter 应用程序,我试图在其中添加一个容器及其子文本。 I want the container to take as much height as the text requires.我希望容器的高度与文本所需的一样高。 But instead it takes the height specified.但它取而代之的是指定的高度。

Following is my code:以下是我的代码:

class ChangePassword extends StatefulWidget {
    @override
  _ChangePasswordState createState() =>
      _ChangePasswordState();
}

class _ChangePasswordState extends State<ChangePassword> {
final GlobalKey<ScaffoldState> _scaffoldkey = new GlobalKey<ScaffoldState>();
@override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        key: _scaffoldkey,
        drawer: Drawer(
          child: ListView(
            // Important: Remove any padding from the ListView.
            padding: EdgeInsets.zero,
            children: <Widget>[
              Container(
                height: (MediaQuery.of(context).size.height * 0.23),
                child: DrawerHeader(
                  child: Container(
                      padding:
                          EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                      child: Row(
                        children: [
                          Container(
                            height: MediaQuery.of(context).size.width * 0.1,
                            width: MediaQuery.of(context).size.width * 0.1,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(30),
                              color: Theme.of(context).primaryColor,
                              image: DecorationImage(
                                  fit: BoxFit.fill,
                                  image: NetworkImage(imageurl)),
                              boxShadow: [
                                BoxShadow(
                                  blurRadius: 25,
                                  color: Colors.black.withOpacity(0.2),
                                  offset: Offset(0, 10),
                                )
                              ],
                            ),
                          ),
                          Container(
                            height: MediaQuery.of(context).size.width * 0.15,
                            width: MediaQuery.of(context).size.width * 0.5,
                            padding: EdgeInsets.symmetric(horizontal: 10),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  name,
                                  style: TextStyle(
                                      fontSize: 15,
                                      color: Colors.lightBlue[900],
                                      fontWeight: FontWeight.bold),
                                ),
                                Text(email),
                                SizedBox(
                                  height: 2,
                                ),
                                Row(
                                  children: [
                                    Icon(
                                      Icons.remove_red_eye_outlined,
                                      size: 12,
                                    ),
                                    Text(
                                      'View Profile',
                                      style: TextStyle(
                                        fontSize: 12,
                                      ),
                                    )
                                  ],
                                )
                              ],
                            ),
                          ),
                        ],
                      )),
                  decoration: BoxDecoration(
                    color: Colors.white,
                  ),
                ),
              ),
                          ],
          ),
        ),
        body: SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                height: MediaQuery.of(context).size.height * 0.35,
                child: Stack(children: [
                  Positioned(
                    height: MediaQuery.of(context).size.height * 0.23,
                    top: 0,
                    child: Padding(
                      padding: EdgeInsets.only(top: 50),
                      child: Container(
                        color: Color(0xff002060),
                        width: MediaQuery.of(context).size.width,
                        height: MediaQuery.of(context).size.height * 0.1,
                        child: Align(
                          alignment: Alignment.topCenter,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              GestureDetector(
                                onTap: () {
                                  _scaffoldkey.currentState.openDrawer();
                                },
                                child: new Container(
                                  child: Icon(
                                    Icons.menu,
                                    color: Colors.white,
                                    size: MediaQuery.of(context).size.height *
                                        0.04,
                                  ),
                                  padding: new EdgeInsets.all(7.0),
                                ),
                              ),
                              SizedBox(
                                width: MediaQuery.of(context).size.width * 0.3,
                              ),
                              Container(
                                height:
                                    MediaQuery.of(context).size.height * 0.07,
                                child: Center(
                                  child: Text(
                                'Profile',
                                style: TextStyle(
                                  color: Colors.white,
                                ),
                              ),
                                ),
                              ),
                              SizedBox(
                                width: MediaQuery.of(context).size.width * 0.3,
                              ),
                              Icon(
                                Icons.search,
                                color: Colors.white,
                                size: MediaQuery.of(context).size.height * 0.04,
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                  Positioned(
                    top: MediaQuery.of(context).size.height * 0.16,
                    left: MediaQuery.of(context).size.width * 0.03,
                    child: Row(
                      children: [
                        Container(
                          width: MediaQuery.of(context).size.height * 0.14,
                          height: MediaQuery.of(context).size.height * 0.14,
                          decoration: ShapeDecoration(
                              shape: CircleBorder(), color: Colors.white),
                          child: Padding(
                            padding: EdgeInsets.all(8.0),
                            child: DecoratedBox(
                              decoration: ShapeDecoration(
                                  shape: CircleBorder(),
                                  image: DecorationImage(
                                      fit: BoxFit.cover,
                                      image: NetworkImage(
                                        'https://thumbor.forbes.com/thumbor/fit-in/416x416/filters%3Aformat%28jpg%29/https%3A%2F%2Fspecials-images.forbesimg.com%2Fimageserve%2F5f4ebe0c87612dab4f12a597%2F0x0.jpg%3Fbackground%3D000000%26cropX1%3D292%26cropX2%3D3684%26cropY1%3D592%26cropY2%3D3987',
                                      ))),
                            ),
                          ),
                        ),
                        SizedBox(
                          width: MediaQuery.of(context).size.width * 0.05,
                        ),
                        Column(
                          children: [
                            Container(
                              height: MediaQuery.of(context).size.height * 0.08,
                              child: Text(
                                'Bill Gates',
                                style: TextStyle(
                                    fontSize: 25,
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontFamily: 'Calibri'),
                              ),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                  Positioned(
                    top: MediaQuery.of(context).size.height * 0.24,
                    left: MediaQuery.of(context).size.height * 0.165,
                    child: FittedBox(
                      fit: BoxFit.fill,
                      child: Container(
                        height: MediaQuery.of(context).size.height * 0.09,
                        width: MediaQuery.of(context).size.width * 0.6,
                        child: Text(
                          'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
                          style: TextStyle(
                              color: Colors.black, fontFamily: 'Calibri'),
                        ),
                      ),
                    ),
                  ),
                ]),
              ),
                ],
              )),
              
            ],
          ),
        ));

}

Following is the image:以下是图片:

在此处输入图像描述

I want the container that contains the text below 'Bill Gates' to be wrapped according to the information entered there.我希望根据在此处输入的信息来包装包含“比尔盖茨”下方文本的容器。

The whole container should decrease as I am planning to add widgets below this customised appbar, so is there any way I can achieve this?整个容器应该减少,因为我计划在这个自定义的应用栏下方添加小部件,那么有什么办法可以实现这一点吗?

I tried to use flexible but it did not work.我尝试使用灵活,但没有奏效。 Can someone help me with this please?有人可以帮我吗?

Use fitted box:-使用合适的盒子:-

FittedBox(
   fit:BoxFit.fitHeight,
   child:Text("Your text"),
),

Do not provide the height of the container and it will adjust automatically.不要提供容器的高度,它会自动调整。

@Ankit, @Ankit,

Culprit:罪魁祸首:
As you mentioned, the Container widget is intended to take the width and height you explicitly specify, so it does what you tell it to do.正如您所提到的, Container小部件旨在采用您明确指定的宽度和高度,因此它会按照您的要求执行操作。

What you should have done in the above case is avoid specifying the height for that Container which contains the Text widget as follows.在上述情况下,您应该避免为包含Text小部件的Container指定高度,如下所示。

Change this code section from:将此代码部分更改为:

             Positioned(
                top: MediaQuery.of(context).size.height * 0.24,
                left: MediaQuery.of(context).size.height * 0.165,
                child: FittedBox(
                  fit: BoxFit.fill,
                  child: Container(
                    height: MediaQuery.of(context).size.height * 0.09,
                    width: MediaQuery.of(context).size.width * 0.6,
                    child: Text(
                      'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
                      style: TextStyle(
                          color: Colors.black, fontFamily: 'Calibri'),
                    ),
                  ),
                ),
              ),

To the following code, where I have removed specifying the height property of the container:对于以下代码,我已删除指定容器的height属性:

           Positioned(
                top: MediaQuery.of(context).size.height * 0.24,
                left: MediaQuery.of(context).size.height * 0.165,
                child: FittedBox(
                  fit: BoxFit.height,
                  child: Container(
                    width: MediaQuery.of(context).size.width * 0.6,
                    child: Text(
                      'Bill Gates welcomes you. Welcome to Microsoft. It gives me immense pleasure to announce Josh Hazlewood as it\'s new CEO. It is always immense pleasure to work with them. looking foward to have a great time with him.',
                      style: TextStyle(
                          color: Colors.black, fontFamily: 'Calibri'),
                    ),
                  ),
                ),
              ),

By doing that, and wrapping the Container within a FittedBox , that varying text length Text widget will cause it to adjust its height accordingly.通过这样做,并将Container包装在FittedBox中,不同文本长度的Text小部件将导致它相应地调整其高度。

Following part is important here:以下部分在这里很重要:

               child: FittedBox(
                  fit: BoxFit.height,

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

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