A value of type 'Null' can't be assigned to a parameter of type 'TextEditingController' in a const constructor

huangapple go评论57阅读模式
英文:

A value of type 'Null' can't be assigned to a parameter of type 'TextEditingController' in a const constructor

问题

新手学习Flutter开发,我创建了一个自定义TextField小部件,该部件接受样式参数和TextEditingController。但是,在传递控制器的行上我遇到了以下错误:

无法将类型为'Null'的值分配给const构造函数中类型为'TextEditingController'的参数。请尝试使用子类型,或删除'const'关键字。

任何帮助将不胜感激。

这是代码:

// 自定义小部件
class TestEditText extends StatelessWidget {
  final Color fontColor;
  final String hintText;
  final Color hintColor;
  final bool isBorder;
  final double borderWidth;
  final Color borderColor;
  final bool isIcon;
  final Icon prefixIcon;
  final TextEditingController textController;

  const TestEditText({
    Key? key,
    required this.textController,
    required this.fontColor,
    required this.hintText,
    required this.hintColor,
    this.isBorder = true,
    this.borderWidth = 0.5,
    this.borderColor = const Color.fromRGBO(204, 204, 204, 1),
    this.isIcon = true,
    this.prefixIcon = const Icon(Icons.abc),
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return PhysicalModel(
        color: Colors.white,
        elevation: 5.0,
        shadowColor: Colors.black54,
        child: TextField(
          controller: textController,
          style: TextStyle(color: fontColor),
          decoration: InputDecoration(
              hintText: hintText,
              hintStyle: TextStyle(color: hintColor),
              focusedBorder: OutlineInputBorder(
                  borderSide:
                      BorderSide(width: borderWidth, color: borderColor),
                  borderRadius: const BorderRadius.all(Radius.circular(0))),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(width: borderWidth, color: borderColor),
              ),
              prefixIcon: prefixIcon),
        ));
  }
}
// 使用自定义小部件
class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController txtEmailController = TextEditingController();
  TextEditingController txtPasswordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Colors.white,
        body: Container(
          alignment: Alignment.topLeft,
          margin: const EdgeInsets.fromLTRB(10, 50, 0, 10),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [              
              Container(
                height: 50,
                margin: const EdgeInsets.only(left: 5, right: 15),
                child: const Center(
                    child: TestEditText(
                  textController: txtEmailController,
                  hintText: 'Enter Email',
                  hintColor: Color.fromRGBO(119, 119, 119, 1),
                  fontColor: Color.fromRGBO(119, 119, 119, 1),
                  isBorder: true,
                  borderWidth: 0.5,
                  borderColor: Color.fromRGBO(204, 204, 204, 1),
                  isIcon: true,
                  prefixIcon: Icon(
                    Icons.email_outlined,
                  ),
                )),
              ),
              const SizedBox(
                height: 20,
              ),
              Container(
                height: 50,
                margin: const EdgeInsets.only(left: 5, right: 15),
                child: const Center(
                    child: TestEditText(
                  textController: txtPasswordController,
                  hintText: 'Enter Password',
                  hintColor: Color.fromRGBO(119, 119, 119, 1),
                  fontColor: Color.fromRGBO(119, 119, 119, 1),
                  isBorder: true,
                  borderWidth: 0.5,
                  borderColor: Color.fromRGBO(204, 204, 204, 1),
                  isIcon: true,
                  prefixIcon: Icon(
                    Icons.lock_outlined,
                  ),
                )),
              ),
              const SizedBox(
                height: 25,
              ),
              GestureDetector(
                onTap: () async {},
                child: Container(
                  margin: const EdgeInsets.only(right: 10),
                  child: ElevatedButton(
                    style: const ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(
                          Color.fromRGBO(2, 118, 236, 1)),
                      foregroundColor: MaterialStatePropertyAll(Colors.white),
                    ),
                    onPressed: null,
                    child: Container(
                      height: 50,
                      width: MediaQuery.of(context).size.width * 0.7,
                      alignment: Alignment.center,
                      child:
                          const Text("Login", style: TextStyle(fontSize: 18)),
                    ),
                  ),
                ),
              ),              
            ],
          ),
        ));
  }
}
英文:

