flutter 即使resizeToAvoidBottomInset为false或SingleChildScrollView,键盘也会溢出

vltsax25  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(282)

当选择一个文本字段时,键盘弹出,我得到溢出消息,并注意到有我的背景的容器也移动了,即使使用resizeToAvoidBottomInset。我也尝试了SingleChildScrollView,这导致了一个约束错误。我猜我的背景不在它应该在的地方,但我不确定我应该把它放在哪里,或者我的错误是在其他地方。WillPopScope()使resizeToAvoidBottomInset停止工作,但如果是这种情况,我如何锁定后退按钮?

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:studentcompanion/constants/colors.dart';
import 'package:http/http.dart' as http;
import 'package:studentcompanion/core/myDB.dart';
import 'package:studentcompanion/screens/home/Menu.dart';
import '../../core/session.dart';
import '../profile/student.dart';

class Login extends StatelessWidget {
  Login({Key? key}) : super(key: key);

  final _emailController = TextEditingController();
  final _passwordController = TextEditingController();

  Future<bool> _onWillPop() async {
    return false; 
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
          resizeToAvoidBottomInset: false,
          body: Container(
              decoration: BoxDecoration(
                  gradient: LinearGradient(
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                      colors: [
                    ColorConstant.bluegray200,
                    ColorConstant.pink100
                  ])),
              child: SizedBox(
                  child: Column(
                children: [
                  Padding(
                      padding: const EdgeInsets.only(top: 60),
                      child: Image.asset('assets/images/Icon.png')),
                  const Padding(
                      padding: EdgeInsets.only(top: 20),
                      child: Text(
                        '¡Bienvenido(a)!',
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 35,
                            fontFamily: 'Jost',
                            fontWeight: FontWeight.bold),
                      )),
                  const SizedBox(height: 30),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 25.0),
                    child: Container(
                        decoration: BoxDecoration(
                            color: Colors.grey[100],
                            border: Border.all(color: Colors.white),
                            borderRadius: BorderRadius.circular(12)),
                        child: Padding(
                          padding: const EdgeInsets.only(left: 20.0),
                          child: TextField(
                            controller: _emailController,
                            decoration: const InputDecoration(
                                border: InputBorder.none, hintText: 'Email'),
                          ),
                        )),
                  ),
                  const SizedBox(height: 10),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 25.0),
                    child: Container(
                        decoration: BoxDecoration(
                            color: Colors.grey[100],
                            border: Border.all(color: Colors.white),
                            borderRadius: BorderRadius.circular(12)),
                        child: Padding(
                          padding: const EdgeInsets.only(left: 20.0),
                          child: TextField(
                            controller: _passwordController,
                            obscureText: true,
                            decoration: const InputDecoration(
                                border: InputBorder.none,
                                hintText: 'Contraseña'),
                          ),
                        )),
                  ),
                  Padding(
                      padding: const EdgeInsets.only(top: 40),
                      child: InkWell(
                        child: Container(
                            height: 50,
                            width: 150,
                            margin: const EdgeInsets.all(10),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(20),
                                boxShadow: [
                                  BoxShadow(
                                    color: Colors.grey.withOpacity(0.2),
                                    spreadRadius: 3,
                                    blurRadius: 7,
                                    offset: const Offset(0, 5),
                                  )
                                ]),
                            child: const Center(
                                child: Text('Ingresar',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 20,
                                        fontFamily: 'Jost',
                                        fontWeight: FontWeight.w400)))),
                        onTap: () {
                          login(context);
                        },
                      )),
                  const Spacer(),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Image.asset('assets/images/Waves.png',
                        width: double.infinity, fit: BoxFit.cover),
                  )
                ],
              )))),
    );
  }

先谢谢你!
已尝试使用resizeToAvoidBottomInset = false和SingleChildScrollView,未产生差异或约束错误。

cygmwpex

cygmwpex1#

最后,我去掉了容器,只在Scaffold中有一个列,然后使用了覆盖列间距的SingleChildScrollView,所以我用maxHeight: MediaQuery.of(context).size.height将列 Package 在ConstrainedBox中,它最终按照我想要的方式工作。

相关问题