I want to pass a function in my constructor and want to use in next. I also used VoidCallback instead of Function but the same error showing.... "The argument type 'Function' can't be assigned to the parameter type 'void Function()'"
``` import 'package:flutter/material.dart';
class MyTextField extends StatelessWidget {
const MyTextField({Key? key, required this.label,required this.onChanged}) : super(key:
key);
final String label;
final VoidCallback onChanged;
@override
Widget build(BuildContext context) {
return TextField(
style: const TextStyle(color: Colors.black),
onChanged: onChanged,
decoration: InputDecoration(
hintText: label,
hintStyle: const TextStyle(color: Colors.grey),
contentPadding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
enabledBorder: const OutlineInputBorder(
borderSide:
BorderSide(color: Colors.lightBlueAccent, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
focusedBorder: const OutlineInputBorder(
borderSide:
BorderSide(color: Colors.lightBlueAccent, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
),
);
}
}
CodePudding user response:
instead
final VoidCallback onChanged;
use
ValueChanged<String> onChanged;
e.g
class MyTextField extends StatelessWidget {
const MyTextField({Key? key, required this.label,required this.onChanged}) : super(key:
key);
final String label;
final ValueChanged<String> onChanged;
@override
Widget build(BuildContext context) {
return TextField(
style: const TextStyle(color: Colors.black),
onChanged: onChanged,
decoration: InputDecoration(
hintText: label,
hintStyle: const TextStyle(color: Colors.grey),
contentPadding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
enabledBorder: const OutlineInputBorder(
borderSide:
BorderSide(color: Colors.lightBlueAccent, width: 1.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
focusedBorder: const OutlineInputBorder(
borderSide:
BorderSide(color: Colors.lightBlueAccent, width: 2.0),
borderRadius: BorderRadius.all(Radius.circular(32.0)),
),
),
);
}
}
CodePudding user response:
because VoidCallback not the same type as Type of onChanged in text filed so
change
final VoidCallback onChanged;
to
final void Function(String)? onChanged;