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<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)),
),
),
),
),
],
),
));
}
}
2
Answers
Remove const from the center widget. You should be left with something like this:
Remove the const keyword before the Center widgets for both the TextFields