Skip to content

Commit

Permalink
Refactors FastMaterialStateProperty to FastWidgetStateProperty fo…
Browse files Browse the repository at this point in the history
…r Flutter 3.22.0
  • Loading branch information
Rexios80 committed May 16, 2024
1 parent e61f2c9 commit b5f9753
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 84 deletions.
4 changes: 4 additions & 0 deletions fast_extensions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.0
- Refactors `FastMaterialStateProperty` into `FastWidgetStateProperty` for Flutter 3.22.0
- Requires Flutter 3.22.0

## 0.1.5
- Updates for Flutter 3.16.0

Expand Down
27 changes: 15 additions & 12 deletions fast_extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ Convenience extensions to make common Flutter tasks less annoying
[![pub points](https://img.shields.io/pub/points/fast_extensions)](https://pub.dev/packages/fast_extensions/score)

## Features
| Extension | Use-case |
| ------------------------- | ---------------------------------------- |
| FastLocale | Localizations.localeOf(context) |
| FastMaterialColor | FastMaterialColor.fromColor(color) |
| FastMaterialLocalizations | MaterialLocalizations.of(context) |
| FastMaterialStateProperty | Create MaterialStateProperty using a map |
| FastMediaQuery | MediaQuery.of(context) |
| FastTheme | Theme.of(context) |

| Extension | Use-case |
| ------------------------- | -------------------------------------- |
| FastLocale | Localizations.localeOf(context) |
| FastMaterialColor | FastMaterialColor.fromColor(color) |
| FastMaterialLocalizations | MaterialLocalizations.of(context) |
| FastWidgetStateProperty | Create WidgetStateProperty using a map |
| FastMediaQuery | MediaQuery.of(context) |
| FastTheme | Theme.of(context) |

## Usage

<!-- embedme readme/usage.dart -->

```dart
import 'package:fast_extensions/fast_extensions.dart';
import 'package:flutter/material.dart';
Expand All @@ -36,14 +38,14 @@ void example(BuildContext context) {
// FastMaterialLocalizations
context.backButtonTooltip;
// FastMaterialStateProperty
final msp = FastMaterialStateProperty(
{MaterialState.selected: Colors.white},
// FastWidgetStateProperty
final wsp = FastWidgetStateProperty(
{WidgetState.selected: Colors.white},
defaultValue: Colors.black,
);
ThemeData(
segmentedButtonTheme:
SegmentedButtonThemeData(style: ButtonStyle(foregroundColor: msp)),
SegmentedButtonThemeData(style: ButtonStyle(foregroundColor: wsp)),
);
// FastMediaQuery
Expand All @@ -61,4 +63,5 @@ void example(BuildContext context) {
```

## Additional information

See [fast_ui](https://pub.dev/packages/fast_ui) for more information
10 changes: 5 additions & 5 deletions fast_extensions/lib/src/fast_material_state_property.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import 'package:flutter/material.dart';

/// Create a [MaterialStateProperty] using only a map of [MaterialState]s
class FastMaterialStateProperty<T> extends MaterialStateProperty<T?> {
/// Create a [WidgetStateProperty] using only a map of [WidgetState]s
class FastWidgetStateProperty<T> extends WidgetStateProperty<T?> {
/// The map used to resolve the property
final Map<MaterialState, T> resolution;
final Map<WidgetState, T> resolution;

/// The value to use if no match is found in [resolution]
final T? defaultValue;

/// Constructor
FastMaterialStateProperty(this.resolution, {this.defaultValue});
FastWidgetStateProperty(this.resolution, {this.defaultValue});

@override
T? resolve(Set<MaterialState> states) {
T? resolve(Set<WidgetState> states) {
for (final state in states) {
if (resolution.containsKey(state)) {
return resolution[state];
Expand Down
4 changes: 2 additions & 2 deletions fast_extensions/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: fast_extensions
description: Convenience extensions to make common Flutter tasks less annoying
version: 0.1.5
version: 0.2.0
homepage: https://github.com/Rexios80/fast_ui/tree/master/fast_extensions

environment:
sdk: ">=2.17.0 <4.0.0"
flutter: ">=1.17.0"
flutter: ">=3.22.0"

dependencies:
flutter:
Expand Down
8 changes: 4 additions & 4 deletions fast_extensions/readme/usage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ void example(BuildContext context) {
// FastMaterialLocalizations
context.backButtonTooltip;

// FastMaterialStateProperty
final msp = FastMaterialStateProperty(
{MaterialState.selected: Colors.white},
// FastWidgetStateProperty
final wsp = FastWidgetStateProperty(
{WidgetState.selected: Colors.white},
defaultValue: Colors.black,
);
ThemeData(
segmentedButtonTheme:
SegmentedButtonThemeData(style: ButtonStyle(foregroundColor: msp)),
SegmentedButtonThemeData(style: ButtonStyle(foregroundColor: wsp)),
);

// FastMediaQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('FastMaterialStateProperty', () {
final msp = FastMaterialStateProperty(
{MaterialState.selected: Colors.white},
test('FastWidgetStateProperty', () {
final wsp = FastWidgetStateProperty(
{WidgetState.selected: Colors.white},
defaultValue: Colors.black,
);
// Expect that the primary shade matches the color we passed in
expect(
msp.resolve({MaterialState.disabled, MaterialState.selected}),
wsp.resolve({WidgetState.disabled, WidgetState.selected}),
Colors.white,
);

expect(
msp.resolve({MaterialState.disabled}),
wsp.resolve({WidgetState.disabled}),
Colors.black,
);
});
Expand Down
Loading

0 comments on commit b5f9753

Please sign in to comment.