New to flutter development and I have created custom TextField widget which accepts style arguments and TextEditingController. But Im getting following error on line where passing controller :

> A value of type 'Null' can't be assigned to a parameter of type 'TextEditingController' in a const constructor.
Try using a subtype, or removing the keyword 'const'.

Any help would be appreciated.

Here is the code:

// Custom Widget
class TestEditText extends StatelessWidget {
final Color fontColor;
final String hintText;
final Color hintColor;
final bool isBorder;
final double borderWidth;
final Color borderColor;
final bool isIcon;
final Icon prefixIcon;
final TextEditingController textController;
const TestEditText({
Key? key,
required this.textController,
required this.fontColor,
required this.hintText,
required this.hintColor,
this.isBorder = true,
this.borderWidth = 0.5,
this.borderColor = const Color.fromRGBO(204, 204, 204, 1),
this.isIcon = true,
this.prefixIcon = const Icon(Icons.abc),
}) : super(key: key);
@override
Widget build(BuildContext context) {
return PhysicalModel(
color: Colors.white,
elevation: 5.0,
shadowColor: Colors.black54,
child: TextField(
controller: textController,
style: TextStyle(color: fontColor),
decoration: InputDecoration(
hintText: hintText,
hintStyle: TextStyle(color: hintColor),
focusedBorder: OutlineInputBorder(
borderSide:
BorderSide(width: borderWidth, color: borderColor),
borderRadius: const BorderRadius.all(Radius.circular(0))),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(width: borderWidth, color: borderColor),
),
prefixIcon: prefixIcon),
));
}
}
// Use of Custom Widget
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State&lt;LoginPage&gt; createState() =&gt; _LoginPageState();
}
class _LoginPageState extends State&lt;LoginPage&gt; {
TextEditingController txtEmailController = TextEditingController();
TextEditingController txtPasswordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: Container(
alignment: Alignment.topLeft,
margin: const EdgeInsets.fromLTRB(10, 50, 0, 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [              
Container(
height: 50,
margin: const EdgeInsets.only(left: 5, right: 15),
child: const Center(
child: TestEditText(
textController: txtEmailController,
hintText: &#39;Enter Email&#39;,
hintColor: Color.fromRGBO(119, 119, 119, 1),
fontColor: Color.fromRGBO(119, 119, 119, 1),
isBorder: true,
borderWidth: 0.5,
borderColor: Color.fromRGBO(204, 204, 204, 1),
isIcon: true,
prefixIcon: Icon(
Icons.email_outlined,
),
)),
),
const SizedBox(
height: 20,
),
Container(
height: 50,
margin: const EdgeInsets.only(left: 5, right: 15),
child: const Center(
child: TestEditText(
textController: txtPasswordController,
hintText: &#39;Enter Password&#39;,
hintColor: Color.fromRGBO(119, 119, 119, 1),
fontColor: Color.fromRGBO(119, 119, 119, 1),
isBorder: true,
borderWidth: 0.5,
borderColor: Color.fromRGBO(204, 204, 204, 1),
isIcon: true,
prefixIcon: Icon(
Icons.lock_outlined,
),
)),
),
const SizedBox(
height: 25,
),
GestureDetector(
onTap: () async {},
child: Container(
margin: const EdgeInsets.only(right: 10),
child: ElevatedButton(
style: const ButtonStyle(
backgroundColor: MaterialStatePropertyAll(
Color.fromRGBO(2, 118, 236, 1)),
foregroundColor: MaterialStatePropertyAll(Colors.white),
),
onPressed: null,
child: Container(
height: 50,
width: MediaQuery.of(context).size.width * 0.7,
alignment: Alignment.center,
child:
const Text(&quot;Login&quot;, style: TextStyle(fontSize: 18)),
),
),
),
),              
],
),
));
}
}

答案1

得分: 1

