Home > Software engineering >  spreadRadius value as parameter in flutter
spreadRadius value as parameter in flutter

Time:09-08

I am making a global boxShadow list that I want to use in the whole app. I am trying to pass spreadRadius value(i.e. 5 in the code below) as a parameter to make it dynamic in my custom list of BoxShadow.

Here is the code:

List<BoxShadow> outerShadow = [
  BoxShadow(
    color: Colors.grey.withOpacity(0.4),
    spreadRadius: 5,
    blurRadius: 7,
    offset: const Offset(0, 3),
  )
];

CodePudding user response:

You need to create a method if you wish to pass some data as a parameter.

List<BoxShadow> outerShadow(double spreadRadiusVal){
return [
  BoxShadow(
    color: Colors.grey.withOpacity(0.4),
    spreadRadius: spreadRadiusVal,
    blurRadius: 7,
    offset: const Offset(0, 3),
  )
];
}

And use it like

boxShadow : outerShadow(5.0),

CodePudding user response:

create a new dart file e.g. global_box_shadow.dart

in there write a method which has your parameters as input and returns BoxShadow

BoxShadow globalBoxShadow(double spreadRadius){
  return BoxShadow(
    color: Colors.grey.withOpacity(0.4),
    spreadRadius: spreadRadius,
    blurRadius: 7,
    offset: const Offset(0, 3),
  );
}

you can know just import this in whatever widget you want and pass this method in as e.g. boxShadow: globalBoxShadow(12.0)

  • Related