【问题标题】:Responsive layout for Flutter web. How to set a proper height?Flutter web 的响应式布局。如何设置合适的高度?
【发布时间】:2020-05-07 22:53:22
【问题描述】:

我完全对 Flutter web 的响应式布局感到困惑。 尝试使用 Columns、Rows 和 TextFormFields 创建一个简单的表单。 我将所有字段包装在 Expanded 列中,但如果我减小浏览器大小,我会得到这样的图片:

如果我设置 Container(height: some value) 而不是 Expanded,我会得到底部溢出。

容器示例(高度:某个值)

return Container(
          margin: EdgeInsets.all(22),
          child: Card(
              child: Container(
                margin: EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text("Inspection",
                          style: Theme.of(context).textTheme.headline5),
                      Padding(
                        padding: EdgeInsets.all(8),
                      ),
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Expanded(
                              child: Column(
                                children: <Widget>[
                                  Card(
                                      child: Container(height: 250,
                                        margin: EdgeInsets.all(16),
                                        child: Form( 
                                          key: formKey,
                                          child: Column(mainAxisSize: MainAxisSize.min,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: <Widget>[
                                              Text("Inspection details",
                                                  style: Theme.of(context)
                                                      .textTheme
                                                      .headline6),

                                                 TextFormField(
                                                  controller: nameController,
                                                  decoration: InputDecoration(
                                                    labelText:
                                                        "Inspection Name",
                                                  ),
                                                  validator: (value) {
                                                    if (value.isEmpty) {
                                                      return 'Please enter Inspection name';
                                                    }
                                                    return null;
                                                  },
                                                ),

                                              Row(
                                                children: [

                                                          Text("Responsible:"),
                                                  Padding(
                                                    padding: EdgeInsets.all(8),
                                                  ),
                                                  Expanded(
                                                    child: ResponsibleSelector(
                                                      dropdownValue:
                                                          inspectionState
                                                              .inspection
                                                              .responsible,
                                                      selectionCallback: (value) =>
                                                          responsibleController
                                                              .text = value,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                               TextFormField(
                                                  onTap: () {
                                                    FocusScope.of(context)
                                                        .requestFocus(
                                                            new FocusNode());
                                                    _selectDate(context);
                                                  },
                                                  controller: dateController,
                                                  decoration: InputDecoration(
                                                    labelText:
                                                        "Inspection date",
                                                  ),
                                                ),

                                            ],
                                          ),
                                        ),
                                      ),
                                    ),

                                  Padding(
                                    padding: EdgeInsets.all(8),
                                  ),
                                 Card(
                                        child: Container(height: 400,
                                            margin: EdgeInsets.all(16),
                                            child: Column(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                mainAxisSize: MainAxisSize.min,
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: <Widget>[
                                                  Text("Client details",
                                                      style: Theme.of(context)
                                                          .textTheme
                                                          .headline6),
                                                  DropdownButton(
                                                    isExpanded: true,
                                                    value: _selectedClient,
                                                    items: _clientList,
                                                    onChanged:
                                                        onChangeDropdownItem,
                                                  ),

                                                  // Spacer(),
                                                  Expanded(
                                                    child: TextFormField(
                                                      maxLines: null,
                                                      minLines: null,
                                                      expands: true,
                                                      controller:
                                                          contactController,
                                                      decoration:
                                                          InputDecoration(
                                                        labelText:
                                                            "Contact person",
                                                      ),
                                                    ),
                                                  ),
                                                ]))),

                                ],
                              ),
                            ),

扩展示例:

return Container(
          margin: EdgeInsets.all(22),
          child: Card(
              child: Container(
                  height: MediaQuery.of(context).size.height,
                  margin: EdgeInsets.all(16),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text("Inspection",
                          style: Theme.of(context).textTheme.headline5),
                      Padding(
                        padding: EdgeInsets.all(8),
                      ),
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Expanded(
                              child: Column(
                                children: <Widget>[
                                  Expanded(
                                    child: Card(
                                      child: Container(
                                        margin: EdgeInsets.all(16),
                                        child: Form(
                                          key: formKey,
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: <Widget>[
                                              Text("Inspection details",
                                                  style: Theme.of(context)
                                                      .textTheme
                                                      .headline6),
                                              Expanded(
                                                child: TextFormField(
                                                  controller: nameController,
                                                  decoration: InputDecoration(
                                                    labelText:
                                                        "Inspection Name",
                                                  ),
                                                  validator: (value) {
                                                    if (value.isEmpty) {
                                                      return 'Please enter Inspection name';
                                                    }
                                                    return null;
                                                  },
                                                ),
                                              ),
                                              Expanded(
                                                  child: Row(
                                                children: [

                                                          Text("Responsible:"),
                                                  Padding(
                                                    padding: EdgeInsets.all(8),
                                                  ),
                                                  Expanded(
                                                    child: ResponsibleSelector(
                                                      dropdownValue:
                                                          inspectionState
                                                              .inspection
                                                              .responsible,
                                                      selectionCallback: (value) =>
                                                          responsibleController
                                                              .text = value,
                                                    ),
                                                  ),
                                                ],
                                              )

                                                  ),

                                              Expanded(
                                                child: TextFormField(
                                                  onTap: () {
                                                    FocusScope.of(context)
                                                        .requestFocus(
                                                            new FocusNode());
                                                    _selectDate(context);
                                                  },
                                                  controller: dateController,
                                                  decoration: InputDecoration(
                                                    labelText:
                                                        "Inspection date",
                                                  ),
                                                ),
                                              ),

                                            ],
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.all(8),
                                  ),
                                  Expanded(
                                    child: Card(
                                        child: Container(
                                            margin: EdgeInsets.all(16),
                                            child: Column(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                mainAxisSize: MainAxisSize.min,
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: <Widget>[
                                                  Text("Client details",
                                                      style: Theme.of(context)
                                                          .textTheme
                                                          .headline6),
                                                  DropdownButton(
                                                    isExpanded: true,
                                                    value: _selectedClient,
                                                    items: _clientList,
                                                    onChanged:
                                                        onChangeDropdownItem,
                                                  ),


                                                  Expanded(
                                                    child: TextFormField(
                                                      maxLines: null,
                                                      minLines: null,
                                                      expands: true,
                                                      controller:
                                                          contactController,
                                                      decoration:
                                                          InputDecoration(
                                                        labelText:
                                                            "Contact person",
                                                      ),
                                                    ),
                                                  ),
                                                ]))),
                                  )
                                ],
                              ),
                            ),

如何在 Flutter Web 上获得正确的布局?

【问题讨论】:

    标签: flutter flutter-web


    【解决方案1】:

    这取决于您要如何布局视图,有多种方法,但我找到了使用MediaQuery 和使用SingleChildScrollView 将高度固定为屏幕尺寸的最佳方法,所以如果屏幕尺寸下降用户可以滚动。

    注意:这通常添加到父窗口小部件

    对你来说,它看起来像:

    return Container(
              margin: EdgeInsets.all(22),
              child: Card(
                  child: Container(
                    height: MediaQuery.of(context).size.height, //added dynamic height
                    margin: EdgeInsets.all(16),
                      child: SingleChildScrollView( //added scrolling
                          child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text("Inspection",
                              style: Theme.of(context).textTheme.headline5),
                          Padding(
                            padding: EdgeInsets.all(8),
                          ),
                          Expanded(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
    

    【讨论】:

      猜你喜欢
      • 2011-11-29
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2016-01-23
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多