diff --git a/packages/altive_lints/lib/altive_lints.dart b/packages/altive_lints/lib/altive_lints.dart index c52c538..8d5fc01 100644 --- a/packages/altive_lints/lib/altive_lints.dart +++ b/packages/altive_lints/lib/altive_lints.dart @@ -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(); @@ -10,5 +11,6 @@ class _AltivePlugin extends PluginBase { List getLintRules(CustomLintConfigs configs) => [ const AvoidHardcodedJapanese(), const AvoidShrinkWrapInListView(), + const AvoidSliverToBoxAdapter(), ]; } diff --git a/packages/altive_lints/lib/src/lints/avoid_sliver_to_box_adapter.dart b/packages/altive_lints/lib/src/lints/avoid_sliver_to_box_adapter.dart new file mode 100644 index 0000000..799930e --- /dev/null +++ b/packages/altive_lints/lib/src/lints/avoid_sliver_to_box_adapter.dart @@ -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); + } + }); + } +} diff --git a/packages/altive_lints/lint_test/lints/avoid_sliver_to_box_adapter.dart b/packages/altive_lints/lint_test/lints/avoid_sliver_to_box_adapter.dart new file mode 100644 index 0000000..f38568c --- /dev/null +++ b/packages/altive_lints/lint_test/lints/avoid_sliver_to_box_adapter.dart @@ -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'), + ], + ), + ), + ], + ), + ); + } +}