简体   繁体   中英

How to I change the selected color of an Flutter Button

example my code

 backgroundColor: MaterialStateProperty.resolveWith((states) {
            if (isDisabled) {
              return const FMThemeColor().strong_bt_04;
            } else {
              if (isSelected) {
                return const FMThemeColor().strong_bt_03;
              } else {
                if (states.contains(MaterialState.hovered)) {
                  return const FMThemeColor().strong_bt_02;
                } else {
                  return const FMThemeColor().strong_bt_01;
                }
              }
            }
          }),

I found out that there were more conditions.

how to use

states.contains(MaterialState.selected

This is an example provided by Darts.

DataTable(
  dataRowColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
    if (states.contains(MaterialState.selected))
      return Theme.of(context).colorScheme.primary.withOpacity(0.08);
    return null;  // Use the default value.
  }),
)

But I don't know from this alone.

how to use selected states?

only flutter web project...

Is this what you want?

DataTable(
  dataRowColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
    var defaultColor = Colors.red;
    if (states.contains(MaterialState.selected))
      defaultColor = Theme.of(context).colorScheme.primary.withOpacity(0.08);
    return defaultColor;  // Use the default value.
  }),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM