gpt4 book ai didi

flutter - 在 Flutter 中使用 OrientationBuilder 打开键盘时如何避免方向改变?

转载 作者:行者123 更新时间:2023-12-05 06:03:18 27 4
gpt4 key购买 nike

在 Flutter 中使用 OrientationBuilder 打开键盘时如何避免方向改变?

确实,当键盘出现时,即使我们不旋转手机,OrientationBuilder 的方向属性也会更改为横向。

图片:

enter image description here

代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: OrientationBuilder(builder: (context, orientation) {
var _children = [
Expanded(
child: Center(
child: Text('Orientation : $orientation',
textAlign: TextAlign.center))),
Flexible(
child: Container(color: Colors.blue, child: TextField())),
Flexible(child: Container(color: Colors.yellow)),
];
if (orientation == Orientation.portrait) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: _children);
} else {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: _children);
}
})),
);
}
}

最佳答案

如何在使用 OrientationBuilder 打开键盘时避免方向改变,在 Scaffold 小部件中将 resizeToAvoidBottomInset 设置为 false

图片:

enter image description here

代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
resizeToAvoidBottomInset: false, // Here
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: OrientationBuilder(builder: (context, orientation) {
return Column(
children: [
Text('Orientation = $orientation',
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold)),
TextField(
decoration: InputDecoration(
hintText: 'tap me',
),
)
],
);
}),
),
)),
);
}
}

关于flutter - 在 Flutter 中使用 OrientationBuilder 打开键盘时如何避免方向改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66692514/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com