I have a reusable button widget and when I pass in the function for the onPressed of the ElevatedButton this doesn’t get executed.
Note, the other places that I use this onPressed to navigate works
Places where I use the onPressed and works
BlackButton(
text: 'Create offer',
onPressed: () => Navigator.pushNamed(context, 'createOfferFighter'), //this works
),
Place where I execute a function – This widget is in a stateful parent widget
BlackRoundedButton(
isLoading: false
text: submitButton,
onPressed: () =>
createOffer() //doesn't print anything
),
The createOffer() function
void createOffer() {
print('casfas');
}
Reusable button widget:
class BlackButton extends StatelessWidget {
final Function onPressed;
final String text;
const BlackButton({Key? key, required this.onPressed, required this.text})
: super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: 230,
height: 60,
child: Container(
decoration: BoxDecoration(boxShadow: [containerShadowRed]),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: const MaterialStatePropertyAll(Colors.black),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
),
),
onPressed: () => onPressed(),
child: Text(
text,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
)),
),
);
}
}
2
Answers
Try this code
This should print the createOffer():