dart TextFieldWidget文本在失去焦点抖动时自动清除

ao218c7q  于 2023-07-31  发布在  其他
关注(0)|答案(3)|浏览(89)

我做了一个简单的登录和注册UI,但是当我用Firebase连接更新我的应用程序时,我发现了这个bug。我的textfield小部件文本正在从登录页面清除,而不是从注册页面虽然我复制了我的注册页面代码从我的登录页面,问题只存在于登录页面。
这是我登录页面代码

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:tutorial_two/widgets/widget_text_field.dart';
import 'package:velocity_x/velocity_x.dart';
import '../utils/auth_controller.dart';
import '../utils/routes.dart';

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

  @override
  Widget build(BuildContext context) {
    var emailLoginController = TextEditingController();
    var passwordLoginController = TextEditingController();

    double h = MediaQuery.of(context).size.height;
    return Scaffold(
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Image.asset(
                "assets/images/flutter_logo_modified.png",
                height: 200,
                width: 200,
              ),
              Container(
                width: double.infinity,
                margin: const EdgeInsets.only(left: 20, right: 20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      "Welcome",
                      style:
                          TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
                    ),
                    Text(
                      "We are glad to see you back here",
                      style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.grey[350],
                      ),
                    ),
                    const SizedBox(
                      height: 50,
                    ),
                    TextFieldWidget(
                        hint: "Enter E-mail",
                        obscuretext: false,
                        prefixiconCode: "0xe22a",
                        prefixiconfamily: "MaterialIcons",
                        textFieldController: emailLoginController),
                    const SizedBox(
                      height: 20,
                    ),
                    TextFieldWidget(
                        hint: "Enter Password",
                        obscuretext: true,
                        prefixiconCode: "0xf0050",
                        prefixiconfamily: "MaterialIcons",
                        textFieldController: passwordLoginController),
                    const SizedBox(
                      height: 30,
                    ),
                    Row(
                      children: [
                        Expanded(
                          child: Container(),
                        ),
                        Text(
                          "Forgot Paswword?",
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.bold,
                            color: Colors.grey[500],
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 30,
              ),
              Container(
                width: 140,
                height: 50,
                decoration: BoxDecoration(boxShadow: [
                  BoxShadow(
                    blurRadius: 10,
                    spreadRadius: 7,
                    offset: const Offset(1, 1),
                    color: Colors.grey.withOpacity(0.1),
                  ),
                ], borderRadius: BorderRadius.circular(40)),
                child: ElevatedButton(
                  onPressed: () {
                    AuthController.instance.login(
                      context,
                      emailLoginController.text.toString(),
                      passwordLoginController.text.toString(),
                    );
                  },
                  style: ButtonStyle(
                    shadowColor: MaterialStateProperty.all(
                        context.theme.scaffoldBackgroundColor),
                  ),
                  child: const Text("Login"),
                ),
              ),
              SizedBox(height: h * 0.08),
              RichText(
                text: TextSpan(children: <TextSpan>[
                  TextSpan(
                    text: 'Don\'t have an account? ',
                    style: TextStyle(
                      fontSize: 18,
                      color: Colors.grey[500],
                      fontFamily: GoogleFonts.syne().fontFamily,
                    ),
                  ),
                  TextSpan(
                      text: 'Create account',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 18,
                        color: context.theme.cardColor,
                        fontFamily: GoogleFonts.syne().fontFamily,
                      ),
                      recognizer: TapGestureRecognizer()
                        ..onTap = () {
                          Navigator.pushReplacementNamed(
                              context, MyRoutes.signupRoute);
                        }),
                ]),
              )
            ],
          ),
        ),
      ),
    );
  }
}

字符串
下面是我制作文本域小部件

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:velocity_x/velocity_x.dart';

class TextFieldWidget extends StatelessWidget {
  final String hint;
  final String label;
  final String prefixiconCode;
  final String prefixiconfamily;
  final String suffixiconCode;
  final String suffixiconfamily;
  final TextEditingController textFieldController;
  final bool obscuretext;

  const TextFieldWidget(
      {super.key,
      this.hint = "",
      this.label = "",
      this.prefixiconCode = "",
      this.prefixiconfamily = "MaterialIcons",
      this.suffixiconCode = "",
      this.suffixiconfamily = "",
      required this.textFieldController,
      this.obscuretext = false});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          color: context.theme.scaffoldBackgroundColor,
          borderRadius: BorderRadius.circular(30),
          boxShadow: [
            BoxShadow(
              blurRadius: 10,
              spreadRadius: 7,
              offset: const Offset(1, 1),
              color: context.theme.canvasColor.withOpacity(0.3),
            ),
          ]),
      child: TextField(
        controller: textFieldController,
        style: TextStyle(
            color: context.theme.cardColor,
            fontFamily: GoogleFonts.syne().fontFamily),
        decoration: InputDecoration(
          fillColor: context.theme.scaffoldBackgroundColor,
          focusColor: context.theme.scaffoldBackgroundColor,
          iconColor: context.theme.scaffoldBackgroundColor,
          prefixIcon: Icon(
            IconData(int.parse(prefixiconCode), fontFamily: prefixiconfamily),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(30),
            borderSide: BorderSide(
                color: context.theme.scaffoldBackgroundColor, width: 1.0),
          ),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(30),
            borderSide: BorderSide(
                color: context.theme.scaffoldBackgroundColor, width: 1.0),
          ),
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(30),
          ),
          hintText: hint,
        ),
        obscureText: obscuretext,
      ),
    );
  }
}


