How to detect orientation change in layout in Flutter?

后端 未结 6 1381
天命终不由人
天命终不由人 2020-12-09 07:56

How to find out Orientation is portrait or landscape in Flutter

if(portrait){
  return ListView.builder()
}else{
  return GridView.count()
}
6条回答
  •  感情败类
    2020-12-09 08:10

    For completeness sake, I would like to add another way to detect orientation in Flutter. Two ways to detect have been mentioned in the answers already. They are

    1. Media Query
    2. Orientation Builder

    There is a third way which I came across when learning Flutter from Responsive Design video (skip to minute 2:34) by Google Engineers. It's called Layout Builder. Here is short snippet:

    return Padding(
        padding: _padding,
        child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
                if(constraints.maxHeight > constraints.maxWidth) {
                    return _getPortraitLayout();
                }
                else {
                    return _getLandscapeLayout();
                }
            },
        ),
    );
    

提交回复
热议问题