【问题标题】:How to call input value in flutter?如何在颤动中调用输入值?
【发布时间】:2021-08-20 02:09:37
【问题描述】:

我只有一个问题

我昨天开始学扑。

如何获取 Textfield 值?

我尝试在班级中的任何人调用 TextEditingController。 但它不起作用。

[![在此处输入图片描述][1]][1]

class LoginScreen extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<LoginScreen> {
  final userId = TextEditingController();
  final userPass = TextEditingController();

  ...TextInputField(),
          PasswordInput(),
          LoginButton()

}

void loginAct() async {
  var data = {
    "user_id": userId,   //undefined
    "user_pass": userPass,  //undefined
  };



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

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 10.0),
      child: Container(
        height: size.height * 0.08,
        width: size.width * 0.8,
        decoration: BoxDecoration(
          color: Colors.grey[500],
          borderRadius: BorderRadius.circular(16),
        ),
        child: TextField(
          controller: userId,  <=== (The getter 'userId' isn't defined for the class 'TextInputField')
          decoration: InputDecoration(
            border: InputBorder.none,
            prefixIcon: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20.0),
              child: Icon(
                FontAwesomeIcons.user,
                size: 28,
                color: Colors.black54,
              ),
            ),
            hintText: 'id',
          ),
        ),
      ),
    );
  }
}

   

如何解决?

【问题讨论】:

  • 您要登录并将您的数据传递给 API 是否正确?

标签: flutter


【解决方案1】:

如果颤振,你可以通过不同的方法获取文本字段值

  1. 通过文本编辑控制器

在上面添加这个

final userNameController = TextEditingController();
final passwordController = TextEditingController();

然后在文本字段上

TextField(
  controller: userNameController,
);

 TextField(
  controller: passwordController,
);

在函数上传递你喜欢的值

onPressed: () => loginAct(userNameController.text,passwordController.text),

在这种情况下,您需要通过控制器指定初始值是什么

第二个问题

http.get(Uri.https('https://swapi.co', 'api/people'));

http.get(Uri.parse('https://swapi.co/api/people'));

【讨论】:

  • 感谢您的教导。但我没能解决这个问题。我尝试在顶级控制器中调用最终的 userId = TextEditingController() 但它发生了定义的类错误所以..我添加了有问题的完整代码。谢谢
  • 我为您添加了另一个答案,您得到的错误是由于您使用了不同的小部件类,文本编辑控制器通常用于其状态完整或无状态小部件
【解决方案2】:

在你的代码上试试这个

class TextInputField  extends StatelessWidget {
  const TextInputField({ Key? key }) : super(key: key);
@override
Widget build(BuildContext context) {
  final userIdController = TextEditingController();
  final passwordController = TextEditingController();
       return Container(
          child: TextField(
         controller: userId, 
         onpressed:  () {loginAct(password:passwordController.text,userName:userIdController. 
          text)})
   );
 
}

您的登录功能

   void loginAct({String password, String userName}) async {
   var data = {
  "user_id": password,  
  "user_pass": userName, 
  }

【讨论】:

  • 所以.. 当我在类中初始化时如何从 loginAct() 调用?我尝试从每个类中调用变量TextInputField { final userId ... }PasswordInput { final userPass ...}LoginButton { onPressed : loginAct() }void loginAct() {`var data = {`"user_id" : userId.text //undefined userId... } }
  • 等待我将更新答案。文本是访问控制器定义的地方
  • 我将分离的小部件类合并为一个并尝试并成功。非常感谢兄弟!!
猜你喜欢
  • 2019-04-09
  • 2021-06-28
  • 1970-01-01
  • 2020-05-07
  • 2022-11-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多