gridview - Flutter 混合布局 - 带行的网格

标签 gridview layout dart flutter

我正在尝试创建一个基本的 Flutter 应用程序,它有一个 2 x 2 的文本输入网格和一个位于其下方的按钮。

最初,我只有网格,没有按钮,没有任何问题:

void main() => runApp(new App());
class App extends StatelessWidget {
...
  Widget build(BuildContext context) {
    return new MaterialApp(
      ...
      home: new InputWidget()
      ...
  class InputWidget extends StatefulWidget {
    Widget build(BuildContext context) {
      ... 
      _InputWidgetState createState() => new _InputWidgetState();
      ...
  class _InputWidgetState extends State<InputWidget> {
     Widget build(BuildContext context) {
       return new Scaffold(
         appBar: new AppBar(...)
         body: new Builder(
           builder: (BuildContext context) {
             return new GridView.count(
               children: <Widget>[
                 // 4 Text Fields here
               ]

我需要脚手架内的 GridView 才能将脚手架用于 snackbar 。

现在我想在这个网格下方添加一个按钮。为了实现这一点,我在两者之间添加了几个层,并且我得到了一个“被无穷大溢出”的错误,其逻辑如下:

void main() => runApp(new App());
class App extends StatelessWidget {
...
  Widget build(BuildContext context) {
    return new MaterialApp(
      ...
      home: new AppContainer()
      ...
  class AppContainer extends StatelessWidget {
    Widget build(BuildCOntext context) {
      return new Material( // tried Container as well
        child: new Column(
          children: <Widget>[
            new InputWidget()
            new BUttonWidget()
        ...
  class ButtonWidget extends StatelessWidget {
    Widget build(BuildContext context) {
      return new Container(
        child: new MaterialButton(...)
  ...
  class InputWidget extends StatefulWidget {
    Widget build(BuildContext context) {
      ... 
      _InputWidgetState createState() => new _InputWidgetState();
      ...
  class _InputWidgetState extends State<InputWidget> {
     Widget build(BuildContext context) {
       return new Scaffold(
         appBar: new AppBar(...)
         body: new Builder(
           builder: (BuildContext context) {
             return new GridView.count(
               children: <Widget>[
                 // 4 Text Fields here
               ]

Code & Stack Trace .

问题似乎出在“列”部分。似乎我需要为 Column 或 Scaffold 提供一些大小调整,但我似乎无法弄清楚我缺少哪些参数。

我在 SO 或 Flutter 文档上找不到任何我可以遵循的示例 - 或者我完全错过了它。

如果有任何指示可以让此布局排序,我们将不胜感激。

最佳答案

感谢@xqwzts 和@ahmadzein,得到了 Flutter Gitter 的帮助。

解决方案是将每个 child 包裹在“Expanded”中

void main() => runApp(new App());
class App extends StatelessWidget {
...
  Widget build(BuildContext context) {
    return new MaterialApp(
      ...
      home: new AppContainer()
      ...
  class AppContainer extends StatelessWidget {
    Widget build(BuildCOntext context) {
      return new Material( // tried Container as well
        child: new Column(
          children: <Widget>[
            new Expanded (
              child: new InputWidget() ... ),
            new Expanded (
              child: new Row ( 
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[new ButtonWidget()] ... )
        ...
  class ButtonWidget extends StatelessWidget {
    Widget build(BuildContext context) {
      return new Container(
        child: new MaterialButton(...)
  ...
  class InputWidget extends StatefulWidget {
    Widget build(BuildContext context) {
      ... 
      _InputWidgetState createState() => new _InputWidgetState();
      ...
  class _InputWidgetState extends State<InputWidget> {
     Widget build(BuildContext context) {
       return new Scaffold(
         appBar: new AppBar(...)
         body: new Builder(
           builder: (BuildContext context) {
             return new GridView.count(
               children: <Widget>[
                 // 4 Text Fields here
               ]

关于gridview - Flutter 混合布局 - 带行的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481505/

相关文章:

android - android中ScrollView中的GridView问题

javascript - 调试时 SQL 数据库中的图像未显示在 GridView 中,但测试查询时却显示?

Android 在 gridview 上滚动弹跳动画

html - CSS 样式表移动布局,页面右侧有巨大的空白

image - 在 Flutter 中可以知道图片的亮度吗?

image - 如何从网络图像中获取 Flutter Uint8List?

Dart Editor 不允许我编辑包文件夹中的文件

c# - Asp.net WebForms DropDownList - 具有不同值和显示文本的控件 (DataValueField/DataTextField)

qt - 在 QScrollArea 内的 QVBoxLayout 中为最初隐藏的小部件保留空间

css - Joomla:是否可以在两列中键入文本?