dart format#50
Conversation
There was a problem hiding this comment.
Code Review
This pull request primarily focuses on code reformatting and indentation adjustments across the example application, core library, and test files. A review comment identifies a logic issue in the example app's UI where using indexOf to retrieve color labels can lead to incorrect results if multiple color shades have identical values. The reviewer also noted variable shadowing and provided a code suggestion to use asMap().entries to ensure reliable indexing and cleaner scope management.
| ...colors.map((color) { | ||
| return Expanded( | ||
| child: Container( | ||
| color: color, | ||
| alignment: Alignment.center, | ||
| child: Text( | ||
| [ | ||
| 'Lightest', | ||
| 'Lighter', | ||
| 'Light', | ||
| 'Default', | ||
| 'Dark', | ||
| 'Darker', | ||
| 'Darkest', | ||
| ][colors.indexOf(color)] + | ||
| '\n${color.toHex()}', | ||
| style: Theme.of(context).textTheme.titleLarge?.copyWith( | ||
| color: color.computeLuminance() >= 0.5 | ||
| ? Colors.black | ||
| : Colors.white, | ||
| ), | ||
| textAlign: TextAlign.center, | ||
| ), | ||
| ), | ||
| ); | ||
| }).toList(), | ||
| ]), | ||
| ), | ||
| ); | ||
| }); | ||
| textAlign: TextAlign.center, | ||
| ), | ||
| ), | ||
| ); | ||
| }).toList(), |
There was a problem hiding this comment.
The current implementation uses indexOf to determine the label for each color. This logic is flawed because if multiple shades have the same color value (which occurs when the platform doesn't provide specific variants and autoAdjustLightness is disabled or results in the same color), indexOf will always return the index of the first occurrence, leading to incorrect labels in the UI. Additionally, the variable name color in the map function shadows the color variable from the SystemThemeBuilder scope. Using asMap().entries allows for a reliable index and avoids shadowing.
...colors.asMap().entries.map((entry) {
final index = entry.key;
final itemColor = entry.value;
return Expanded(
child: Container(
color: itemColor,
alignment: Alignment.center,
child: Text(
[
'Lightest',
'Lighter',
'Light',
'Default',
'Dark',
'Darker',
'Darkest',
][index] +
'\n${itemColor.toHex()}',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
color: itemColor.computeLuminance() >= 0.5
? Colors.black
: Colors.white,
),
textAlign: TextAlign.center,
),
),
);
}).toList(),
No description provided.