Skip to content

Commit

Permalink
feat: Add avoid_sliver_to_box_adapter rule
Browse files Browse the repository at this point in the history
  • Loading branch information
naipaka committed Jul 29, 2024
1 parent eb7444e commit f82f3da
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/altive_lints/lib/altive_lints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:custom_lint_builder/custom_lint_builder.dart';

import 'src/lints/avoid_hardcoded_japanese.dart';
import 'src/lints/avoid_shrink_wrap_in_list_view.dart';
import 'src/lints/avoid_sliver_to_box_adapter.dart';

PluginBase createPlugin() => _AltivePlugin();

Expand All @@ -10,5 +11,6 @@ class _AltivePlugin extends PluginBase {
List<LintRule> getLintRules(CustomLintConfigs configs) => [
const AvoidHardcodedJapanese(),
const AvoidShrinkWrapInListView(),
const AvoidSliverToBoxAdapter(),
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

/// An `avoid_sliver_to_box_adapter` rule that discourages using
/// `SliverToBoxAdapter` due to performance inefficiencies.
///
/// This widget can lead to layout issues and increased
/// re-rendering in scrollable environments.
/// It's recommended to use`CustomScrollView` with `SliverList` or
/// other sliver widgets to optimize performance and ensure smoother scrolling.
///
/// See more here: https://api.flutter.dev/flutter/widgets/SliverToBoxAdapter-class.html
///
/// ### Example
///
/// #### BAD:
///
/// ```dart
/// CustomScrollView(
/// slivers: [
/// SliverToBoxAdapter( // LINT
/// child: YourWidget()
/// ),
/// ],
/// );
/// ```
///
/// #### GOOD:
///
/// ```dart
/// CustomScrollView(
/// slivers: [
/// SliverList.list(
/// children: [
/// YourWidget(),
/// ],
/// ),
/// ],
/// );
/// ```
class AvoidSliverToBoxAdapter extends DartLintRule {
const AvoidSliverToBoxAdapter() : super(code: _code);

static const _code = LintCode(
name: 'avoid_sliver_to_box_adapter',
problemMessage:
'Avoid using `SliverToBoxAdapter` due to performance issues. '
'Consider alternatives like `CustomScrollView` with `SliverList`.',
);

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addInstanceCreationExpression((node) {
final targetType =
node.staticType?.getDisplayString(withNullability: false);
if (targetType == 'SliverToBoxAdapter') {
reporter.reportErrorForNode(_code, node);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';

class MyWidget extends StatelessWidget {
const MyWidget({super.key});

@override
Widget build(BuildContext context) {
return const Scaffold(
body: CustomScrollView(
slivers: [
// expect_lint: avoid_sliver_to_box_adapter
SliverToBoxAdapter(
child: Column(
children: [
Text('Hello'),
Text('World'),
],
),
),
],
),
);
}
}

0 comments on commit f82f3da

Please sign in to comment.