Navigating to a new page with button class

Issue

This is my buttons class. I want to use its on pressed function to navigate to another page.

class DigerButtonu extends StatelessWidget {
  final String butonyazisi;
  final IconData butoniconu;
  final Function butonfonksiyonu;
  const DigerButtonu(this.butonyazisi,this.butonfonksiyonu,this.butoniconu);
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(left: 20,right: 20),
      width: 50,
      height: 550,
      decoration: BoxDecoration(
          boxShadow: [
            BoxShadow(
              color: Color(0xFF15182D).withOpacity(0.4),
              blurRadius: 8,
              spreadRadius: 2,
              offset: Offset(4,4),
            ),
          ],
          borderRadius: BorderRadius.all(Radius.circular(10)),
          border: Border.all(
            color: Colors.black,
            width: 0.2,
          )
      ),
      child: ElevatedButton(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            Icon(butoniconu, color: Colors.white,size: 22,),
            MyVerticalText(butonyazisi),
            Icon(butoniconu, color: Colors.white,size: 22,),
          ],
        ),
        style: ElevatedButton.styleFrom(
          primary: Color(0xFF15182D),
          onPrimary: Colors.white,
          onSurface: Colors.grey,
          //elevation: 0,
        ),
        onPressed: (){
          butonfonksiyonu;
        },
      ),
    );
  }
}

But when i try to use the code below, it says ‘The argument type ‘Future’can’t be assigned to the parameter type ‘Function’ ‘. How can i use it?

DigerButtonu('AYARLAR',Navigator.push(context,MaterialPageRoute(builder: (context) => clicker()), route),Icons.settings_rounded)

Solution

You can simply use lambdas for passing functions. Currently you pass the result of your method and not the method itself.
Here is your code with the corresponding lambda expression:

DigerButtonu('AYARLAR', () => Navigator.push(context,MaterialPageRoute(builder: (context) => clicker()), route),Icons.settings_rounded)

Answered By – Mäddin

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *