使容器小部件高度适合父行高度

Mor*_*smo 4 flutter flutter-layout

我有一个具有以下布局的 Web 应用程序:

在此处输入图片说明

我正在尝试创建一个具有类似布局的颤振应用程序,这就是我目前所拥有的:

在此处输入图片说明

出于重现目的,我的布局逻辑如下(我的所有代码都在main.dart此示例的文件中):

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Assemblers Tasks',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.deepOrange,
      ),
      home: MyHomePage(title: 'Assembly Tasks'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String dropdownValue;

  List<Map<String, dynamic>> buildItems = [];
  getBuilds() {
    List<Map<String, dynamic>> items = [];
    items.add({"id": 10, "vehicleModel": "A10", "vehicleNumber": "TEST-00010"});
    setState(() {
      buildItems = items;
    });
  }

  List<Map<String, dynamic>> buildSections = [];
  getSections() {
    List<Map<String, dynamic>> items = [];
    items.add({
      "id": 5,
      "section": "Front",
    });
    items.add({
      "id": 15,
      "section": "Rear",
    });
    setState(() {
      buildSections = items;
    });
  }

  Future<List<Map<String, dynamic>>> getSystems(String buildSectionId) async {
    List<Map<String, dynamic>> items = [];
    if (int.parse(buildSectionId) == 5) {
      items.add({
        "id": 4,
        "system": "Hydraulics",
      });
      items.add({
        "id": 20,
        "system": "High Voltage",
      });
    }
    return items;
  }

  @override
  void initState() {
    getBuilds();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: 275.0,
                  child: DropdownButton(
                      value: dropdownValue,
                      hint: Text("Choose Build"),
                      isExpanded: true,
                      items: buildItems
                          .map<Map<String, String>>((Map<String, dynamic> item) {
                            String id = item["id"].toString();
                            String name = item["vehicleModel"] + " " + item["vehicleNumber"];
                            return {"id": id, "name": name};
                          })
                          .toList()
                          .map<DropdownMenuItem<String>>((Map<String, String> item) {
                            return DropdownMenuItem<String>(
                              value: item["id"],
                              child: Text(item["name"]),
                            );
                          })
                          .toList(),
                      onChanged: (String newValue) {
                        getSections();
                        setState(() {
                          dropdownValue = newValue;
                        });
                      }),
                ),
              ],
            ),
            Row(
              children: <Widget>[
                Container(
                  width: 150.0,
                  height: 60.0,
                  color: Colors.black,
                  child: Align(
                    alignment: Alignment.center,
                    child: Text(
                      "SECTION",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 17.3,
                        letterSpacing: 1.35,
                      ),
                    ),
                  ),
                ),
                Container(
                  width: 150.0,
                  height: 60.0,
                  color: Color(0xff444444),
                  child: Align(
                    alignment: Alignment.center,
                    child: Text(
                      "SYSTEM",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 17.3,
                        letterSpacing: 1.35,
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 60.0,
                    padding: EdgeInsets.only(left: 36.0),
                    margin: EdgeInsets.only(right: 72.0),
                    color: Color(0xff666666),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "TASK",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 17.3,
                          letterSpacing: 1.35,
                        ),
                      ),
                    ),
                  ),
                )
              ],
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(
                  child: Container(
                    decoration: BoxDecoration(border: Border.all(color: Colors.black12, width: 1.0, style: BorderStyle.solid)),
                    height: MediaQuery.of(context).size.height - 225,
                    child: ListView.builder(
                      shrinkWrap: true,
                      itemCount: buildSections.length,
                      itemBuilder: (BuildContext context, int index) {
                        String section = buildSections[index]["section"] != null ? buildSections[index]["section"] : "";
                        String buildSectionId = buildSections[index]["id"].toString();
                        return Row(
                          children: <Widget>[
                            Container(
                              width: 150.0,
                              decoration: BoxDecoration(
                                border: Border(
                                  right: BorderSide(
                                    color: Colors.black12,
                                    width: 1.0,
                                  ),
                                  bottom: BorderSide(
                                    color: Colors.black12,
                                    width: 1.0,
                                  ),
                                ),
                              ),
                              padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  Align(
                                    alignment: Alignment.center,
                                    child: Center(
                                      child: RotatedBox(
                                        quarterTurns: -1,
                                        child: Text(
                                          section.toUpperCase(),
                                          style: TextStyle(
                                            fontSize: 15.0,
                                            letterSpacing: 1.2,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.all(12.0),
                                  ),
                                  Align(
                                    alignment: Alignment.center,
                                    child: FloatingActionButton(
                                      child: Icon(Icons.image),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            FutureBuilder(
                              future: getSystems(buildSectionId),
                              builder: (BuildContext context, AsyncSnapshot systemsSnap) {
                                if (systemsSnap.connectionState == ConnectionState.waiting) {
                                  return Container(
                                    height: 100.0,
                                    width: 200.0,
                                    child: Text("Please wait..."),
                                  );
                                } else if (systemsSnap.hasError) {
                                  return Container(
                                    height: 100.0,
                                    width: 200.0,
                                    child: Text("Oops! There was an error!"),
                                  );
                                }
                                return Row(
                                  children: <Widget>[
                                    Container(
                                      width: MediaQuery.of(context).size.width - 256.0,
                                      child: ListView.builder(
                                        shrinkWrap: true,
                                        itemCount: systemsSnap.data.length,
                                        itemBuilder: (context, index) {
                                          Map<String, dynamic> system = systemsSnap.data[index];
                                          String systemName = system["system"];
                                          return Row(
                                            children: <Widget>[
                                              Container(
                                                padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
                                                width: 150.0,
                                                decoration: BoxDecoration(
                                                  border: Border(
                                                    right: BorderSide(
                                                      color: Colors.black12,
                                                      width: 1.0,
                                                    ),
                                                    bottom: BorderSide(
                                                      color: Colors.black12,
                                                      width: 1.0,
                                                    ),
                                                  ),
                                                ),
                                                child: Column(
                                                  children: <Widget>[
                                                    Align(
                                                      alignment: Alignment.center,
                                                      child: RotatedBox(
                                                        quarterTurns: -1,
                                                        child: Text(
                                                          systemName.toUpperCase(),
                                                          style: TextStyle(
                                                            fontSize: 15.0,
                                                            letterSpacing: 1.2,
                                                          ),
                                                        ),
                                                      ),
                                                    ),
                                                    Padding(
                                                      padding: EdgeInsets.all(12.0),
                                                    ),
                                                    Align(
                                                      alignment: Alignment.center,
                                                      child: FloatingActionButton(
                                                        child: Icon(Icons.image),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ],
                                          );
                                        },
                                      ),
                                    ),
                                  ],
                                );
                              },
                            ),
                          ],
                        );
                      },
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(16.0, 16.0, 0.0, 0.0),
                  child: Column(
                    children: <Widget>[
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.photo_library),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(10.0),
                      ),
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.library_books),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(10.0),
                      ),
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.list),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(10.0),
                      ),
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.history),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

上述代码已准备好粘贴到 main.dart 文件中并在虚拟设备(最好是平板电脑)上预览。

到目前为止,我已经尝试了我在这些帖子中找到的解决方案,但都无济于事:
-使容器小部件垂直填充父级
- Flutter Container 高度与父级高度相同
- Flutter 展开 Container 以填充 Row 的剩余空间
-相当于 flutter 中的 wrap_content 和 match_parent ?

由于包含部分ContainerRow也有一个ListView正在使用FutureBuilder生成,因此Row的高度会自动扩展以适合ListView。我还希望Container部分扩展到与Row小部件扩展方式相同的高度;即,部分的底边框容器,上面写着,应与下边框对齐海特电压系统和右边框容器,应一路去顶。

我已经花了 3 天没有解决。


编辑

我已经尝试了@MaadhavSharma 提供的答案的建议,但出现以下异常:

????????? 异常被渲染库捕获 ??????????????????????????????
在 performLayout() 期间抛出以下断言: BoxConstraints 强制无限高度。

Pab*_*era 9

我稍微改变了结构以使其工作,这是整个build()方法:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                Container(
                  width: 275.0,
                  child: DropdownButton(
                      value: dropdownValue,
                      hint: Text("Choose Build"),
                      isExpanded: true,
                      items: buildItems
                          .map<Map<String, String>>((Map<String, dynamic> item) {
                        String id = item["id"].toString();
                        String name = item["vehicleModel"] + " " + item["vehicleNumber"];
                        return {"id": id, "name": name};
                      })
                          .toList()
                          .map<DropdownMenuItem<String>>((Map<String, String> item) {
                        return DropdownMenuItem<String>(
                          value: item["id"],
                          child: Text(item["name"]),
                        );
                      })
                          .toList(),
                      onChanged: (String newValue) {
                        getSections();
                        setState(() {
                          dropdownValue = newValue;
                        });
                      }),
                ),
              ],
            ),
            Row(
              children: <Widget>[
                Container(
                  width: 150.0,
                  height: 60.0,
                  color: Colors.black,
                  child: Align(
                    alignment: Alignment.center,
                    child: Text(
                      "SECTION",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 17.3,
                        letterSpacing: 1.35,
                      ),
                    ),
                  ),
                ),
                Container(
                  width: 150.0,
                  height: 60.0,
                  color: Color(0xff444444),
                  child: Align(
                    alignment: Alignment.center,
                    child: Text(
                      "SYSTEM",
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 17.3,
                        letterSpacing: 1.35,
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    height: 60.0,
                    padding: EdgeInsets.only(left: 36.0),
                    margin: EdgeInsets.only(right: 72.0),
                    color: Color(0xff666666),
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        "TASK",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 17.3,
                          letterSpacing: 1.35,
                        ),
                      ),
                    ),
                  ),
                )
              ],
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Expanded(
                  child: Container(
                    decoration: BoxDecoration(border: Border.all(color: Colors.black12, width: 1.0, style: BorderStyle.solid)),
                    height: MediaQuery.of(context).size.height - 225,
                    child: ListView.builder(
                      shrinkWrap: true,
                      itemCount: buildSections.length,
                      itemBuilder: (BuildContext context, int index) {
                        String section = buildSections[index]["section"] != null ? buildSections[index]["section"] : "";
                        String buildSectionId = buildSections[index]["id"].toString();
                        return IntrinsicHeight(
                          child:  Row(
                            crossAxisAlignment: CrossAxisAlignment.stretch,
                            children: <Widget>[
                              Container(
                                width: 150.0,
                                decoration: BoxDecoration(
                                  color: Colors.green,
                                  border: Border(
                                    right: BorderSide(
                                      color: Colors.black12,
                                      width: 1.0,
                                    ),
                                    bottom: BorderSide(
                                      color: Colors.black12,
                                      width: 1.0,
                                    ),
                                  ),
                                ),
                                padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Align(
                                      alignment: Alignment.center,
                                      child: Center(
                                        child: RotatedBox(
                                          quarterTurns: -1,
                                          child: Text(
                                            section.toUpperCase(),
                                            style: TextStyle(
                                              fontSize: 15.0,
                                              letterSpacing: 1.2,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ),
                                    Padding(
                                      padding: EdgeInsets.all(12.0),
                                    ),
                                    Align(
                                      alignment: Alignment.center,
                                      child: FloatingActionButton(
                                        child: Icon(Icons.image),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              FutureBuilder(
                                future: getSystems(buildSectionId),
                                builder: (BuildContext context, AsyncSnapshot systemsSnap) {
                                  if (systemsSnap.connectionState == ConnectionState.waiting) {
                                    return Container(
                                      height: 100.0,
                                      width: 200.0,
                                      child: Text("Please wait..."),
                                    );
                                  } else if (systemsSnap.hasError) {
                                    return Container(
                                      height: 100.0,
                                      width: 200.0,
                                      child: Text("Oops! There was an error!"),
                                    );
                                  }

                                  return

                                    Container(
                                      width: MediaQuery.of(context).size.width - 256.0,
                                      child: Column(
                                        children: systemsSnap.data.map<Widget>((e) => Container(
                                          padding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0),
                                          width: 150.0,
                                          decoration: BoxDecoration(
                                            border: Border(
                                              right: BorderSide(
                                                color: Colors.black12,
                                                width: 1.0,
                                              ),
                                              bottom: BorderSide(
                                                color: Colors.black12,
                                                width: 1.0,
                                              ),
                                            ),
                                          ),
                                          child: Column(
                                            children: <Widget>[
                                              Align(
                                                alignment: Alignment.center,
                                                child: RotatedBox(
                                                  quarterTurns: -1,
                                                  child: Text(
                                                    e["system"].toUpperCase(),
                                                    style: TextStyle(
                                                      fontSize: 15.0,
                                                      letterSpacing: 1.2,
                                                    ),
                                                  ),
                                                ),
                                              ),
                                              Padding(
                                                padding: EdgeInsets.all(12.0),
                                              ),
                                              Align(
                                                alignment: Alignment.center,
                                                child: FloatingActionButton(
                                                  child: Icon(Icons.image),
                                                ),
                                              ),
                                            ],
                                          ),
                                        )).toList(),
                                      ),
                                    );

//                                  Row(
//                                  children: <Widget>[
//                                    Container(
//                                      width: MediaQuery.of(context).size.width - 256.0,
//                                      child: ListView.builder(
//                                        shrinkWrap: true,
//                                        itemCount: systemsSnap.data.length,
//                                        itemBuilder: (context, index) {
//                                          Map<String, dynamic> system = systemsSnap.data[index];
//                                          String systemName = system["system"];
//                                          return Row(
//                                            children: <Widget>[
//
//                                            ],
//                                          );
//                                        },
//                                      ),
//                                    ),
//                                  ],
//                                );
                                },
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(16.0, 16.0, 0.0, 0.0),
                  child: Column(
                    children: <Widget>[
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.photo_library),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(10.0),
                      ),
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.library_books),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(10.0),
                      ),
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.list),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(10.0),
                      ),
                      Container(
                        child: FloatingActionButton(
                          child: Icon(Icons.history),
                          onPressed: () {
                            //TODO action
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

基本上我改变了for aListView的第二个元素,因为它已经在 a 里面,这使得它有双滚动并且高度没有正确扩展行的高度,而且我把里面包裹起来并放到所以内容将适合的高度。RowColumnListViewRowIntrinsicHeightcrossAxisAlignment: CrossAxisAlignment.stretchRowRow

的使用IntrinsicHeight成本很高,但我没有看到针对您构建小部件的方式的另一种解决方案。我建议您尝试优化所有结构,以便找到更好的最佳解决方案。