flutter_ui_guard 0.0.3
flutter_ui_guard: ^0.0.3 copied to clipboard
A Flutter static analysis tool that detects UI performance issues, deep widget nesting, and missing best practices in widget trees.
Flutter UI Guard đĄī¸ #
Flutter UI Guard is a powerful static analysis tool for Flutter developers that helps identify performance issues, best practice violations, and optimization opportunities in your widget tree before they impact your app's performance.
⨠Features #
đ¯ Core Analysis #
- Const Detection - Identifies widgets that should be marked as
constfor better performance - Deep Nesting Analysis - Warns about excessive widget tree depth that can hurt readability and performance
- Heavy Build Detection - Finds widgets with too many children that should use builders
- Container Optimization - Suggests using
SizedBoxinstead of emptyContainerwidgets - Opacity Performance - Warns about expensive
Opacitywidgets and suggests alternatives
đ Performance Checks #
- ListView Optimization - Detects missing
itemExtentorprototypeItemfor better scrolling - GestureDetector vs InkWell - Suggests Material Design alternatives for better UX
- MediaQuery Usage - Helps identify potential rebuild issues with MediaQuery
- Column to ListView - Suggests ListView.builder for long scrollable lists
đ Detailed Reporting #
- Severity Levels - Info, Warning, Error, and Critical classifications
- Actionable Suggestions - Every issue comes with a concrete fix suggestion
- Performance Metrics - Analysis time, widget count, and max depth tracking
- Beautiful Console Output - Color-coded, emoji-rich reports
đĻ Installation #
Add this to your pubspec.yaml:
dev_dependencies:
flutter_ui_guard: ^0.0.3
Then run:
flutter pub get
đ Quick Start #
Basic Usage #
import 'package:flutter/material.dart';
import 'package:flutter_guard/flutter_ui_guard.dart';
void main() {
final widget = Scaffold(
appBar: AppBar(
title: Text('My App'), // â ī¸ Should be const
),
body: Column(
children: [
Text('Hello World'), // â ī¸ Should be const
const Text('This is optimized'), // â
Good!
],
),
);
// Analyze the widget tree
final report = FlutterGuard.analyze(rootWidget: widget);
// Print detailed report
report.printReport();
}
Custom Configuration #
final report = FlutterGuard.analyze(
rootWidget: myWidget,
config: GuardConfig(
maxNestingLevel: 5, // Stricter nesting limit
maxChildrenWarning: 3, // Warn with fewer children
checkConst: true, // Enable const checking
checkNesting: true, // Enable nesting checks
checkHeavyBuilds: true, // Enable heavy build detection
checkListView: true, // Enable ListView optimization
checkOpacity: true, // Enable Opacity warnings
checkContainer: true, // Enable Container optimization
verbose: false, // Disable verbose logging
),
);
report.printReport();
đ Examples #
Example 1: Performance Issues #
Before:
Column(
children: List.generate(
100,
(i) => Text('Item $i'), // â Heavy build!
),
)
After (Following Flutter Guard Suggestion):
ListView.builder(
itemCount: 100,
itemBuilder: (context, i) => Text('Item $i'), // â
Optimized!
)
Example 2: Const Optimization #
Before:
Text('Hello World') // â ī¸ Not const
After:
const Text('Hello World') // â
Const widget - better performance!
Example 3: Container vs SizedBox #
Before:
Container(
width: 16,
height: 16,
) // âšī¸ Empty container
After:
const SizedBox(
width: 16,
height: 16,
) // â
More efficient spacing
đ Report Output #
============================================================
đĄī¸ FLUTTER GUARD ANALYSIS REPORT
============================================================
đ Total Widgets Analyzed: 45
đ Max Nesting Depth: 8
âąī¸ Analysis Time: 12ms
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
đ Issues Found: 7
âšī¸ Info: 3
â ī¸ Warnings: 3
â Errors: 1
đ´ Critical: 0
============================================================
đ Detailed Issues:
â ī¸ [Text] Text widget should be const when data is static
đĄ Add const: const Text("your text")
đ´ [Container] Container has both color and decoration
đĄ Use decoration: BoxDecoration(color: yourColor) instead
âšī¸ [GestureDetector] GestureDetector detected
đĄ Consider using InkWell for Material Design ripple effects
============================================================
đī¸ Configuration Options #
| Option | Type | Default | Description |
|---|---|---|---|
maxNestingLevel |
int |
6 |
Maximum widget nesting depth before warning |
maxChildrenWarning |
int |
5 |
Max children before suggesting builder pattern |
checkConst |
bool |
true |
Enable const usage detection |
checkNesting |
bool |
true |
Enable deep nesting warnings |
checkHeavyBuilds |
bool |
true |
Enable heavy build detection |
checkMediaQuery |
bool |
true |
Enable MediaQuery usage checks |
checkListView |
bool |
true |
Enable ListView optimization checks |
checkGestureDetector |
bool |
true |
Enable GestureDetector vs InkWell suggestions |
checkOpacity |
bool |
true |
Enable Opacity performance warnings |
checkContainer |
bool |
true |
Enable Container optimization suggestions |
verbose |
bool |
false |
Enable verbose widget tree logging |
đ¯ Best Practices Checked #
â DO: #
- Use
constconstructors whenever possible - Use
ListView.builder()for long lists - Use
SizedBoxfor spacing instead of emptyContainer - Use
InkWellfor Material Design tap effects - Add
itemExtentorprototypeItemtoListViewwhen possible - Keep widget tree nesting under 6-7 levels
â DON'T: #
- Forget
conston static widgets - Use
Columnwith 50+ children - Use empty
Containerfor spacing - Nest widgets more than 7-8 levels deep
- Set both
coloranddecorationonContainer - Use
OpacitywhenAnimatedOpacitywould work
đ¤ Contributing #
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Development Setup #
git clone https://github.com/smtechviral/flutter_ui_guard.git
cd flutter_ui_guard
flutter pub get
flutter test
đ Changelog #
See CHANGELOG.md for a detailed list of changes.
đ License #
This project is licensed under the MIT License - see the LICENSE file for details.
đ Acknowledgments #
- Inspired by Flutter's performance best practices
- Built with â¤ī¸ for the Flutter community
đ Support #
- đ Report Bug
- đĄ Request Feature
- đ§ Contact: [email protected]
â Show Your Support #
If you found this package helpful, please give it a â on GitHub!
Made with â¤ī¸ by the Flutter community