在 flutter 中制作 TextFormField 全局小部件

问题描述

我正在为应用程序制作一个全局 TextFormField 小部件。但它没有在控制器中返回数据。 我的全局文本表单字段小部件:请告诉我我做错了什么。我正在 SignUp person 小部件中初始化控制器。我还想验证验证器中的文本表单字段。

import 'package:flutter/material.dart';

class GlobalTextField extends StatelessWidget {
  final Widget fieldIcon;
  final String fieldText;
  final TextEditingController fieldController;
  final bool isEnabled;

  const GlobalTextField(
    this.fieldIcon,this.fieldText,this.fieldController,[
    this.isEnabled,]);
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: fieldController,enabled: isEnabled ?? true,decoration: InputDecoration(
        hintText: fieldText,prefixIcon: fieldIcon,hintStyle: TextStyle(color: Colors.grey),filled: true,fillColor: Colors.white70,enabledBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Color.fromRGBO(198,57,93,1)),),focusedBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: Color.fromRGBO(198,cursorColor: Color.fromRGBO(198,1),);
  }
}

我喜欢用它

import 'package:flutter/material.dart';
import 'package:neighbourhood_watch/ui/widgets/button_global.dart';
import 'package:neighbourhood_watch/ui/widgets/textfield_global.dart';
import 'package:neighbourhood_watch/widgets/user_image_picker.dart';
import 'dart:io';

class SignUpPerson extends StatefulWidget {
  @override
  _SignUpPersonState createState() => _SignUpPersonState();
}

class _SignUpPersonState extends State<SignUpPerson> {
  TextEditingController username = new TextEditingController();
  TextEditingController description = new TextEditingController();
  TextEditingController contact = new TextEditingController();
  TextEditingController password = new TextEditingController();
  TextEditingController area = new TextEditingController();
  TextEditingController email = new TextEditingController();
  final _formKey = GlobalKey<FormState>();
  File _userImageFile;
  void _pickedImage(File image) {
    _userImageFile = image;
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return Scaffold(
      backgroundColor: Colors.white,body: SafeArea(
        child: Container(
          height: height,width: width,child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,children: [
                Container(
                  height: height * 0.05,Container(
                  height: height * 0.25,child: Image.asset(
                    'assets/images/nhwlogo_global.png',fit: BoxFit.contain,Text(
                  'Create an Account',style: TextStyle(
                    fontSize: 22,fontWeight: FontWeight.bold,SizedBox(
                  height: 30.0,Container(
                  height: height * 0.12,width: width * 0.5,child: UserImagePicker(
                    imagePickFn: _pickedImage,SizedBox(
                  height: 10.0,Padding(
                  padding: const EdgeInsets.only(left: 25.0,right: 25.0),child: GlobalTextField(
                      Icon(
                        Icons.person,color: Color.fromRGBO(198,'Username',username),SizedBox(
                  height: 15.0,child: GlobalTextField(
                      Icon(
                        Icons.edit,'Description',description),child: GlobalTextField(
                      Icon(
                        Icons.call,'Contact No.',contact),Form(
                  key: _formKey,child: Padding(
                    padding: const EdgeInsets.only(left: 25.0,child: GlobalTextField(
                        Icon(
                          Icons.cake,'Date of Birth',password),child: GlobalTextField(
                      Icon(
                        Icons.location_on,'Area',area),child: GlobalTextField(
                      Icon(
                        Icons.email,'Email',email),child: GlobalTextField(
                      Icon(
                        Icons.lock,'Password','Confirm Password',SizedBox(
                  height: 70.0,GlobalButton('CONTINUE',() {
                  print('userName: ${username}');
                },width * 0.7),SizedBox(
                  height: 50.0,Text(
                  'By creating an account you agree to our',style: TextStyle(
                    fontSize: 18,Text(
                  'Terms of Service and Privacy Policy',],);
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)