Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FlutterでScrollViewとExpandedを併用し てSignIn / SignU...

Avatar for iganin iganin
October 15, 2021

FlutterでScrollViewとExpandedを併用し てSignIn / SignUp画面 など レイアウトを作成する

Mobile Act Online #6 登壇資料

Avatar for iganin

iganin

October 15, 2021

More Decks by iganin

Other Decks in Technology

Transcript

  1. 対処法1と問題 - ScaffoldのresizeToAvoidBottomInset Scaffold { resizeToAvoidBottomInset: false, body: ... }

    ScaffoldのresizeToAvoidBottomInsetをfalse にすることでひとまずの対処は可能
  2. 対処法2と問題 - SingleChildScrollView SingleChildScrollView( child: Column( children: [...] ) )

    検索するとよく出てくる ScrollViewないでキーボードの高さ分を調整してく れる 多くの場合、この方法で問題ない ただし、Columnのchildrenに SpacerやExpandedなどを含むと レイアウトが崩れる
  3. 対処法2と問題 - SingleChildScrollView RenderBox was not laid out: RenderRepaintBoundary#f85cc relayoutBoundary=up1

    NEEDS-PAINT 'package:flutter/src/rendering/box.dart': Failed assertion: line 1930 pos 12: 'hasSize' 例えば左のような例外が発生する SpacerやExpandedは可能な限りスペースを取ろう とする SingleChildScrollViewは内部の高さから全体の高 さを決める したがって、高さが不定となりレイアウトが崩れる
  4. 対処法3 - IntrinsicHeight 実は今回のケースに対する対応法は公式ドキュメントに書かれている Simply doing so, however, usually results

    in a conflict between the Column, which typically tries to grow as big as it can, and the SingleChildScrollView, which provides its children with an infinite amount of space.To resolve this apparent conflict, there are a couple of techniques, as discussed below. https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html
  5. 対処法3 - IntrinsicHeight LayoutBuilder( builder: (BuildContext context, BoxConstraints viewportConstraints) {

    return Widget(); } ) 親Widgetでとりうる最大の高さなどをconstraintsとして取得できる
  6. 対処法3 - IntrinsicHeight 実装(主要部分のみ) LayoutBuilder( builder: (context, constraints) { return

    SingleChildScrollView( child: ConstrainedBox( constraints: BoxConstraints(minHeight: constraints.maxHeight), child: IntrinsicHeight(....), …... minHeightを親WidgetのmaxHeightに指定する IntrinsicHeightのchildの高さが親Viewより小さい場 合でも内部の要素が親 View一杯に広がる
  7. 対処法3 - IntrinsicHeight 実装(主要部分のみ) LayoutBuilder( builder: (context, constraints) { return

    SingleChildScrollView( child: ConstrainedBox( constraints: BoxConstraints(minHeight: constraints.maxHeight), child: IntrinsicHeight(....), …... IntrinsicHeightを使用することで、 ScrollView内の高さが無限大になることを防ぐ
  8. 対処法3 - IntrinsicHeight 実装(主要部分のみ) LayoutBuilder( builder: (context, constraints) { return

    SingleChildScrollView( child: ConstrainedBox( constraints: BoxConstraints(minHeight: constraints.maxHeight), child: IntrinsicHeight(....), …... SingleChildScrollViewを使用することでキーボードの 問題に対処する また、IntrinsicHeightの中身の高さが親Widgetより高 くなった場合のレイアウト崩れを防止
  9. 対処法3 - IntrinsicHeight 注意点 IntrinsicHeightは計算コストが高い 最悪 O(N^2) This class is

    relatively expensive, because it adds a speculative layout pass before the final layout phase. Avoid using it where possible. In the worst case, this widget can result in a layout that is O(N²) in the depth of the tree. 複雑なレイアウト構成ではできれば避けたい