Skip to content

Commit

Permalink
API improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
leoafarias committed Dec 24, 2023
1 parent 0766fee commit a7533b6
Show file tree
Hide file tree
Showing 35 changed files with 1,901 additions and 1,622 deletions.
1,765 changes: 874 additions & 891 deletions coverage/lcov.info

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions demo/lib/views/button_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ abstract class Button extends StatelessWidget {

@override
Widget build(BuildContext context) {
final mergedStyle = _baseStyle
.selectVariant(size ?? ButtonSizeVariants.medium)
.merge(style);
final mergedStyle =
_baseStyle.variant(size ?? ButtonSizeVariants.medium).merge(style);

return Pressable(
onPressed: onPressed,
Expand Down
2 changes: 0 additions & 2 deletions lib/exports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ export 'src/utils/context_variant_util/on_directionality_util.dart';
export 'src/utils/context_variant_util/on_helper_util.dart';
export 'src/utils/context_variant_util/on_orientation_util.dart';
export 'src/utils/helper_util.dart';
export 'src/variants/context_variant.dart';
export 'src/variants/multi_variant.dart';
export 'src/variants/variant.dart';
export 'src/widgets/gap_widget.dart';
export 'src/widgets/pressable/gesture_state.dart';
Expand Down
7 changes: 0 additions & 7 deletions lib/src/attributes/border/border_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class BoxBorderUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final border = BorderUtility<StyleAttribute>(builder);
/// final attribute = border.all(
/// color: Colors.red,
/// width: 2.0,
Expand All @@ -86,7 +85,6 @@ class BoxBorderUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final border = BorderUtility<StyleAttribute>(builder);
/// final attribute = border.bottom(
/// color: Colors.blue,
/// width: 3.0,
Expand All @@ -112,7 +110,6 @@ class BoxBorderUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final border = BorderUtility<StyleAttribute>(builder);
/// final attribute = border.top(
/// color: Colors.blue,
/// width: 3.0,
Expand All @@ -138,7 +135,6 @@ class BoxBorderUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final border = BorderUtility<StyleAttribute>(builder);
/// final attribute = border.left(
/// color: Colors.blue,
/// width: 3.0,
Expand All @@ -164,7 +160,6 @@ class BoxBorderUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final border = BorderUtility<StyleAttribute>(builder);
/// final attribute = border.right(
/// color: Colors.blue,
/// width: 3.0,
Expand Down Expand Up @@ -234,7 +229,6 @@ class BoxBorderUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final border = BorderUtility<StyleAttribute>(builder);
/// final attribute = border.vertical(
/// color: Colors.indigo,
/// width: 1.5,
Expand All @@ -259,7 +253,6 @@ class BoxBorderUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final border = BorderUtility<StyleAttribute>(builder);
/// final attribute = border.horizontal(
/// color: Colors.teal,
/// width: 1.0,
Expand Down
7 changes: 4 additions & 3 deletions lib/src/attributes/color/color_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import 'color_dto.dart';

@immutable
class ColorUtility<T extends StyleAttribute>
extends DtoUtility<T, ColorDto, Color>
with CallableDtoUtilityMixin<T, ColorDto, Color> {
extends DtoUtility<T, ColorDto, Color> {
const ColorUtility(super.builder) : super(valueToDto: ColorDto.new);

T _buildColor(Color color) => builder(valueToDto(color));
Expand Down Expand Up @@ -87,7 +86,7 @@ class ColorUtility<T extends StyleAttribute>

T of(ColorToken ref) => _buildColor(ref());

T transparent() => _buildColor(const Color(0x00000000));
T transparent() => _buildColor(Colors.transparent);
T black() => _buildColor(const Color(0xFF000000));
T black87() => _buildColor(const Color(0xDD000000));
T black54() => _buildColor(const Color(0x8A000000));
Expand All @@ -104,6 +103,8 @@ class ColorUtility<T extends StyleAttribute>
T white24() => _buildColor(const Color(0x3DFFFFFF));
T white12() => _buildColor(const Color(0x1FFFFFFF));
T white10() => _buildColor(const Color(0x1AFFFFFF));

T call(Color color) => _buildColor(color);
}

abstract class ColorSwatchUtility<T extends StyleAttribute>
Expand Down
12 changes: 6 additions & 6 deletions lib/src/attributes/constraints/constraints_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'constraints_dto.dart';

/// Utility class for building box constraints-related style attributes.
///
/// Accepts a builder function that returns [T] and takes a [BoxConstraintsAttribute] as a parameter.
/// Accepts a builder function that returns [T] and takes a [BoxConstraintsDto] as a parameter.
///
/// This utility allows for detailed configuration of box constraints,
/// supporting attributes like `minWidth`, `maxWidth`, `minHeight`, and `maxHeight`.
Expand All @@ -19,7 +19,7 @@ class BoxConstraintsUtility<T extends StyleAttribute>
extends DtoUtility<T, BoxConstraintsDto, BoxConstraints> {
/// Creates a [BoxConstraintsUtility] with a builder function.
///
/// The builder function takes a [BoxConstraintsAttribute] as a parameter and returns [T].
/// The builder function takes a [BoxConstraintsDto] as a parameter and returns [T].
///
/// Example:
/// ```dart
Expand All @@ -39,7 +39,7 @@ class BoxConstraintsUtility<T extends StyleAttribute>
/// final attribute = boxConstraints.maxWidth(100);
/// ```
///
/// Attribute now holds a [T] with a [BoxConstraintsAttribute] that has a maxWidth value of `100`.
/// Attribute now holds a [T] with a [BoxConstraintsDto] that has a maxWidth value of `100`.
DoubleUtility<T> get maxWidth {
return DoubleUtility<T>((value) => call(maxWidth: value));
}
Expand All @@ -54,7 +54,7 @@ class BoxConstraintsUtility<T extends StyleAttribute>
/// final attribute = boxConstraints.minWidth(100);
/// ```
///
/// Attribute now holds a [T] with a [BoxConstraintsAttribute] that has a minWidth value of `100`.
/// Attribute now holds a [T] with a [BoxConstraintsDto] that has a minWidth value of `100`.
DoubleUtility<T> get minWidth {
return DoubleUtility<T>((value) => call(minWidth: value));
}
Expand All @@ -70,7 +70,7 @@ class BoxConstraintsUtility<T extends StyleAttribute>
/// final attribute = boxConstraints.minHeight(100);
/// ```
///
/// Attribute now holds a [T] with a [BoxConstraintsAttribute] that has a minHeight value of `100`.
/// Attribute now holds a [T] with a [BoxConstraintsDto] that has a minHeight value of `100`.
DoubleUtility<T> get minHeight {
return DoubleUtility<T>((value) => call(minHeight: value));
}
Expand All @@ -86,7 +86,7 @@ class BoxConstraintsUtility<T extends StyleAttribute>
/// final attribute = boxConstraints.maxHeight(100);
/// ```
///
/// Attribute now holds a [T] with a [BoxConstraintsAttribute] that has a maxHeight value of `100`.
/// Attribute now holds a [T] with a [BoxConstraintsDto] that has a maxHeight value of `100`.
DoubleUtility<T> get maxHeight {
return DoubleUtility<T>((value) => call(maxHeight: value));
}
Expand Down
63 changes: 45 additions & 18 deletions lib/src/attributes/scalars/scalar_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ abstract class DtoUtility<Attr extends StyleAttribute, D extends Dto<Value>,
Attr as(Value value) => _builder(valueToDto(value));
}

mixin CallableUtilityMixin<Attr extends StyleAttribute, Value>
on MixUtility<Attr, Value> {
Attr call(Value value) => _builder(value);
}

mixin CallableDtoUtilityMixin<Attr extends StyleAttribute, D extends Dto<Value>,
Value> on DtoUtility<Attr, D, Value> {
Attr call(Value value) => _builder(valueToDto(value));
}

abstract class ScalarUtility<Return extends StyleAttribute, Param>
extends MixUtility<Return, Param> {
const ScalarUtility(super.builder);
Expand All @@ -48,23 +38,56 @@ abstract class ScalarUtility<Return extends StyleAttribute, Param>
Return call(Param value) => _builder(value);
}

/// AlignmentUtility - A utility class for defining alignment attributes for widgets.
///
/// This class extends `ScalarUtility<T, AlignmentGeometry>`, allowing it to handle alignment attributes
/// using generic types. It provides methods to set various predefined alignments as well as custom alignments.
class AlignmentUtility<T extends StyleAttribute>
extends ScalarUtility<T, AlignmentGeometry> {
// Constructor accepting a builder function to create instances of T.
const AlignmentUtility(super.builder);

/// Sets the alignment to top left.
T topLeft() => builder(Alignment.topLeft);

/// Sets the alignment to top center.
T topCenter() => builder(Alignment.topCenter);

/// Sets the alignment to top right.
T topRight() => builder(Alignment.topRight);

/// Sets the alignment to center left.
T centerLeft() => builder(Alignment.centerLeft);

/// Sets the alignment to center.
T center() => builder(Alignment.center);

/// Sets the alignment to center right.
T centerRight() => builder(Alignment.centerRight);

/// Sets the alignment to bottom left.
T bottomLeft() => builder(Alignment.bottomLeft);

/// Sets the alignment to bottom center.
T bottomCenter() => builder(Alignment.bottomCenter);

/// Sets the alignment to bottom right.
T bottomRight() => builder(Alignment.bottomRight);

/// Sets a custom alignment based on the provided x, y, or start values.
///
/// The `x` and `start` parameters are mutually exclusive to avoid conflicts.
/// The `x` parameter sets a specific horizontal alignment, while `start` aligns based on text direction (LTR/RTL).
/// The `y` parameter sets the vertical alignment.
///
/// - `x`: Horizontal alignment value, ignored if `start` is provided.
/// - `y`: Vertical alignment value.
/// - `start`: Horizontal alignment based on text direction, overrides `x` if provided.
T only({double? x, double? y, double? start}) {
assert(x == null || start == null,
'Cannot provide both an x and a start parameter.');

// If `start` is provided, it creates an AlignmentDirectional, otherwise a regular Alignment.
return start == null
? builder(Alignment(x ?? 0, y ?? 0))
: builder(AlignmentDirectional(start, y ?? 0));
Expand All @@ -80,9 +103,11 @@ class AlignmentUtility<T extends StyleAttribute>
/// final utility = DoubleUtility(builder);
/// final tenValue = utility(10);
/// ```
class DoubleUtility<T extends StyleAttribute> extends ScalarUtility<T, double>
with CallableUtilityMixin<T, double> {
class DoubleUtility<T extends StyleAttribute> extends ScalarUtility<T, double> {
const DoubleUtility(super.builder);

@override
T call(double value) => builder(value);
}

/// Utility for Size values. Includes predefined values such as zero and infinity.
Expand Down Expand Up @@ -110,11 +135,13 @@ abstract class SizingUtility<T extends StyleAttribute>
/// final tenValue = utility(10);
/// // zeroValue is 0
/// ```
class IntUtility<T extends StyleAttribute> extends ScalarUtility<T, int>
with CallableUtilityMixin<T, int> {
class IntUtility<T extends StyleAttribute> extends ScalarUtility<T, int> {
const IntUtility(super.builder);

T zero() => builder(0);

@override
T call(int value) => builder(value);
}

/// Utility for creating `bool` values.
Expand All @@ -128,12 +155,14 @@ class IntUtility<T extends StyleAttribute> extends ScalarUtility<T, int>
/// final disabled = boolUtility.off();
/// final boolValue = boolUtility(true);
/// ```
class BoolUtility<T extends StyleAttribute> extends ScalarUtility<T, bool>
with CallableUtilityMixin<T, bool> {
class BoolUtility<T extends StyleAttribute> extends ScalarUtility<T, bool> {
const BoolUtility(super.builder);

T on() => builder(true);
T off() => builder(false);

@override
T call(bool value) => builder(value);
}

/// Utility for setting `VerticalDirection` values.
Expand Down Expand Up @@ -318,7 +347,6 @@ class TileModeUtility<T extends StyleAttribute>
/// final rotate90 = gradientTransform.rotate(90);
/// ```
/// See [GradientTransform] for more information.
class GradientTransformUtility<T extends StyleAttribute>
extends ScalarUtility<T, GradientTransform> {
const GradientTransformUtility(super.builder);
Expand Down Expand Up @@ -456,7 +484,6 @@ class ImageRepeatUtility<T extends StyleAttribute>
/// final offset = OffsetUtility(builder);
/// final offsetValue = offset(10, 10);
/// ```
class OffsetUtility<T extends StyleAttribute> extends MixUtility<T, Offset> {
const OffsetUtility(super.builder);

Expand Down
3 changes: 1 addition & 2 deletions lib/src/attributes/shadow/shadow_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,10 @@ class ElevationUtility<T extends StyleAttribute>
/// Example usage:
///
/// ```dart
/// final elevation = ElevationUtility<StyleAttribute>(builder);
/// final attribute = elevation.two();
/// ```
///
/// Attribute now holds a list of [BoxShadowAttribute] corresponding to a material elevation of `2`.
/// Attribute now holds a list of [BoxShadowDto] corresponding to a material elevation of `2`.
T call(int value) {
assert(kElevationToShadow.containsKey(value), 'Invalid elevation value');

Expand Down
14 changes: 0 additions & 14 deletions lib/src/attributes/variant_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:flutter/material.dart';

import '../core/attribute.dart';
import '../factory/style_mix.dart';
import '../variants/context_variant.dart';
import '../variants/multi_variant.dart';
import '../variants/variant.dart';

@immutable
Expand Down Expand Up @@ -88,15 +86,3 @@ class MultiVariantAttribute extends VariantAttribute<MultiVariant>
return MultiVariantAttribute(variant, _style.merge(other._style));
}
}

@immutable
class GestureContextVariantAttribute extends ContextVariantAttribute {
const GestureContextVariantAttribute(super.variant, super.style);

@override
GestureContextVariantAttribute merge(GestureContextVariantAttribute other) {
if (other.variant != variant) throw throwArgumentError(other);

return GestureContextVariantAttribute(variant, _style.merge(other._style));
}
}
29 changes: 8 additions & 21 deletions lib/src/deprecations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,29 @@ const kShortAliasDeprecation =
extension DeprecatedMixExtension<T extends Attribute> on Style {
/// Adds an Attribute to a Mix.
@Deprecated('Simplifying the mix API to avoid confusion. Use apply instead')
SpreadFunctionParams<T, Style> get mix {
return SpreadFunctionParams(addAttributes);
}
SpreadFunctionParams<T, Style> get mix => SpreadFunctionParams(addAttributes);

@Deprecated('Use selectVariants now')
Style withVariants(List<Variant> variants) {
return withManyVariants(variants);
}
Style withVariants(List<Variant> variants) => withManyVariants(variants);

@Deprecated(
'Use merge() or mergeMany() now. You might have to turn into a Mix first. firstMixFactory.merge(secondMix)',
)
Style addAttributes(Iterable<Attribute> attributes) {
return merge(Style.create(attributes));
}
Style addAttributes(Iterable<Attribute> attributes) =>
merge(Style.create(attributes));

@Deprecated('Use selectVariants now')
Style withManyVariants(Iterable<Variant> variants) {
return selectVariantList(variants);
}
Style withManyVariants(Iterable<Variant> variants) => variantList(variants);

@Deprecated('Use merge() or mergeMany() instead')
SpreadFunctionParams<Style, Style> get apply =>
const SpreadFunctionParams(Style.combine);

@Deprecated('Use selectVariant now')
Style withVariant(Variant variant) {
return selectVariant(variant);
}
Style withVariant(Variant value) => variant(value);

@Deprecated('Use combine now')
Style combineAll(List<Style> mixes) {
return Style.combine(mixes);
}
Style combineAll(List<Style> mixes) => Style.combine(mixes);

@Deprecated('Use selectVariant now')
Style withMaybeVariant(Variant? variant) {
Expand All @@ -58,9 +47,7 @@ extension DeprecatedMixExtension<T extends Attribute> on Style {
}

@Deprecated('Use applyNullable instead')
Style applyMaybe(Style? mix) {
return maybeApply(mix);
}
Style applyMaybe(Style? mix) => maybeApply(mix);
}

/// This refers to the deprecated class MixData and it's here for the purpose of maintaining compatibility.
Expand Down
Loading

0 comments on commit a7533b6

Please sign in to comment.