将容器小部件高度调整为父行高度

5
我有一个网页应用程序,它的布局如下:

enter image description here

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

enter image description here

为了复制,我的布局逻辑如下(此示例中所有代码都在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
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

上述代码已准备好粘贴到main.dart文件中,并在虚拟设备(最好是平板电脑)上进行预览。
到目前为止,我已尝试了这些帖子中找到的解决方案,但都无济于事:
- 使容器小部件垂直填充父级
- Flutter容器高度与父容器高度相同
- Flutter展开容器以填充行的剩余空间
- 在flutter中相当于wrap_content和match_parent? 由于包含部分 Container Row 还使用 FutureBuilder 生成 ListView ,因此 Row 的高度会自动扩展以适应 ListView 。我还希望部分 Container 可以扩展到与 Row 小部件扩展的高度相同; 即,部分 Container 的底部边框上写着FRONT ,应与 Hight Voltage 系统的底部边框对齐,FRONT 部分 Container 的右边框应一直延伸到顶部。

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


编辑

我尝试了@MaadhavSharma提供的答案中的建议,但是我遇到了以下异常:

════════渲染库捕获的异常════════════════════════════
在执行布局期间引发了以下断言: BoxConstraints强制使用无限高度。


为什么不使用 Table 呢? - Rémi Rousselet
@RémiRousselet 我以前没有用过表格。我会尝试一下并告诉你结果。 - Morfinismo
@RémiRousselet 我试过使用表格但它不符合我的目的。不过还是谢谢您的建议。 - Morfinismo
4个回答

10

我稍微修改了一下结构,使它能够工作。这是整个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
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

基本上,我将Row的第二个元素的ListView更改为Column,因为它已经在一个ListView中并且这使得它有双重滚动并且高度不能正确地扩展行的高度,此外,我将Row包装在一个IntrinsicHeight中,并将crossAxisAlignment: CrossAxisAlignment.stretch应用于Row,以便内容适合Row的高度。

IntrinsicHeight的使用是昂贵的,但我看不到其他解决方案,因为您组织小部件的方式。我建议您尝试优化整个结构,以便找到更好和更优化的解决方案。


我会尝试一下,并向您更新我的结果。感谢您的建议。 - Morfinismo
我尝试了这个但它并没有像预期的那样工作。无论如何还是非常感谢。我已经更新了问题中的源代码,以防您想要复现它。 - Morfinismo
我可以重现它并且它有效,让我验证你的新代码。 - Pablo Barrera
Pablo,我以最大的尊重向你问候!这个解决方案非常好,它帮助我更好地理解了如何在Flutter中更好地处理布局。非常感谢! - Morfinismo

2
如果您想将容器拉伸到与其父容器相匹配,则可以使用height和width属性的double.infinity。
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text('Container as a layout')),
    body: Container(
      height: double.infinity,
      width: double.infinity,
      color: Colors.yellowAccent,
      child: Text("Hi"),
    ),
  );
}

谢谢您的建议,但它并没有起作用。我已经更新了问题,以反映应用您的建议后的结果。 - Morfinismo

1

关键是在想要填充行高的某些容器中使用constraints: BoxConstraints.expand()

尝试像这样:

screenshot of layout

