为什么我的flutter应用程序除了空白屏幕外什么都不显示?

时间:2020-04-28 19:14:42

标签: flutter dart

我似乎无法理解问题所在。应该输出带有16个按钮的两个Text小部件。相反,我得到的是空白的空白屏幕。任何帮助将不胜感激,谢谢!

/ *我只是为了使我的问题通过而添加此内容。我真的没有什么要补充的主要问题了。 * /

import 'package:flutter/cupertino.dart';

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

class MainScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          body: Column(
            children: <Widget>[

/* -------------------------------------------------------------------------- */
/*                               Display section                              */
/* -------------------------------------------------------------------------- */

              Column(
                crossAxisAlignment: CrossAxisAlignment.end,

                children: <Widget>[
                  Container(width: double.infinity),
                  Text('Hello World', style: TextStyle(fontSize: 20)),
                  Text('I\'m smaller!')
                ],
              ),

/* -------------------------------------------------------------------------- */
/*                               Button section                               */
/* -------------------------------------------------------------------------- */

              Row(
                children: <Widget>[
                  createButtonColumn(),
                  createButtonColumn(),
                  createButtonColumn(),
                  createButtonColumn()
                ],
              )
            ],
          )
        )
      )
    );
  }
}


Column createButtonColumn() {
  return Column(
    children: <Widget>[
      createButton(),
      createButton(),
      createButton(),
      createButton()
    ],
  );
}

