Search code examples
flutterdartrefactoringflutter-testdart-null-safety

How to Refactor a Function in Flutter


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)),
      );
    }