0

关闭键盘时如何让AnimatedContainer恢复到之前的状态(1)?目前通过 onTap 改变容器的位置

Widget _formAuth(String labelForm, String labelButton, void func()) {
    return AnimatedContainer(
      duration: Duration(seconds: 1),
      curve: Curves.ease,
      margin: EdgeInsets.fromLTRB(13, _marginFormAuth, 13, 50),
Widget _inputLoginOrPassword(
      Icon icon, String hint, TextEditingController controller, bool obscure) {
    return Container(
      margin: EdgeInsets.fromLTRB(10, 35, 10, 0),
      child: TextField(
        onTap: () {
          setState(() {
            _marginFormAuth = 20;
          });
        },

在此处输入图像描述

4

2 回答 2

1

您可以在状态类中定义一个布尔变量,并在 build 方法中为其赋值,如下所示:

bool isKeyboardOpened;

build () {
isKeyboardOpened = MediaQuery.of(context).viewInsets.bottom > 0;
_marginFormAuth = isKeyboardOpened ? 20 : whateverYouHadBefore;
...
}

这种在打开键盘时移动容器的解决方案并不是最好的方法,但是,您应该修改代码背后的整个概念,并使用当您聚焦一个字段时自然滚动的可滚动对象:)

于 2021-02-19T09:39:45.790 回答
0

检测键盘状态使用:https ://github.com/adee42/flutter_keyboard_visibility

import 'package:keyboard_visibility/keyboard_visibility.dart';

@protected
void initState() {
  super.initState();

  KeyboardVisibilityNotification().addNewListener(
    onChange: (bool visible) {
      print(visible);
    },
  );
}

要为容器设置动画,请使用 AnimationController 而不是 AnimatedContainer:https ://flutter.dev/docs/development/ui/animations/tutorial

于 2021-02-19T09:46:10.823 回答