简体   繁体   English

幻灯片转换无法正常工作 flutter

[英]Slide transition not working correctly flutter

I am using a method keyboardSlide which is being passed into my login_page.dart, the method is using a conditional which will slide the keyboard on and off the screen when keyboardUp is true or false.我正在使用一个方法keyboardSlide,它被传递到我的login_page.dart,该方法使用一个条件,当keyboardUp为真或假时,它将在屏幕上滑动键盘。 I have the condition being set to true when I click on the username field, and false when I click on the password field, I pasted part of the code for my login screen below.当我点击用户名字段时,我将条件设置为 true,当我点击密码字段时,我将条件设置为 false,我在下面粘贴了登录屏幕的部分代码。 The problem I am having trouble understanding is how to do the animation automatically, I can hard code it and it will work, but I need to know how to have the animation be rebuilt every time the Boolean is changed from true to false so the keyboard will slide up and down.我难以理解的问题是如何自动执行 animation,我可以对其进行硬编码并且它会工作,但我需要知道如何在每次将 Z27226C864BAC7454A8504F8EDBZ15 键盘从 true 更改为 false 时重建 animation会上下滑动。

Here are the images with and without the keyboard.这是有和没有键盘的图像。

在此处输入图像描述 在此处输入图像描述

SlideTransistion method SlideTransition 方法

SlideTransition keyboardSlide(AnimationController controller1, Widget Keyboard) {
 if (keyboardUp == true) {
   return SlideTransition(
     position: Tween<Offset>(
       begin: Offset(0,1),
       end: Offset(0, 0),
     ).animate(controller1),
     child: Padding(
       padding: const EdgeInsets.only(top: 300),
       child: Container(
         child: Keyboard,
       ),
     ),
   );
 } else {
   return SlideTransition(
     position: Tween<Offset>(
       begin: Offset(0,0),
       end: Offset(0, 1),
     ).animate(controller1),
     child: Padding(
       padding: const EdgeInsets.only(top: 300),
       child: Container(
         child: Keyboard,
       ),
     ),
   );
 }
}

Part of login_page.dart login_page.dart的一部分

 @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        child: Stack(
          children: <Widget>[
            keyboardSlide(_controller, toggleKeyboard()),
            Positioned(
                top: 652,
                left: 0,
                child: Container(
                    width: 1280,
                    height: 100,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                          image: AssetImage('assets/login/rectangle_112.png'),
                          fit: BoxFit.fitWidth
                      ),
                    )
                )
            ),
            Positioned(
                top: 652,
                left: 930,
                child: Container(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        SizedBox(
                          child: IconButton(
                            padding: EdgeInsets.all(0.0),
                            // iconColor() is grabbing the icon based on the isUserFilled & isPasswordFilled
                            // This will cause the icon to turn from grey to white when both fields are filled
                            icon: Image.asset(iconColor()),
                            onPressed: () {
                              Navigator.pushNamed(context, '/menu');
                            },
                          ),
                        ),
                        Text(
                          'Login',
                          // textColor() is grabbing the color based on the isUserFilled & isPasswordFilled
                          // This will cause the login text to turn from grey to white when both fields are filled
                          style: TextStyle(color: textColor()),
                        ),
                      ],
                    ),
                    width: 350,
                    height: 100,
                    decoration: BoxDecoration(
                      // backgroundColor() is grabbing the color based on the isUserFilled & isPasswordFilled
                      // This will cause the login button to turn from grey to green when both fields are filled
                      color: backgroundColor(),
                    )
                )

            ), Positioned(
                top: userFieldHeight,
                left: 201,
                child: Container(
                    child: TextField(
                      // Checking the UserName textField for user input, returns a boolean to isUserFilled
                     controller: myController1,
                      readOnly: true,
                      showCursor: true,
                      onTap: () {
                        userSelected = true;
                        keyboardUp = true; // This boolean being passed to keyboardSlide
                      },
                      onChanged: (input) {
                        setState(() {
                          (input.isEmpty) ? isUserFilled = false: isUserFilled = true;
                        });
                      },
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                      ),
                    ),
                    width: 400,
                    height: 60,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(255, 255, 255, 1),
                    )
                )
            ), Positioned(
                top: userFieldHeight, // original is 350
                left: 640,
                child: Container(
                    child: TextField(
                      style: TextStyle(fontSize: 24),
                     controller: myController2,
                      readOnly: true,
                      showCursor: true,
                      obscureText: true,
                      onTap: () {
                        userSelected = false;
                        keyboardUp = false; // This boolean being passed to keyboardSlide
                      },
                      // Checking the Password textField for user input, returns a boolean to isPasswordFilled
                      onChanged: (input) {
                        setState(() {
                          (input.isEmpty) ? isPasswordFilled = false: isPasswordFilled = true;
                        });
                      },
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                      ),
                    ),

I was actually able to make it work by creating a conditional.我实际上能够通过创建条件来使其工作。 If you add a status listener to the controller you can see two values AnimationStatus.completed and AnimationStatus.dismissed.如果您向 controller 添加状态侦听器,您可以看到两个值 AnimationStatus.completed 和 AnimationStatus.dismissed。 You can set a boolean where if the animation is completed this means the keyboard is up and vice versa.您可以设置 boolean,如果 animation 完成,则表示键盘已启动,反之亦然。 then you can put another conditonal statement inside the onpressed or onTap function which can then toggle the animation.然后您可以在 onpressed 或 onTap function 中放置另一个条件语句,然后可以切换 animation。 Im not sure if this is the best way to do this, but it does the trick.我不确定这是否是最好的方法,但它确实有效。

_controller.addStatusListener((status) {
      print(status);
      if (status == AnimationStatus.completed){
        keyboardUp = true;
      }
      if (status == AnimationStatus.dismissed) {
        keyboardUp = false;
      }
    });
TextField(
                      // Checking the UserName textField for user input, returns a boolean to isUserFilled
                     controller: userNameController,
                      readOnly: true,
                      showCursor: true,
                      onTap: () {
                       setState(() {
                         keyboardUp ? _controller.reverse() : _controller.forward();
                       });
                      },
                      onChanged: (input) {
                        setState(() {
                          (input.isEmpty) ? isUserFilled = false: isUserFilled = true;
                        });
                      },
                      decoration: InputDecoration(
                        border: OutlineInputBorder(),
                      ),
                    ),

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

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