Unverified Commit 7ba5078f authored by Hans Muller's avatar Hans Muller Committed by GitHub

Added MaterialStateProperty.all() convenience method (#56589)

parent 3cb79079
......@@ -209,14 +209,27 @@ abstract class MaterialStateProperty<T> {
/// Convenience method for creating a [MaterialStateProperty] from a
/// [MaterialPropertyResolver] function alone.
static MaterialStateProperty<T> resolveWith<T>(MaterialPropertyResolver<T> callback) => _MaterialStateProperty<T>(callback);
static MaterialStateProperty<T> resolveWith<T>(MaterialPropertyResolver<T> callback) => _MaterialStatePropertyWith<T>(callback);
/// Convenience method for creating a [MaterialStateProperty] that resolves
/// to a single value for all states.
static MaterialStateProperty<T> all<T>(T value) => _MaterialStatePropertyAll<T>(value);
}
class _MaterialStateProperty<T> implements MaterialStateProperty<T> {
_MaterialStateProperty(this._resolve);
class _MaterialStatePropertyWith<T> implements MaterialStateProperty<T> {
_MaterialStatePropertyWith(this._resolve);
final MaterialPropertyResolver<T> _resolve;
@override
T resolve(Set<MaterialState> states) => _resolve(states);
}
class _MaterialStatePropertyAll<T> implements MaterialStateProperty<T> {
_MaterialStatePropertyAll(this.value);
final T value;
@override
T resolve(Set<MaterialState> states) => value;
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
void main() {
test('MaterialStateProperty.resolveWith()', () {
final MaterialStateProperty<MaterialState> value = MaterialStateProperty.resolveWith<MaterialState>(
(Set<MaterialState> states) => states.first,
);
expect(value.resolve(<MaterialState>{MaterialState.hovered}), MaterialState.hovered);
expect(value.resolve(<MaterialState>{MaterialState.focused}), MaterialState.focused);
expect(value.resolve(<MaterialState>{MaterialState.pressed}), MaterialState.pressed);
expect(value.resolve(<MaterialState>{MaterialState.dragged}), MaterialState.dragged);
expect(value.resolve(<MaterialState>{MaterialState.selected}), MaterialState.selected);
expect(value.resolve(<MaterialState>{MaterialState.disabled}), MaterialState.disabled);
expect(value.resolve(<MaterialState>{MaterialState.error}), MaterialState.error);
});
test('MaterialStateProperty.all()', () {
final MaterialStateProperty<int> value = MaterialStateProperty.all<int>(123);
expect(value.resolve(<MaterialState>{MaterialState.hovered}), 123);
expect(value.resolve(<MaterialState>{MaterialState.focused}), 123);
expect(value.resolve(<MaterialState>{MaterialState.pressed}), 123);
expect(value.resolve(<MaterialState>{MaterialState.dragged}), 123);
expect(value.resolve(<MaterialState>{MaterialState.selected}), 123);
expect(value.resolve(<MaterialState>{MaterialState.disabled}), 123);
expect(value.resolve(<MaterialState>{MaterialState.error}), 123);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment