[Flutter]キーボード表示時のレイアウトエラー

スポンサーリンク

TextFieldを含んだ、Widgetを作っていた時に、そのTextFieldにフォーカスを当てて、キーボードが表示されたときに、エラーが発生した。
その時の解決方法を記載しておきます。

エラーメッセージ

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during layout:
A RenderFlex overflowed by 161 pixels on the bottom.

The relevant error-causing widget was:
  Column file:///C:/Users/User/AndroidStudioProjects/flutter_app/lib/views/search.dart:84:20
The overflowing RenderFlex has an orientation of Axis.vertical.
The edge of the RenderFlex that is overflowing has been marked in the rendering with a yellow and black striped pattern. This is usually caused by the contents being too big for the RenderFlex.

Consider applying a flex factor (e.g. using an Expanded widget) to force the children of the RenderFlex to fit within the available space instead of being sized to their natural size.
This is considered an error condition because it indicates that there is content that cannot be seen. If the content is legitimately bigger than the available space, consider clipping it with a ClipRect widget before putting it in the flex, or using a scrollable container rather than a Flex, like a ListView.

The specific RenderFlex in question is: RenderFlex#67a6e relayoutBoundary=up3 OVERFLOWING
...  needs compositing
...  parentData: offset=Offset(16.0, 16.0) (can use size)
...  constraints: BoxConstraints(0.0<=w<=379.4, 0.0<=h<=289.1)
...  size: Size(379.4, 289.1)
...  direction: vertical
...  mainAxisAlignment: start
...  mainAxisSize: min
...  crossAxisAlignment: center
...  verticalDirection: down
◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤◢◤
════════════════════════════════════════════════════════════════════════════════════════════════════

※パスは適当に変えています。

原因

RenderFlexが下部に161ピクセルオーバーフローしました。という原因

キーボードが表示されたことで、表示されるアプリのレイアウト高さが変わり、全部のWidgetが表示できなくなっている。
つまり、スクロールViewで実装してあげる必要がある。

修正概要

修正前

  body: Stack(
    children: <Widget>[
      Container(
      ・・・略(ここで、TextField実装)・・・
      ),
      ・・・略・・・
    ],
  ),

Stackで実装していました。

修正後

  body: SingleChildScrollView(
    child: Column(
      children: <Widget>[
        Container(
        ・・・略(ここで、TextField実装)・・・
        ),
        ・・・略・・・
      ],
    ),
  ),

SingleChildScrollView を使うようにしました。
今回、結果的にStackを使う必要がなかったので消してColumnを使うようにしていましたが、作る画面によって変更があるかと思われます。

コメント

タイトルとURLをコピーしました