I am building an flutter app and I need to pass function to other widget.

I have these 8 TextFormFields with values in it. When I press the grey button "Smazat hodnoty pasažérů" it should clear all the text fields.
I have the textfields in one file pasazeri.dart and the button in another file deleteBtn.dart. I think I can just send the function to clear the boxes to my button widget but it doesn't work.
This is my full code of pasazeri.dart
import 'package:flutter/services.dart';
import 'deleteBtn.dart';
class Pasazeri extends StatefulWidget {
@override
_PasazeriState createState() => _PasazeriState();
}
class _PasazeriState extends State<Pasazeri> {
final FocusNode pasazer1 = FocusNode();
final FocusNode pasazer2 = FocusNode();
final FocusNode pasazer3 = FocusNode();
final FocusNode pasazer4 = FocusNode();
final FocusNode pasazer5 = FocusNode();
final FocusNode pasazer6 = FocusNode();
final FocusNode pasazer7 = FocusNode();
final FocusNode pasazer8 = FocusNode();
TextEditingController contPasazer1 = TextEditingController();
TextEditingController contPasazer2 = TextEditingController();
TextEditingController contPasazer3 = TextEditingController();
TextEditingController contPasazer4 = TextEditingController();
TextEditingController contPasazer5 = TextEditingController();
TextEditingController contPasazer6 = TextEditingController();
TextEditingController contPasazer7 = TextEditingController();
TextEditingController contPasazer8 = TextEditingController();
_fieldFocusChange(
BuildContext context, FocusNode currentFocus, FocusNode nextFocus) {
currentFocus.unfocus();
FocusScope.of(context).requestFocus(nextFocus);
}
void clearBoxes() {
setState(() {
contPasazer1.clear();
contPasazer2.clear();
contPasazer3.clear();
contPasazer4.clear();
contPasazer5.clear();
contPasazer6.clear();
contPasazer7.clear();
contPasazer8.clear();
});
}
Widget pasazer(cisloPasazer, controller, focusnode, next) {
return Container(
margin: EdgeInsets.only(left: 8),
child: Row(
children: <Widget>[
Text(
"$cisloPasazer. pasažér: ",
style: TextStyle(fontSize: 14.5, fontWeight: FontWeight.w500),
),
Container(
padding: EdgeInsets.only(left: 3),
width: 40,
height: 25,
decoration: BoxDecoration(
color: Color(0xFFFFDD80),
borderRadius: BorderRadius.all(Radius.circular(10))),
child: Center(
child: TextFormField(
controller: controller,
focusNode: focusnode,
textInputAction: TextInputAction.next,
onChanged: (String str) => DeleteBtn(delete: clearBoxes),
onFieldSubmitted: (term) {
_fieldFocusChange(context, focusnode, next);
},
style: TextStyle(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.w600),
cursorWidth: 1,
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
contentPadding: EdgeInsets.symmetric(vertical: 9),
border: InputBorder.none,
hintText: "",
hintStyle: TextStyle(
color: Color(0xFF9c9a98),
),
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(3),
],
),
),
),
Text(" kg",
style: TextStyle(fontSize: 14.5, fontWeight: FontWeight.w500))
],
),
);
}
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.015,
bottom: MediaQuery.of(context).size.height * 0.015,
left: 8,
right: 8),
padding: EdgeInsets.symmetric(
vertical: MediaQuery.of(context).size.height * 0.012),
decoration: BoxDecoration(
color: Color(0xFFa38b5f),
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.symmetric(vertical: 6, horizontal: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
pasazer(1, contPasazer1, pasazer1, pasazer2),
Container(
margin: EdgeInsets.only(right: 8),
child: Row(
children: <Widget>[
pasazer(5, contPasazer5, pasazer5, pasazer6),
],
),
)
],
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 6, horizontal: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
pasazer(2, contPasazer2, pasazer2, pasazer3),
Container(
margin: EdgeInsets.only(right: 8),
child: Row(
children: <Widget>[
pasazer(6, contPasazer6, pasazer6, pasazer7),
],
),
)
],
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 6, horizontal: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
pasazer(3, contPasazer3, pasazer3, pasazer4),
Container(
margin: EdgeInsets.only(right: 8),
child: Row(
children: <Widget>[
pasazer(7, contPasazer7, pasazer7, pasazer8),
],
),
)
],
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 6, horizontal: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
pasazer(4, contPasazer4, pasazer4, pasazer5),
Container(
margin: EdgeInsets.only(right: 8),
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 8),
child: Row(
children: <Widget>[
Text(
"8. pasažér: ",
style: TextStyle(
fontSize: 14.5, fontWeight: FontWeight.w500),
),
Container(
padding: EdgeInsets.only(left: 3),
width: 40,
height: 25,
decoration: BoxDecoration(
color: Color(0xFFFFDD80),
borderRadius:
BorderRadius.all(Radius.circular(10))),
child: Center(
child: TextFormField(
controller: contPasazer8,
focusNode: pasazer8,
textInputAction: TextInputAction.done,
onFieldSubmitted: (term) {
pasazer8.unfocus();
},
style: TextStyle(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.w600),
cursorWidth: 1,
textAlign: TextAlign.center,
textAlignVertical: TextAlignVertical.center,
decoration: InputDecoration(
contentPadding:
EdgeInsets.symmetric(vertical: 9),
border: InputBorder.none,
hintText: "",
hintStyle: TextStyle(
color: Color(0xFF9c9a98),
),
),
keyboardType: TextInputType.number,
inputFormatters: <TextInputFormatter>[
WhitelistingTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(3),
],
onChanged: (String str) {},
),
),
),
Text(" kg",
style: TextStyle(
fontSize: 14.5,
fontWeight: FontWeight.w500))
],
),
),
],
),
)
],
),
),
],
),
);
}
}
And here is my code of deleteBtn.dart
class DeleteBtn extends StatelessWidget {
final Function delete;
DeleteBtn({this.delete});
@override
Widget build(BuildContext context) {
return Expanded(
child: Container(
height: 40,
margin: EdgeInsets.only(right: 8),
decoration: BoxDecoration(
color: Color(0xFF616161),
borderRadius: BorderRadius.all(Radius.circular(20)),
),
child: FlatButton(
padding: EdgeInsets.symmetric(horizontal: 0),
child: Text(
"Smazat hodnoty pasažérů",
style: TextStyle(fontSize: 10),
),
onPressed: () {
print(delete);
delete();
},
),
),
);
}
}
When i press the button i need to call the function clearBoxes() which is in the pasazeri.dart
I added print(delete); to onPressed: and it gives me:
The following NoSuchMethodError was thrown while handling a gesture:
The method 'call' was called on null.
Receiver: null
Tried calling: call()
So i think it's problem with passing the function.
Does anyone know how can i fix it?