将中心小部件中的const移除。应该剩下类似这样的内容:

Container(
    height: 50,
    margin: EdgeInsets.only(left: 5, right: 15),
    child: Center(
        child: TestEditText(
            textController: txtEmailController,
            hintText: '输入电子邮件',
            hintColor: Color.fromRGBO(119, 119, 119, 1),
            fontColor: Color.fromRGBO(119, 119, 119, 1),
            isBorder: true,
            borderWidth: 0.5,
            borderColor: Color.fromRGBO(204, 204, 204, 1),
            isIcon: true,
            prefixIcon: Icon(
                Icons.email_outlined,
            ),
        ),
    ),
)
英文:

Remove const from the center widget. You should be left with something like this:

Container(
height: 50,
margin: const EdgeInsets.only(left: 5, right: 15),
child: Center(
child: TestEditText(
textController: txtEmailController,
hintText: &#39;Enter Email&#39;,
hintColor: Color.fromRGBO(119, 119, 119, 1),
fontColor: Color.fromRGBO(119, 119, 119, 1),
isBorder: true,
borderWidth: 0.5,
borderColor: Color.fromRGBO(204, 204, 204, 1),
isIcon: true,
prefixIcon: Icon(
Icons.email_outlined,
),
)),
)

答案2

得分: 0

移除const关键字在Center小部件之前的TextFields。对于非常量值,请不要使用const关键字。

Container(
height: 50,
margin: EdgeInsets.only(left: 5, right: 15),
child: Center(  // 这里你之前使用了const关键字,导致错误
child: TestEditText(
textController: txtEmailController,
hintText: '输入电子邮件',
hintColor: Color.fromRGBO(119, 119, 119, 1),
fontColor: Color.fromRGBO(119, 119, 119, 1),
isBorder: true,
borderWidth: 0.5,
borderColor: Color.fromRGBO(204, 204, 204, 1),
isIcon: true,
prefixIcon: Icon(
Icons.email_outlined,
),
)),
),
SizedBox(
height: 20,
),
Container(
height: 50,
margin: EdgeInsets.only(left: 5, right: 15),
child: Center(  // 这里你之前使用了const关键字,导致错误
child: TestEditText(
textController: txtPasswordController,
hintText: '输入密码',
hintColor: Color.fromRGBO(119, 119, 119, 1),
fontColor: Color.fromRGBO(119, 119, 119, 1),
isBorder: true,
borderWidth: 0.5,
borderColor: Color.fromRGBO(204, 204, 204, 1),
isIcon: true,
prefixIcon: Icon(
Icons.lock_outlined,
),
)),
),
英文:

Remove the const keyword before the Center widgets for both the TextFields. Do not use const keyword for non-constant values.

Container(
height: 50,
margin: const EdgeInsets.only(left: 5, right: 15),
child: Center(  // here you were using const keyword which is causing error
child: TestEditText(
textController: txtEmailController,
hintText: &#39;Enter Email&#39;,
hintColor: Color.fromRGBO(119, 119, 119, 1),
fontColor: Color.fromRGBO(119, 119, 119, 1),
isBorder: true,
borderWidth: 0.5,
borderColor: Color.fromRGBO(204, 204, 204, 1),
isIcon: true,
prefixIcon: Icon(
Icons.email_outlined,
),
)),
),
const SizedBox(
height: 20,
),
Container(
height: 50,
margin: const EdgeInsets.only(left: 5, right: 15),
child: Center(  // here you were using const keyword which is causing error
child: TestEditText(
textController: txtPasswordController,
hintText: &#39;Enter Password&#39;,
hintColor: Color.fromRGBO(119, 119, 119, 1),
fontColor: Color.fromRGBO(119, 119, 119, 1),
isBorder: true,
borderWidth: 0.5,
borderColor: Color.fromRGBO(204, 204, 204, 1),
isIcon: true,
prefixIcon: Icon(
Icons.lock_outlined,
),
)),
),

huangapple
  • 本文由 发表于 2023年5月14日 22:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定