How to Refactor a Function in Flutter

Issue

import 'package:flutter/material.dart';

Widget justButton({
  String btText = '',
  Color bgColor = Colors.blue,
  Color? txtColor = Colors.white,
  Color borderColor = Colors.black,
  void Function() onpressedAction,//here iam getting problem
}) {
  return OutlinedButton(
    //i wanted to refactor this onpressed 
    onPressed: () {
      print('Go to events Page');
    },
    child: Text(btText),
    style: OutlinedButton.styleFrom(
        backgroundColor: bgColor,
        primary: txtColor,
        side: BorderSide(color: borderColor)),
  );
}

This is my code here I have trying to refactor the onpressed outlinedButton ,
How can it possible to refactor a function

Solution

Did you want to fix error?
Here is my refactoring result.

import 'package:flutter/material.dart';

Widget justButton({
  String btText = '',
  Color bgColor = Colors.blue,
  Color? txtColor = Colors.white,
  Color borderColor = Colors.black,
  Function? onpressedAction,//here iam getting problem
}) {
  return OutlinedButton(
    //i wanted to refactor this onpressed 
    onPressed: () => onpressedAction!(),
    child: Text(btText),
    style: OutlinedButton.styleFrom(
        backgroundColor: bgColor,
        primary: txtColor,
        side: BorderSide(color: borderColor)),
  );
}

Answered By – KuKu

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

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