简体   繁体   English

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

[英]How to avoid orientation changes when keyboard open with OrientationBuilder in Flutter?

how to avoid orientation changes when keyboard is opened with OrientationBuilder in Flutter?在 Flutter 中使用OrientationBuilder打开键盘时如何避免方向更改?

Indeed when the keyboard appears the orientation property of OrientationBuilder changes to landscape even if we don't rotate the phone.事实上,当键盘出现时,即使我们不旋转手机,OrientationBuilder 的方向属性也会变为横向。

Image:图片:

在此处输入图像描述

Code:代码:

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);
            }
          })),
    );
  }
}

How to avoid orientation changes when keyboard is opened with OrientationBuilder, set resizeToAvoidBottomInset to false in Scaffold widget;使用 OrientationBuilder 打开键盘时如何避免方向更改,在Scaffold小部件中将resizeToAvoidBottomInset设置为false

Image:图片:

在此处输入图像描述

Code:代码:

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',
                      ),
                    )
                  ],
                );
              }),
            ),
          )),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM