简体   繁体   English

Flutter - 如何在键盘出现时使用 SingleScrollView() 滚动整个屏幕,而不仅仅是自动显示文本

[英]Flutter - how to use SingleScrollView() to Scroll Entire Screen when Keyboard Appears not just text automatically

Even After using SingleChildScrollView() as main Parent, still only the text field is shifted up, but i need to shift up the Login button as well.即使在使用SingleChildScrollView()作为主父级之后,仍然只有文本字段向上移动,但我也需要向上移动登录按钮。

The output of my code is Left image but i need the result of right.我的代码的 output 是左图,但我需要右图的结果。

I have used Material to custom made a Button.我已经使用 Material 定制了一个 Button。

在此处输入图像描述

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_catalog/utils/routes.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String name = "";
  bool changeButton = false;
  final _formKey = GlobalKey<FormState>();

  moveToHome(BuildContext context) async {
    if (_formKey.currentState!.validate()) {
      setState(() {
        changeButton = true;
      });

      await Future.delayed(Duration(seconds: 1));
      await Navigator.pushNamed(context, MyRoutes.homeRoute);
      setState(() {
        changeButton = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: true,
        // color: Colors.white,
        body: Center(
          child: SingleChildScrollView(
              child: Form(
            key: _formKey,
            child: Column(
              children: [
                Image.asset(
                  "assets/images/login.png",
                  fit: BoxFit.cover,
                  height: 100,
                ),
                SizedBox(height: 24.0),
                Text("Welcome, $name",
                    style: TextStyle(
                      fontSize: 24,
                      fontWeight: FontWeight.bold,
                    )),
                SizedBox(
                  height: 20.0,
                ),
                Padding(
                  // padding: const EdgeInsets.all(16.0),  //everywhere padding
                  padding: const EdgeInsets.symmetric(
                      vertical: 16.0, horizontal: 20.0),
                  child: Column(
                    children: [
                      TextFormField(
                          decoration: InputDecoration(
                            hintText: "Enter Username",
                            hintStyle: TextStyle(
                                height: 2, fontWeight: FontWeight.bold),
                            labelText: "Username",
                          ),
                          onChanged: (value) {
                            name = value;
                            setState(() {});
                          },
                          validator: (value) {
                            if (value!.isEmpty)
                              return "Username cannot be Empty";
                            else
                              return null;
                          }),
                      TextFormField(
                          obscureText: true, //wont show text
                          decoration: InputDecoration(
                            hintText: "Enter Password",
                            hintStyle: TextStyle(
                                height: 2, fontWeight: FontWeight.bold),
                            labelText: "Password",
                          ),
                          validator: (value) {
                            if (value!.isEmpty)
                              return "Password Cannot be Empty";
                            else if (value.length < 6)
                              return "Password must be Greater than 6 Length";
                          }),
                    ],
                  ),
                ),
                SizedBox(
                  height: 20.0,
                ),

                Material(
                  color: Colors.deepPurple,
                  borderRadius: BorderRadius.circular(changeButton ? 50 : 8),
                  child: InkWell(
                    // splashColor: Colors.redAccent,
                    onTap: () => moveToHome(context),
                    child: AnimatedContainer(
                      duration: Duration(seconds: 1),
                      width: (changeButton ? 50 : 150),
                      height: 50,
                      //you can keep color at one place
                      alignment: Alignment.center,
                      child: changeButton
                          ? Icon(Icons.done, color: Colors.white)
                          : Text(
                              "Login",
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 18),
                            ),
                    ),

                    // ElevatedButton(
                    //   child: Text('Login'),
                    //   style: TextButton.styleFrom(minimumSize: Size(150, 40)),
                    //   onPressed: () {
                    //     Navigator.pushNamed(context, MyRoutes.homeRoute);
                    //     // print('hi');
                    //   },
                  ),
                ) // ),
              ],
            ),
          )),
        ));
  }
}

Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole code Ignore This, This is used to allow stackoverflow to show whole codeIgnore This, This is used to allow stackoverflow Ignore This,这是用来让 stackoverflow 显示整个代码 Ignore This,这是用来让 stackoverflow 显示整个代码 Ignore This,这是用来让 stackoverflow 显示整个代码 Ignore This,这是用来让 stackoverflow 显示整个代码 忽略这个,这个用来让stackoverflow显示整个代码 忽略这个,这个用来让stackoverflow显示整个代码 忽略这个,这个用来让stackoverflow显示整个代码 忽略这个,这个用来允许stackoverflow显示整个代码 Ignore This,这是用于允许 stackoverflow 显示整个代码 Ignore This,这是用于允许 stackoverflow 显示整个代码 Ignore This,这是用于允许 stackoverflow 显示整个代码 Ignore This,这是用于允许 stackoverflow 显示整个代码 Ignore This,这是用于允许 stackoverflow 显示整个代码 Ignore This,这是用于允许 stackoverflow 显示整个代码Ignore This,这是用于允许 stackoverflow to show whole codeIgnore This, This is used to allow stackoverflow to show whole codeIgnore This, This is used to allow stackoverflow to show whole codeIgnore This, This is used to allow stackoverflow to show whole code to show whole codeIgnore This, This is used to allow stackoverflow to show whole codeIgnore This, This is used to allow stackoverflow to show whole codeIgnore This, This is used to allow stackoverflow to show whole code

scrollPadding: EdgeInsets.only(bottom: 150),

to the TextFormField到 TextFormField

SingleChildScrollView(... SingleChildScrollView(...

reverse: false,反向:错误,

... ...

) )

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

相关问题 键盘出现时如何滚动我的整个布局? - How to scroll my entire layout when the keyboard appears? 出现键盘时向上滚动屏幕 - Scroll the screen up when keyboard appears 当键盘出现在屏幕上时,android Scroll Layout向上 - android Scroll Layout upwards when keyboard appears on Screen 当虚拟键盘出现在本机反应中时如何自动向上滚动屏幕(在 android 上) - How to auto scroll the screen up when virtual keyboard appears in react native (on android) 当键盘显示时,Android使整个屏幕滚动到特定点 - Android make entire screen scroll to certain point when keyboard shows Flutter:当键盘出现在 android 时屏幕不向上滚动 - Flutter : screen not scrolling up when keyboard appears in android 出现键盘时导致滚动视图无法滚动以编辑文本的标题 - Header causing scrollview to not scroll to edit text when keyboard appears 添加文本字段时,Flutter 屏幕显示为空 - Flutter screen appears empty when adding text field Android:当键盘出现时如何滚动滚动视图以显示下一个字段 - Android: how to scroll scrollview to show next field when keyboard appears 在 flutter 中,如何在不重建键盘的情况下调整页面大小? - How to resize a page when the keyboard appears without rebuilding it in the flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM