Home > other >  I want to use TextButton properties like color to change using dynamic function
I want to use TextButton properties like color to change using dynamic function

Time:01-02

This is the function I want to change in


This is the function

Expanded buildKey({Color bgColor, int n}) {

    return Expanded(
      child: TextButton(
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(bgColor),
        ),
        onPressed: () {
          playSound(n);
        },
        child: Text(''),
      ),
    );
  }

And here I'm calling the above function

buildKey(Color: Colors.red, n: 1);

These 2 errors I'm receiving.

error: The parameter 'n' can't have a value of 'null' because of its type, but the implicit default value is 'null'. (missing_default_value_for_parameter at [xylophone] lib\main.dart:12)

error: The parameter 'bgColor' can't have a value of 'null' because of its type, but the implicit default value is 'null'. (missing_default_value_for_parameter at [xylophone] lib\main.dart:12)

CodePudding user response:

this is happening because youur passing parameters can be null, so give them property required as below.

Expanded buildKey({required Color bgColor, required int n}) {
    return Expanded(
      child: TextButton(
        style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(bgColor),
        ),
        onPressed: () {
          playSound(n);
        },
        child: Text(''),
      ),
    );
  }

CodePudding user response:

Expanded buildKey({Color bgColor, int n}) {

    return Expanded(
      child: TextButton(
        style: ButtonStyle(
          backgroundColor: bgColor != null ?  MaterialStateProperty.all(bgColor) :  MaterialStateProperty.all(Colors.green),
        ),
        onPressed: () {
          playSound(n);
        },
        child: Text(''),
      ),
    );
  }
  • Related