Last updated:
0 purchases
mix lint
mix_lint #
Mix Lint is a powerful tool that helps you enforce coding standards and best practices in your Flutter apps using Mix.
Getting Started #
Run this command in the root of your Flutter project:
flutter pub add -d mix_lint custom_lint
copied to clipboard
Then edit your analysis_options.yaml file and add these lines of code:
analyzer:
plugins:
- custom_lint
copied to clipboard
Then run:
flutter clean
flutter pub get
dart run custom_lint
copied to clipboard
Customize rules #
Some rules have their own configuration. You can customize them in the analysis_options.yaml file. For example, you can customize the rules for the mix_max_number_of_attributes_per_style rule.
custom_lint:
rules:
- mix_max_number_of_attributes_per_style:
max_number: 11
copied to clipboard
Rules #
mix_attributes_ordering #
Ensure that the attributes are ordered in groups of the same category in the Style constructor. It makes the code easier to read and understand the Style.
Don't
Style (
$box.color.red(),
$text.color.blue(),
$box.height(200),
$text.style.fontSize(10),
)
copied to clipboard
Do
Style (
$box.color.red(),
$box.height(200),
$text.color.blue(),
$text.style.fontSize(10),
)
copied to clipboard
mix_avoid_defining_tokens_or_variants_within_style #
Ensure that Variant and MixToken instances are not created directly inside Style constructors. Instead, instantiate Variant and MixToken outside of Style constructors.
Variants and MixTokens should be shared across the application. If they are created inside a Style, it means that they are local to the Style, and it will be harder to reuse them.
Don't
Style(
const Variant('example')(
$text.textAlign.center(),
$box.height(200),
$box.width(200),
),
)
copied to clipboard
Style(
$box.color.ref(ColorToken('primary')),
)
copied to clipboard
Do
final example = Variant('example');
Style(
example(
$text.textAlign.center(),
$box.height(200),
$box.width(200),
),
)
copied to clipboard
final primary = ColorToken('primary');
Style(
$box.color.ref(primary),
)
copied to clipboard
mix_avoid_defining_tokens_within_theme_data #
Ensure that Tokens instances are not created directly inside MixThemeData constructors.
When you create tokens within a MixThemeData, you're essentially creating a localized scope that may not be easily accessible elsewhere in your application. This can lead to unnecessary token recreation or, worse, duplicated efforts to reuse them.
Don't
MixThemeData(
colors: {
const ColorToken('a'): Colors.black12,
}
)
copied to clipboard
Do
final primary = ColorToken('a');
MixThemeData(
colors: {
primary: Colors.black12,
}
)
copied to clipboard
mix_avoid_empty_variants #
Avoid creating empty variants directly inside Style. Empty variants are essentially useless and can make the code harder to read and understand.
Don't
final a = Variant('a');
final wrong_case = Style(
a(),
);
copied to clipboard
Do
final correct_case = Style(
a(
$box.color.amber(),
),
);
copied to clipboard
mix_avoid_variant_inside_context_variant #
ContextVariant and the standard Variant are applied at different moments during the Style lifecycle. Because of this, we strongly recommend that you don't create a Variant inside the ContextVariant's scope. Instead, you can combine the Variants using the & and | operators.
Don't
final variantTest = Variant('test');
Style (
$box.color.red(),
$on.hover(
$box.color.green(),
variantTest(
$box.color.blue(),
)
),
)
copied to clipboard
Do
final variantTest = Variant('test');
Style (
$box.color.red(),
$on.hover(
$box.color.green(),
),
($on.hover & variantTest)(
$box.color.blue(),
),
)
copied to clipboard
mix_max_number_of_attributes_per_style #
Limit the number of attributes per style. The default value is 10. This rule aims to encourage developers to keep their styles concise and focused on a few key aspects.
Don't
final style = Style (
$attribute1(),
$attribute2(),
$attribute3(),
$attribute4(),
$attribute5(),
$attribute6(),
$attribute7(),
$attribute8(),
$attribute9(),
$attribute10(),
$attribute11(),
);
copied to clipboard
Do
final auxStyle = Style(
$attribute1(),
$attribute2(),
$attribute3(),
$attribute4(),
$attribute5(),
);
final mainStyle = Style(
auxStyle(),
$attribute6(),
$attribute7(),
$attribute8(),
$attribute9(),
$attribute10(),
$attribute11(),
);
copied to clipboard
Parameters
max_number (int)
The maximum number of attributes allowed per style. The default value is 10.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.