颤振混合布局 - 网格与行

时间:2018-01-27 23:10:02

标签: 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
               ]

我需要Scaffold内的GridView才能将Scaffold用于小吃店。

现在我想在此网格下方添加一个按钮。为了达到这个目的,我在两者之间增加了几层。我通过以下逻辑得到“无限溢出”错误:

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&amp; Stack Trace

问题似乎源于“专栏”部分。似乎我需要为Column或Scaffold提供一些尺寸,但我似乎无法弄清楚我缺少哪些参数。

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

非常感谢能够对此布局进行排序的任何指针。

1 个答案:

答案 0 :(得分:0)

得到了Flutter Gitter的帮助,感谢@xqwzts&amp; @ahmadzein。

解决方案是将每个孩子都包裹在“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
               ]