createButton() {
  return Expanded(
    // flex: 1,
    child: RaisedButton(
      onPressed: () {

      },
      child: Text('Hello')
      ),
  );
}```

Console log
----------------------

    Restarted application in 3,271ms.
I/flutter ( 5199): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 5199): The following assertion was thrown during performResize():
I/flutter ( 5199): Vertical viewport was given unbounded height.
I/flutter ( 5199): Viewports expand in the scrolling direction to fill their container. In this case, a vertical
I/flutter ( 5199): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 5199): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 5199): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 5199): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 5199): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 5199): the height of the viewport to the sum of the heights of its children.
I/flutter ( 5199):
I/flutter ( 5199): The relevant error-causing widget was:
[38;5;248mI/flutter ( 5199):   GridView[39;49m
I/flutter ( 5199):
I/flutter ( 5199): When the exception was thrown, this was the stack:
[38;5;244mI/flutter ( 5199): #0      RenderViewport.performResize.<anonymous closure>[39;49m
[38;5;244mI/flutter ( 5199): #1      RenderViewport.performResize[39;49m
[38;5;244mI/flutter ( 5199): #2      RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #3      RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #4      RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #5      RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #6      RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #7      RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #8      RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #9      RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #10     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #11     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #12     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #13     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #14     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #15     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #16     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #17     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #18     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #19     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #20     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #21     RenderFlex.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #22     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #23     RenderPadding.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #24     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #25     MultiChildLayoutDelegate.layoutChild[39;49m
[38;5;244mI/flutter ( 5199): #26     _ScaffoldLayout.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #27     MultiChildLayoutDelegate._callPerformLayout[39;49m
[38;5;244mI/flutter ( 5199): #28     RenderCustomMultiChildLayoutBox.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #29     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #30     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #31     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #32     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #33     _RenderCustomClip.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #34     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #35     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #36     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #37     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #38     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #39     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #40     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #41     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #42     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #43     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #44     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #45     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #46     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #47     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #48     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #49     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #50     RenderOffstage.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #51     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #52     RenderStack.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #53     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #54     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #55     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #56     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #57     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #58     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #59     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #60     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #61     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #62     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #63     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #64     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #65     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #66     RenderProxyBoxMixin.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #67     RenderObject.layout[39;49m
[38;5;244mI/flutter ( 5199): #68     RenderView.performLayout[39;49m
[38;5;244mI/flutter ( 5199): #69     RenderObject._layoutWithoutResize[39;49m
[38;5;244mI/flutter ( 5199): #70     PipelineOwner.flushLayout[39;49m
[38;5;244mI/flutter ( 5199): #71     RendererBinding.drawFrame[39;49m
[38;5;244mI/flutter ( 5199): #72     WidgetsBinding.drawFrame[39;49m
[38;5;244mI/flutter ( 5199): #73     RendererBinding._handlePersistentFrameCallback[39;49m
[38;5;244mI/flutter ( 5199): #74     SchedulerBinding._invokeFrameCallback[39;49m
[38;5;244mI/flutter ( 5199): #75     SchedulerBinding.handleDrawFrame[39;49m
[38;5;244mI/flutter ( 5199): #76     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure>[39;49m
[38;5;244mI/flutter ( 5199): #85     _Timer._runTimers  (dart:isolate-patch/timer_impl.dart:384:19)[39;49m
[38;5;244mI/flutter ( 5199): #86     _Timer._handleMessage  (dart:isolate-patch/timer_impl.dart:418:5)[39;49m
[38;5;244mI/flutter ( 5199): #87     _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:174:12)[39;49m
I/flutter ( 5199): (elided 8 frames from package dart:async and package dart:async-patch)
I/flutter ( 5199):
I/flutter ( 5199): The following RenderObject was being processed when the exception was fired: RenderViewport#9f404 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
I/flutter ( 5199):   needs compositing
I/flutter ( 5199):   creator: Viewport ← IgnorePointer-[GlobalKey#317b3] ← Semantics ← _PointerListener ← Listener ←
I/flutter ( 5199):     _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#d3688] ←
I/flutter ( 5199):     _PointerListener ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#d8d85] ←
I/flutter ( 5199):     RepaintBoundary ← ⋯
I/flutter ( 5199):   parentData: <none> (can use size)
I/flutter ( 5199):   constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=Infinity)
I/flutter ( 5199):   size: MISSING
I/flutter ( 5199):   axisDirection: down
I/flutter ( 5199):   crossAxisDirection: right
I/flutter ( 5199):   offset: ScrollPositionWithSingleContext#a94d8(offset: 0.0, range: null..null, viewport: null,
I/flutter ( 5199):     ScrollableState, AlwaysScrollableScrollPhysics -> ClampingScrollPhysics, IdleScrollActivity#cbf73,
I/flutter ( 5199):     ScrollDirection.idle)
I/flutter ( 5199):   anchor: 0.0
I/flutter ( 5199): This RenderObject had the following descendants (showing up to depth 5):
I/flutter ( 5199):     center child: RenderSliverPadding#a6421 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199):       child: RenderSliverGrid#b12d1 NEEDS-LAYOUT NEEDS-PAINT
I/flutter ( 5199): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderViewport#9f404 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderViewport#9f404 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#4f70f relayoutBoundary=up11 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#63f8a relayoutBoundary=up10 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#05d0a relayoutBoundary=up9 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderSemanticsGestureHandler#cd528 relayoutBoundary=up8 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderPointerListener#821d7 relayoutBoundary=up7 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: _RenderScrollSemantics#2700b relayoutBoundary=up6 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#99506 relayoutBoundary=up5 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#af2b0 relayoutBoundary=up4 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#3e297 relayoutBoundary=up3 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderFlex#5c063 relayoutBoundary=up2 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: RenderBox was not laid out: RenderPadding#b3df3 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 5199): Another exception was thrown: NoSuchMethodError: The method '>' was called on null.

2 个答案:

答案 0 :(得分:2)

如果用扩展包装行,它将非常有效。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

class MainScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: SafeArea(
            child: Scaffold(
                body: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
/* -------------------------------------------------------------------------- */
/*                               Display section                              */
/* -------------------------------------------------------------------------- */

        Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            Container(width: double.infinity),
            Text('Hello World', style: TextStyle(fontSize: 20)),
            Text('I\'m smaller!')
          ],
        ),

/* -------------------------------------------------------------------------- */
/*                               Button section                               */
/* -------------------------------------------------------------------------- */

        Expanded(
          child: Row(
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              createButtonColumn(),
              createButtonColumn(),
              createButtonColumn(),
              createButtonColumn()
            ],
          ),
        )
      ],
    ))));
  }
}

Widget createButtonColumn() {
  return Expanded(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        createButton(),
        createButton(),
        createButton(),
        createButton()
      ],
    ),
  );
}

createButton() {
  return Expanded(
    // flex: 1,
    child: RaisedButton(onPressed: () {}, child: Text('Hello')),
  );
}


答案 1 :(得分:2)

尝试以下代码:您应该使用 Flexible Expanded 小部件来包装 Column Row 小部件。拥有孩子财产的一方。

import 'package:flutter/material.dart';

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

class MainScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: SafeArea(
            child: Scaffold(
                body:Body()
            )
        )
    );
  }
}


class Body extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    var size =MediaQuery.of(context).size;

    return  Column(
      children: <Widget>[

/* -------------------------------------------------------------------------- */
/*                               Display section                              */
/* -------------------------------------------------------------------------- */

        Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisSize:
          MainAxisSize.min,
          children: <Widget>[
            Container(width: double.infinity),
            Text('Hello World', style: TextStyle(fontSize: 20)),
            Text('I\'m smaller!')
          ],
        ),

/* -------------------------------------------------------------------------- */
/*                               Button section                               */
/* -------------------------------------------------------------------------- */

        Expanded(
          child: Row(mainAxisSize:
          MainAxisSize.min,
            children: <Widget>[

              createButtonColumn(),
              createButtonColumn(),
              createButtonColumn(),
              createButtonColumn()
            ],
          ),
        )
      ],
    );
  }
}

Column createButtonColumn() {

  return Column(mainAxisSize:
    MainAxisSize.min,
    children: <Widget>[
      createButton(),
      createButton(),
      createButton(),
      createButton()
    ],
  );
}

createButton() {
  return Expanded(
    // flex: 1,
    child: RaisedButton(
        onPressed: () {

        },
        child: Text('Hello')
    ),
  );
}