如何在 Flutter 中使用 OrientationBuilder 打开键盘时避免屏幕方向改变 ?
实际上,当键盘弹出时,即使我们没有旋转手机,OrientationBuilder 的方向属性也会更改为横向。
图像:
代码:
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);
}
})),
);
}
}