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

Possibility to create a base style as to not repeat #24

Closed
linxydmhg opened this issue Jan 5, 2020 · 4 comments
Closed

Possibility to create a base style as to not repeat #24

linxydmhg opened this issue Jan 5, 2020 · 4 comments

Comments

@linxydmhg
Copy link

linxydmhg commented Jan 5, 2020

I saw its possible to use the Styled.widget(child) in the example, but this does not seem have the methods that are available for Text e.g. textColor().

            Text('NAME')
                .fontSize(16)
                .fontWeight(FontWeight.w600)
                .textColor(Colors.black),

            Text('AGE')
                .fontSize(16)
                .fontWeight(FontWeight.w600)
                .textColor(Colors.black),

Imagine we had something like above, it seems redundant to repeat ourselves. Is there a possibility to create a base style that can be applied to all Text widgets

e.g.

     final styledText= (Widget child) => StyledText.widget(child)
        .textColor(Colors.black)
        .fontSize(16)

     styledText(Text('NAME')),
     styledText(Text('AGE')),

or

Text('NAME')
  .applyStyles(?)

Text('AGE')
  .applyStyles(?)
@moraispgsi
Copy link
Contributor

Hi @linxydmhg the second example is similar to a previous pull-request that I have made that was rejected by @ReinBentdal #17. All of this can be accomplished with your own code by adding an extension method like I did in the pull request, without the need to change the library.

@moraispgsi
Copy link
Contributor

moraispgsi commented Jan 5, 2020

@linxydmhg Here is a snippet to get you started

typedef Text TextWrapper(Text text);
typedef Text TextWrapperWithContext(Text text, BuildContext context);

extension StyledText on Text {
  wrap(TextWrapper textWrapper) {
    if(textWrapper == null) {
      return this;
    }
    return textWrapper(this);
  }

  wrapWithContext(TextWrapperWithContext textWrapperWithContext, BuildContext context) {
    if(textWrapperWithContext == null) {
      return this;
    }
    return textWrapperWithContext(this, context);
  }
}

@ReinBentdal
Copy link
Owner

A way to achieve what you want is by doing this:

final styledText = (String data) => Text(data)
  .fontSize(16)
  .fontWeight(FontWeight.w600)
  .textColor(Colors.black);

styledText('NAME')
styledText('AGE')

@linxydmhg
Copy link
Author

@ReinBentdal That works out perfectly.

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

No branches or pull requests

3 participants