Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
refactor: use dedicated MediaQuery methods instead of depending on al…
Browse files Browse the repository at this point in the history
…l MediaQuery data
  • Loading branch information
Craftplacer committed Dec 31, 2023
1 parent 13b44c6 commit d8928b9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DynamicDialogContainer extends StatelessWidget {
curve: Curves.easeOutQuad,
child: Padding(
padding: fullscreen
? MediaQuery.of(context).viewInsets
? MediaQuery.viewInsetsOf(context)
: EdgeInsets.zero,
child: Material(
borderRadius: borderRadius,
Expand Down
8 changes: 6 additions & 2 deletions src/kaiteki/lib/ui/user/user_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ class _UserScreenState extends ConsumerState<UserScreen> {
const a = avatarSize * 0.5;
final primaryButton = buildPrimaryButton(
user,
MediaQuery.of(context).size.width < 840,
MediaQuery
.sizeOf(context)
.width < 840,
);
return Scaffold(
backgroundColor: _getBackgroundColor(context),
Expand All @@ -146,7 +148,9 @@ class _UserScreenState extends ConsumerState<UserScreen> {
// recommended side pane width is 360dp, we use what is lower of
// width and a third of the screen, so the content pane doesn't get
// too squished.
width: min(MediaQuery.of(context).size.width / 3, 360),
width: min(MediaQuery
.sizeOf(context)
.width / 3, 360),
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Column(
Expand Down
3 changes: 3 additions & 0 deletions src/kaiteki_lints/lib/kaiteki_lints.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:custom_lint_builder/custom_lint_builder.dart';

import 'rules/l10n.dart';
import 'rules/prefer_media_query_aspects.dart';
import 'rules/prefer_widget_classes.dart';

PluginBase createPlugin() => _KaitekiLinter();

Expand All @@ -9,6 +11,7 @@ class _KaitekiLinter extends PluginBase {
List<LintRule> getLintRules(CustomLintConfigs configs) {
return [
const LocalizationRule(),
const PreferMediaQueryAspectsRule(),
];
}
}
66 changes: 66 additions & 0 deletions src/kaiteki_lints/lib/rules/prefer_media_query_aspects.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

const _typeChecker =
TypeChecker.fromName('MediaQueryData', packageName: 'flutter');

class PreferMediaQueryAspectsRule extends DartLintRule {
const PreferMediaQueryAspectsRule() : super(code: _code);

static const _code = LintCode(
name: 'prefer_media_query_aspects',
problemMessage:
'Prefer calling MediaQuery method that depend on only one aspect',
);

static const mediaQueryAspects = {
"size": "sizeOf",
"orientation": "orientationOf",
"devicePixelRatio": "devicePixelRatioOf",
"textScaler": "textScalerOf",
"textScaleFactor": "textScaleFactorOf",
"platformBrightness": "platformBrightnessOf",
"boldText": "boldTextOf",
"highContrast": "highContrastOf",
"disableAnimations": "disableAnimationsOf",
"invertColors": "invertColorsOf",
"alwaysUse24HourFormat": "alwaysUse24HourFormatOf",
"accessibleNavigation": "accessibleNavigationOf",
"navigationMode": "navigationModeOf",
"displayFeatures": "displayFeaturesOf",
"gestureSettings": "gestureSettingsOf",
"viewPadding": "viewPaddingOf",
"systemGestureInsets": "systemGestureInsetsOf",
"viewInsets": "viewInsetsOf",
"padding": "paddingOf",
};

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
// Detect access to e.g. MediaQuery.of(context).size and prompt to use MediaQuery.sizeOf(context) instead.
context.registry.addPropertyAccess((node) {
var propertyName = node.propertyName.name;
final methodName = mediaQueryAspects[propertyName];
if (methodName == null) return;

final targetType = node.target?.staticType;
if (targetType == null) return;
if (!_typeChecker.isAssignableFromType(targetType)) return;

reporter.reportErrorForNode(
LintCode(
name: 'prefer_media_query_aspects',
problemMessage:
'Prefer calling MediaQuery.$methodName(context) instead of MediaQuery.of(context).$propertyName',
errorSeverity: ErrorSeverity.WARNING,
),
node,
);
});
}
}

0 comments on commit d8928b9

Please sign in to comment.