π₯ Automatically detect nested Flutter widgets and convert them to styled_widget syntax with one click!
A custom_lint plugin for Flutter that:
- β Automatically detects widget nesting that can be simplified
- β
Provides Quick Fix for one-click conversion to
styled_widgetmethod chains - β Reduces boilerplate code by 30-40%
- β Improves code readability
Based on styled_widget official wiki
| Category | Methods | Coverage |
|---|---|---|
| Basic Styles | 6 | β 100% |
| Background Styles | 8 | β 100% |
| Borders & Clipping | 5 | β 100% |
| Shadows & Elevation | 2 | β 100% |
| Transforms | 4 | β 100% |
| Gestures | 2 | β 100% |
| Scrolling & Overflow | 2 | β 100% |
| Others | 3 | β 100% |
| Total | 32 | β 96.9% |
π View Complete List
Basic Styles: .padding(), .opacity(), .alignment(), .width(), .height(), .constrained()
Background Styles: .backgroundColor(), .backgroundImage(), .backgroundGradient(), .backgroundLinearGradient(), .backgroundSweepGradient(), .backgroundRadialGradient(), .backgroundBlendMode(), .backgroundBlur()
Borders & Clipping: .borderRadius(), .clipRRect(), .clipOval(), .border(), .decorated()
Shadows & Elevation: .elevation(), .boxShadow()
Transforms: .rotate(), .scale(), .translate(), .transform()
Gestures: .ripple(), .gestures()
Scrolling & Overflow: .scrollable(), .overflow()
Others: .semanticsLabel(), .textColor(), .parent()
# pubspec.yaml
dependencies:
flutter:
sdk: flutter
styled_widget: ^0.4.1# pubspec.yaml
dev_dependencies:
custom_lint: ^0.8.0
styled_widget_lint: ^1.0.0# analysis_options.yaml
analyzer:
plugins:
- custom_lint- Open a file with nested widgets
- See yellow underline hints
- Hover and click "Convert to styled_widget"
- β¨ Conversion complete!
Advantages: Safe, previewable, controllable
# Detect all issues
dart run custom_lint
# For Flutter projects
flutter pub run custom_lintBefore:
Padding(
padding: EdgeInsets.all(16),
child: Text('Hello'),
)After:
Text('Hello').padding(all: 16)Before:
Container(
color: Colors.blue,
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
alignment: Alignment.center,
child: Text('Styled'),
)After:
Text('Styled')
.padding(horizontal: 20, vertical: 10)
.backgroundColor(Colors.blue)
.alignment(Alignment.center)Before:
Padding(
padding: EdgeInsets.all(16),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
),
child: Align(
alignment: Alignment.center,
child: Opacity(
opacity: 0.8,
child: Text('Complex'),
),
),
),
)After (step-by-step conversion):
Text('Complex')
.opacity(0.8)
.alignment(Alignment.center)
.decorated(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
)
.padding(all: 16)π‘ Tip: For multi-layer nesting, apply Quick Fix from innermost to outermost.
Currently supported lint rule:
prefer_styled_widget: Detects all convertible widgets
styled_widget_lint/
βββ lib/
β βββ styled_widget_lint.dart # Plugin entry point
β βββ src/
β βββ rules/
β β βββ prefer_styled_widget.dart # Lint rule
β βββ fixes/
β β βββ convert_to_styled_widget_fix.dart # Quick Fix
β βββ utils/
β βββ widget_detector.dart # Widget detector
β βββ code_generator.dart # Code generator
βββ example/ # Example project
βββ README.md
# 1. Enter example project
cd example
# 2. Install dependencies
flutter pub get
# 3. Run lint
dart run custom_lint
# 4. Open example/lib/all_widgets_test.dart in IDE
# to see lint hints and Quick Fix- Complex Expressions: Dynamically generated widgets may not convert correctly
- Formatting: Converted code may need manual formatting (run
dart format) - Multi-layer Nesting: Requires manual step-by-step Quick Fix application from inside out
- Custom Widgets: Only detects Flutter built-in widgets
PRs and Issues are welcome!
MIT License
- styled_widget - Provides elegant widget extensions
- custom_lint - Provides custom lint framework
Happy coding! π