和注册页面代码,以防有帮助我已经看过github和堆栈溢出的解决方案,但没有一个适合我。

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:tutorial_two/utils/auth_controller.dart';
import 'package:tutorial_two/utils/routes.dart';
import 'package:velocity_x/velocity_x.dart';
import '../widgets/widget_text_field.dart';

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

  @override
  Widget build(BuildContext context) {
    var emailController = TextEditingController();
    var usernameController = TextEditingController();
    var passwordController = TextEditingController();

    return Scaffold(
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            children: [
              Image.asset(
                "assets/images/flutter_logo_modified.png",
                height: 200,
                width: 200,
              ),
              Container(
                width: double.infinity,
                margin: const EdgeInsets.only(left: 20, right: 20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      "Hello There",
                      style:
                          TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
                    ),
                    Text(
                      "We are glad to welcome you here",
                      style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.grey[350],
                      ),
                    ),
                    const SizedBox(
                      height: 50,
                    ),
                    TextFieldWidget(
                      hint: "Enter E-mail",
                      obscuretext: false,
                      prefixiconCode: "0xe22a",
                      prefixiconfamily: "MaterialIcons",
                      textFieldController: emailController,
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    TextFieldWidget(
                      hint: "Enter Username",
                      obscuretext: false,
                      prefixiconCode: "0xf01f3",
                      prefixiconfamily: "MaterialIcons",
                      textFieldController: usernameController,
                    ),
                    const SizedBox(
                      height: 20,
                    ),
                    TextFieldWidget(
                      hint: "Enter Password",
                      obscuretext: true,
                      prefixiconCode: "0xf0050",
                      prefixiconfamily: "MaterialIcons",
                      textFieldController: passwordController,
                    ),
                    const SizedBox(
                      height: 30,
                    ),
                    Row(
                      children: [
                        Expanded(
                          child: Container(),
                        ),
                        RichText(
                          text: TextSpan(children: <TextSpan>[
                            TextSpan(
                              text: 'Already have an account? ',
                              style: TextStyle(
                                fontSize: 18,
                                color: Colors.grey[500],
                                fontFamily: GoogleFonts.syne().fontFamily,
                              ),
                            ),
                            TextSpan(
                                text: 'Login',
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 18,
                                  color: context.theme.cardColor,
                                  fontFamily: GoogleFonts.syne().fontFamily,
                                ),
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () {
                                    Navigator.pushReplacementNamed(
                                        context, MyRoutes.loginRoute);
                                  }),
                          ]),
                        )
                      ],
                    ),
                  ],
                ),
              ),
              const SizedBox(
                height: 30,
              ),
              Container(
                width: 140,
                height: 50,
                decoration: BoxDecoration(boxShadow: [
                  BoxShadow(
                    blurRadius: 10,
                    spreadRadius: 7,
                    offset: const Offset(1, 1),
                    color: Colors.grey.withOpacity(0.1),
                  ),
                ], borderRadius: BorderRadius.circular(40)),
                child: ElevatedButton(
                  onPressed: () {
                    AuthController.instance.register(
                      context,
                      emailController.text.toString(),
                      usernameController.text.toString(),
                      passwordController.text.toString(),
                    );
                  },
                  style: ButtonStyle(
                    shadowColor: MaterialStateProperty.all(
                        context.theme.scaffoldBackgroundColor),
                  ),
                  child: const Text("Signup"),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}


输出:https://youtu.be/UMKgY3Zatjw

flseospp

flseospp1#

1.将您的登录屏幕转换为有状态小部件。
1.在build方法之外声明控制器,并将其标记为final并使其私有
1.初始化并以适当覆盖方法(即,initState和dispose)

m2xkgtsf

m2xkgtsf2#

请将TEXT EDITING CONTROLLER置于构建方法之外

class xyz extends StatefulWidget{

   var emailController = TextEditingController();
   var usernameController = TextEditingController();
   var passwordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
     return Container();
   }

}

字符串

f8rj6qna

f8rj6qna3#

我的错误修复了此代码RichText(text:TextSpan(识别器:TapGestureRecognizer()..onTap =()=> Get.to(()=> const SignInPage()),text:“已经有账号了?“,style:TextStyle(fontSize:Dimensions.font26,颜色:颜色.灰色[500])),
我忘记了这个代码Get.to(()=> const SignInPage()
这里是我的登录页面RichText(text:TextSpan(文本:“没有账号?“,style:TextStyle(fontSize:Dimensions.font20,颜色:Colors.grey[500]),儿童:[ TextSpan(识别器:TapGestureRecognizer()..onTap =()=> Get.to(()=> const SignUpPage(),transition:Transition.fadeIn),文本:“创建”,样式:TextStyle(fontWeight:FontWeight.bold,fontSize:Dimensions.font26,颜色:Colors.black54),)]))
你需要这个代码-> recognizer:TapGestureRecognizer()..onTap =()=> Get.to(()=> const SignInPage())

相关问题