flutter 不能将类型为“Null”的值赋给常量构造函数中类型为“TextEditingController”的参数

5sxhfpxr  于 2023-05-19  发布在  Flutter
关注(0)|答案(2)|浏览(181)

Flutter开发新手,我创建了自定义TextField小部件,它接受样式参数和TextEditingController。但是我在通过控制器的线路上得到以下错误:

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

任何帮助将不胜感激。
代码如下:

// 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<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)),
                    ),
                  ),
                ),
              ),              
            ],
          ),
        ));
  }
}
wj8zmpe1

wj8zmpe11#

从中间小部件中删除const。你应该得到这样的东西:

Container(
                height: 50,
                margin: const EdgeInsets.only(left: 5, right: 15),
                child: 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,
                  ),
                )),
              )
sqougxex

sqougxex2#

删除两个TextField的Center小部件前的const关键字。不要对非常量值使用const关键字。

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: '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: Center(  // here you were using const keyword which is causing error
                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,
              ),
            )),
          ),

相关问题