Flutter - 容器高度与子 Text.length 相同?

标签 flutter dart containers height singlechildscrollview

各位开发者大家晚上好

我正在尝试为我的父容器找到一种方法,使其高度设置为等于子文本小部件的内容。 文本小部件中的文本 (textDes) 正在更改。它可以很短也可以很长,因此我无法将高度设置为特定数字,例如 1400,因为它看起来很难看。

请好心帮助我,努力几个小时。

import 'package:angelbay_bungalows/screens/overview.dart';
import 'package:angelbay_bungalows/widgets/drawer.dart';
import 'package:flutter/material.dart';

class Amenities extends StatelessWidget {
  final String titleTop;
  final String textDes;
  final String img;

  Amenities(this.titleTop, this.textDes, this.img);

  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  @override
  Widget build(BuildContext context) {
    var screenSize = MediaQuery.of(context).size;

    return Scaffold(
      key: scaffoldKey,
      drawer: AppDrawer(),
      body: SingleChildScrollView(
          child: Container(
        height: 1400,
        width: screenSize.width,
        child: Stack(
          // overflow: Overflow.visible,
          children: <Widget>[
            Image.asset(
              "$img",
              height: 400.0,
              width: screenSize.width,
              fit: BoxFit.cover,
            ),
            Positioned(),
            Positioned(),
            Positioned(
              top: 375.0,
              child: Container(
                // height: ,
                width: screenSize.width,
                decoration: BoxDecoration(
                  color: Color.fromRGBO(216, 216, 216, 1),
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(30.0),
                    topRight: Radius.circular(30.0),
                  ),
                ),
                child: Padding(
                  padding: const EdgeInsets.all(25.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        titleTop,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 26.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(
                        height: 40.0,
                      ),
                      Text(
                        'Description',
                        style: TextStyle(
                          fontSize: 18.0,
                          fontWeight: FontWeight.w500,
                          color: Color.fromRGBO(50, 54, 67, 1),
                        ),
                      ),
                      SizedBox(
                        height: 20.0,
                      ),
                      Text(
                        textDes,
                        style: TextStyle(
                          fontSize: 16.0,
                          color: Color.fromRGBO(117, 117, 117, 1),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      )),
    );
  }
}

最佳答案

将您的 Text 小部件包裹在 Expanded 中并删除硬编码的 1400 高度

                          Expanded(
                            child: Text(
                              textDes,
                              style: TextStyle(
                                fontSize: 16.0,
                                color: Color.fromRGBO(117, 117, 117, 1),
                              ),
                            ),
                          ),

希望对您有所帮助!

关于Flutter - 容器高度与子 Text.length 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60873598/

相关文章:

Flutter删除For循环中的多个列表项(For循环中的多个返回)

flutter - 'Future<dynamic>'类型不是'List <Profile>类型的子类型

c++ - 我可以使用不完整类型列表的迭代器吗?

android - Flutter Android 透明导航栏和按钮

flutter - 如何在 Flutter 中删除 BottomNavigationBarItem 的 "icon"属性的填充?

flutter - 有什么方法可以检测它是否在网络上运行?

dart - 如何使用shelf_auth库验证已建立的 session ?

c# - 我无法理解 Dart 中的 `Null` 是什么

C++容器/迭代器依赖问题

docker - Windows Server Containers 域帐户身份验证