Scaffold(
      appBar: AppBar(
        title: Text("Title"),
      ),
      body: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: <Widget>[
              Expanded(
                flex: 2,
                child: Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  color: Colors.yellow,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Container(
                            constraints: BoxConstraints.expand(),
                            color: Colors.blue,
                            child: Text("box 1")),
                      ),
                      Expanded(
                        flex: 2,
                        child: Container(
                            constraints: BoxConstraints.expand(),
                            color: Colors.red,
                            child: Column(
                              children: <Widget>[
                                Expanded(
                                  flex: 2,
                                  child: Container(
                                    padding:
                                        EdgeInsets.fromLTRB(0, 0, 0, 0),
                                    color: Colors.yellow,
                                    child: Row(
                                      children: <Widget>[
                                        Expanded(
                                          flex: 1,
                                          child: Container(
                                              constraints:
                                                  BoxConstraints.expand(),
                                              color: Colors.lightGreen,
                                              child: Text("box 2")),
                                        ),
                                        Expanded(
                                          flex: 2,
                                          child: Container(
                                              constraints:
                                                  BoxConstraints.expand(),
                                              color: Colors.lightBlue,
                                              child: Text("box 3")),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                                Expanded(
                                  flex: 1,
                                  child: Container(
                                    padding:
                                        EdgeInsets.fromLTRB(0, 0, 0, 0),
                                    color: Colors.yellow,
                                    child: Row(
                                      children: <Widget>[
                                        Expanded(
                                          flex: 1,
                                          child: Container(
                                              constraints:
                                                  BoxConstraints.expand(),
                                              color: Colors.purple,
                                              child: Text("box 4")),
                                        ),
                                        Expanded(
                                          flex: 2,
                                          child: Container(
                                              constraints:
                                                  BoxConstraints.expand(),
                                              color: Colors.orange,
                                              child: Text("")),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            )),
                      ),
                    ],
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  color: Colors.yellow,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        flex: 1,
                        child: Container(
                            constraints: BoxConstraints.expand(),
                            color: Colors.yellow,
                            child: Text("box 5")),
                      ),
                      Expanded(
                        flex: 2,
                        child: Container(
                            constraints: BoxConstraints.expand(),
                            color: Colors.green,
                            child: Text("box 6")),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          )),
    );

这个解决方案不需要使用 ListView。我的方法存在一个问题,就是我使用了一个包含 ListView 的行,其中包含更多的 ListView 行。尝试测试我留下的整个代码,你会自己看到。感谢你的建议! - Morfinismo
我尝试将ListView添加到其中一个容器中,看起来运行良好。请尝试将您的ListView代码放入此结构中。您能否发布带有虚拟数据且没有构建器的代码,以便我可以在我的端上复制它。 - K Vij
如果您正在嵌套液压式列表视图和前置选项卡列表视图,我建议保持它们的高度固定,以便液压式列表视图的高度为前置选项卡高度的1/2,以此类推。 - K Vij
我会的,请给我一些时间来处理,然后我会更新我的代码并提供测试数据,去掉构建器,这样你就可以在你那边复制了。感谢你的帮助! - Morfinismo
我已经编辑了问题并包含了完整的源代码。它已经准备好被复制/粘贴以进行再现。任何建议都将不胜感激! - Morfinismo

1

我认为你需要使用一些固定的高度,因为你正在使用ListView和FutureBuilders。如果你能够摆脱FutureBuilders,你可能可以实现动态高度。请注意父行的高度为320,子行的高度为160。

但是,请看这个:

enter image description here

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 Container(
                          height: 320,
                          child: Row(

                            children: <Widget>[
                              Container(                                
                                width: 120.0,
                                decoration: BoxDecoration(
                                  border: Border(
                                    right: BorderSide(
                                      color: Colors.yellow,
                                      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),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Expanded(

                                child: Container(

                                  color: Colors.orange,
                                  child: false ? Text(" ") : 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: 50.0, 
                                                      height: 160,
                                                      decoration: BoxDecoration(
                                                        color: Colors.yellow,
                                                        border: Border(
                                                          right: BorderSide(
                                                            color: Colors.red,
                                                            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,
                                                                ),
                                                              ),
                                                            ),
                                                          ),

                                                        ],
                                                      ),
                                                    ),
                                                  ],
                                                );
                                              },
                                            ),
                                          ),
                                        ],
                                      );

                                    },
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  ),
                ),
                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
                          },
                        ),
                      ),
                    ],
                  ),
                ),

              ],
            ),
          ],
        ),
      ),
    );
  }
}

感谢您的帮助。我非常感激您的时间和努力。我真的需要使用Future Builder,所以这个建议不适合我的需求;尽管如此,我非常感谢您试图提供帮助的善意。 - Morfinismo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接