-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
avoid_sliver_to_box_adapter
rule
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/altive_lints/lib/src/lints/avoid_sliver_to_box_adapter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/altive_lints/lint_test/lints/avoid_sliver_to_box_adapter.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
], | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} |