flutter - 方向生成器给出错误的方向
问题描述
在我的应用程序中,我使用 aGridView
来显示类别网格和TextField
. 我想根据Screen Orientation
(即在Portrait mode 5 items per line
和中时landscape mode 8 items per line
)更改一行中显示的项目数。
我正在尝试通过使用OrientationBuilder
小部件来实现这一点。在我打开软键盘来编辑 TextField 之前,它工作得很好。
但是当我打开软键盘时,OrientationBuilder 会返回方向,landscape
从而导致溢出问题。
这是我的代码,
return new Scaffold(
appBar: buildFilterAppBar(context),
body: new OrientationBuilder(builder: (context, orientation) {
return new Column(
children: <Widget>[
new Expanded(
child: new ListView(
children: <Widget>[
buildContentTitle(
context, true, Icons.local_offer, '', 'Choose category'),
new GridView.count(
crossAxisCount: orientation == Orientation.portrait ? 5 : 8,
shrinkWrap: true,
physics: new NeverScrollableScrollPhysics(),
children: buildCategories(orientation),
),
new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
buildContentTitle(context, false, null,
'assets/money_pouch.png', 'Set a price range'),
new Padding(
padding: const EdgeInsets.only(left: 44.0),
child: new Text(
'Price (\u20B9)',
style: new TextStyle(
color: Theme.of(context).primaryColorDark,
fontSize: 15.0,
fontWeight: FontWeight.w600),
),
),
new Padding(
padding: const EdgeInsets.only(left: 44.0, right: 12.0),
child: new Row(
children: <Widget>[
new Flexible(
child: new TextField(
decoration: new InputDecoration(labelText: 'From'),
)),
new Container(
width: 1.0,
height: 40.0,
color: Colors.grey[700],
margin: const EdgeInsets.symmetric(horizontal: 5.0),
),
new Flexible(
child: new TextField(
decoration: new InputDecoration(labelText: 'To'),
)),
],
),
),
],
),
],
),
),
new MaterialButton(
padding: const EdgeInsets.symmetric(vertical: 15.0),
onPressed: () {},
color: Theme.of(context).accentColor,
minWidth: double.infinity,
child: new Text(
'APPLY FILTERS',
style: new TextStyle(color: Colors.white, fontSize: 16.0, fontWeight: FontWeight.w500),
),
),
],
);
}),
);
有没有其他选择而不是使用OrientationBuilder
或者有什么方法可以纠正它?
提前致谢。
解决方案
文档中已经说过
OrientationBuilder builds a widget tree that can depend on the parent widget's orientation (distinct from the device orientation).
在我打开软键盘的情况下,屏幕的宽度变得大于高度(请注意,我将高度计算为Full Screen Height - Soft Keyboard height
)。
因此landscape
, OrientationBuildercorrect behaviour
按文档返回。
因此OrientationBuilder cannot be used
对于这种情况。
为了解决这个问题,我们可以使用从MediaQuery
MediaQuery.of(context).orientation == Orientation.portrait ? 5 : 8
推荐阅读
- git - 为什么 Git Bash 不上传我的一些照片?
- lua - C_TaskQuest.GetQuestInfoByQuestID(questID) 的问题
- apache-spark - Spark 进行昂贵的 S3 API 调用
- javascript - 如何使用 AdonisJs 迁移保存数组?
- android - 需要 Appium 代码在测试用例之间重新启动应用程序
- scala - ListBuffer 的 Scala 更新尾部
- flutter - ChangeNotifierProxyProvider 创建的颤振问题
- java - Java程序根据用户输入复制文件
- vue.js - 使用 Vue.js 进行操作的路由器
- delphi - 为什么我在地址 x 处存在访问冲突?