Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] InheritedAttribute - Extend Mix functionality with custom attributes. #94

Merged
merged 9 commits into from
Sep 27, 2022

Conversation

pbissonho
Copy link
Contributor

@pbissonho pbissonho commented Sep 26, 2022

@leoafarias Hello, excellent work here! This lib is amazing!

Currently not is possible to use Mix in components not yet supported by the lib, such as TextField and DropdownButton.

I worked on an implementation that makes possible to create new attributes and consume them from the context so that it is possible to work with the Mix in any component.

Related issues:
#93, #91 and #46;

Design

Create the extended inherited attribute

class InputDecorationThemeAttribute extends InputDecorationTheme
    implements InheritedAttribute<InputDecorationThemeAttribute> {
  const InputDecorationThemeAttribute({
    Color? iconColor,
    Color? fillColor,
    InputBorder? border,
  }) : super(
          iconColor: iconColor,
          fillColor: fillColor,
          border: border,
        );

  @override
  InputDecorationThemeAttribute merge(InputDecorationThemeAttribute other) {
    return InputDecorationThemeAttribute(
      iconColor: other.iconColor ?? iconColor,
      fillColor: other.fillColor ?? fillColor,
      border: other.border ?? border,
    );
  }

  static InputDecorationThemeAttribute inputDecoration(
      {Color? iconColor,
      Color? fillColor,
      InputBorder? border,
      Color? hoverColor,
      InputBorder? enabledBorder}) {
    return InputDecorationThemeAttribute(
      iconColor: iconColor ?? iconColor,
      fillColor: fillColor ?? fillColor,
      border: border ?? border,
    );
  }

  @override
  Object get type => InputDecorationThemeAttribute;
}

Define a short utils

const inputDecoration = InputDecorationThemeAttribute.inputDecoration;

Create a Mix

final inputDecorationMix = Mix(
        inputDecoration(
          border: const UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.grey),
          ),
        ),
        myVariant(
          inputDecoration(
            enabledBorder: const UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.yellow),
            ),
          ),
        ),
      )

Consume attributes from mixContext

   ...

 @override
  Widget build(BuildContext context) {
    return MixContextBuilder(
      mix: inputDecorationMix,
      builder: (context, mixContext) {
        final inputDecorationTheme =
            mixContext.fromType<InputDecorationThemeAttribute>();

        return TextFormField(
          decoration: const InputDecoration(
            filled: true,
            labelText: 'Label Text',
            suffixIcon: Icon(
              Icons.check_circle,
            ),
          ).applyDefaults(inputDecorationTheme),
        );
      },
    );
  }

Reference

The implementation is based from: https://api.flutter.dev/flutter/material/ThemeData/extensions.html

@pbissonho pbissonho changed the title [Feature] InheritedAttribute - Extend Mix with custom attributes. [Feature] InheritedAttribute - Eextend mix functionality with custom attributes. Sep 27, 2022
@pbissonho pbissonho changed the title [Feature] InheritedAttribute - Eextend mix functionality with custom attributes. [Feature] InheritedAttribute - Extend Mix functionality with custom attributes. Sep 27, 2022
Copy link
Member

@leoafarias leoafarias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@leoafarias
Copy link
Member

@pbissonho looks good, that is a great contribution. Might make some small adjustments as this feature matures, but I think its good to be merged!

@leoafarias leoafarias merged commit 403902c into conceptadev:main Sep 27, 2022
@allcontributors
Copy link
Contributor

@leoafarias

@pbissonho already contributed before to code, ideas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants