$npx -y skills add softspark/ai-toolkit --skill flutter-patternsFlutter/Dart: widgets, state mgmt (Riverpod/Bloc), navigation, platform channels. Triggers: Flutter, Dart, widget, Riverpod, Bloc, pubspec, hot reload.
| 1 | # Flutter Patterns Skill |
| 2 | |
| 3 | ## Project Structure |
| 4 | |
| 5 | ``` |
| 6 | lib/ |
| 7 | ├── main.dart |
| 8 | ├── app.dart |
| 9 | ├── core/ |
| 10 | │ ├── constants/ |
| 11 | │ ├── errors/ |
| 12 | │ ├── network/ |
| 13 | │ └── utils/ |
| 14 | ├── features/ |
| 15 | │ └── feature_name/ |
| 16 | │ ├── data/ |
| 17 | │ │ ├── models/ |
| 18 | │ │ ├── repositories/ |
| 19 | │ │ └── sources/ |
| 20 | │ ├── domain/ |
| 21 | │ │ ├── entities/ |
| 22 | │ │ └── usecases/ |
| 23 | │ └── presentation/ |
| 24 | │ ├── bloc/ |
| 25 | │ ├── pages/ |
| 26 | │ └── widgets/ |
| 27 | └── shared/ |
| 28 | ├── widgets/ |
| 29 | └── theme/ |
| 30 | ``` |
| 31 | |
| 32 | --- |
| 33 | |
| 34 | ## State Management |
| 35 | |
| 36 | ### BLoC Pattern |
| 37 | ```dart |
| 38 | // Event |
| 39 | abstract class AuthEvent {} |
| 40 | class LoginRequested extends AuthEvent { |
| 41 | final String email; |
| 42 | final String password; |
| 43 | LoginRequested(this.email, this.password); |
| 44 | } |
| 45 | |
| 46 | // State |
| 47 | abstract class AuthState {} |
| 48 | class AuthInitial extends AuthState {} |
| 49 | class AuthLoading extends AuthState {} |
| 50 | class AuthSuccess extends AuthState { |
| 51 | final User user; |
| 52 | AuthSuccess(this.user); |
| 53 | } |
| 54 | class AuthFailure extends AuthState { |
| 55 | final String message; |
| 56 | AuthFailure(this.message); |
| 57 | } |
| 58 | |
| 59 | // BLoC |
| 60 | class AuthBloc extends Bloc<AuthEvent, AuthState> { |
| 61 | AuthBloc() : super(AuthInitial()) { |
| 62 | on<LoginRequested>(_onLoginRequested); |
| 63 | } |
| 64 | |
| 65 | Future<void> _onLoginRequested( |
| 66 | LoginRequested event, |
| 67 | Emitter<AuthState> emit, |
| 68 | ) async { |
| 69 | emit(AuthLoading()); |
| 70 | try { |
| 71 | final user = await authRepository.login(event.email, event.password); |
| 72 | emit(AuthSuccess(user)); |
| 73 | } catch (e) { |
| 74 | emit(AuthFailure(e.toString())); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | ``` |
| 79 | |
| 80 | ### Riverpod |
| 81 | ```dart |
| 82 | final userProvider = FutureProvider<User>((ref) async { |
| 83 | final repository = ref.watch(userRepositoryProvider); |
| 84 | return repository.getUser(); |
| 85 | }); |
| 86 | |
| 87 | // Usage |
| 88 | Consumer( |
| 89 | builder: (context, ref, child) { |
| 90 | final userAsync = ref.watch(userProvider); |
| 91 | return userAsync.when( |
| 92 | data: (user) => Text(user.name), |
| 93 | loading: () => CircularProgressIndicator(), |
| 94 | error: (e, s) => Text('Error: $e'), |
| 95 | ); |
| 96 | }, |
| 97 | ) |
| 98 | ``` |
| 99 | |
| 100 | --- |
| 101 | |
| 102 | ## Navigation (GoRouter) |
| 103 | |
| 104 | ```dart |
| 105 | final router = GoRouter( |
| 106 | routes: [ |
| 107 | GoRoute( |
| 108 | path: '/', |
| 109 | builder: (context, state) => HomeScreen(), |
| 110 | routes: [ |
| 111 | GoRoute( |
| 112 | path: 'details/:id', |
| 113 | builder: (context, state) => DetailsScreen( |
| 114 | id: state.pathParameters['id']!, |
| 115 | ), |
| 116 | ), |
| 117 | ], |
| 118 | ), |
| 119 | ], |
| 120 | ); |
| 121 | |
| 122 | // Navigation |
| 123 | context.go('/details/123'); |
| 124 | context.push('/details/123'); |
| 125 | context.pop(); |
| 126 | ``` |
| 127 | |
| 128 | --- |
| 129 | |
| 130 | ## Widget Patterns |
| 131 | |
| 132 | ### Responsive Layout |
| 133 | ```dart |
| 134 | class ResponsiveLayout extends StatelessWidget { |
| 135 | final Widget mobile; |
| 136 | final Widget? tablet; |
| 137 | final Widget desktop; |
| 138 | |
| 139 | @override |
| 140 | Widget build(BuildContext context) { |
| 141 | return LayoutBuilder( |
| 142 | builder: (context, constraints) { |
| 143 | if (constraints.maxWidth < 600) { |
| 144 | return mobile; |
| 145 | } else if (constraints.maxWidth < 1200) { |
| 146 | return tablet ?? desktop; |
| 147 | } |
| 148 | return desktop; |
| 149 | }, |
| 150 | ); |
| 151 | } |
| 152 | } |
| 153 | ``` |
| 154 | |
| 155 | ### Sliver Patterns |
| 156 | ```dart |
| 157 | CustomScrollView( |
| 158 | slivers: [ |
| 159 | SliverAppBar( |
| 160 | floating: true, |
| 161 | title: Text('Title'), |
| 162 | ), |
| 163 | SliverPadding( |
| 164 | padding: EdgeInsets.all(16), |
| 165 | sliver: SliverList( |
| 166 | delegate: SliverChildBuilderDelegate( |
| 167 | (context, index) => ListTile(title: Text('Item $index')), |
| 168 | childCount: 100, |
| 169 | ), |
| 170 | ), |
| 171 | ), |
| 172 | ], |
| 173 | ) |
| 174 | ``` |
| 175 | |
| 176 | --- |
| 177 | |
| 178 | ## Performance |
| 179 | |
| 180 | ### const Constructors |
| 181 | ```dart |
| 182 | // Good |
| 183 | const Text('Hello'); |
| 184 | const SizedBox(height: 8); |
| 185 | |
| 186 | // Widget with const constructor |
| 187 | class MyWidget extends StatelessWidget { |
| 188 | const MyWidget({super.key}); |
| 189 | } |
| 190 | ``` |
| 191 | |
| 192 | ### RepaintBoundary |
| 193 | ```dart |
| 194 | RepaintBoundary( |
| 195 | child: ComplexAnimatedWidget(), |
| 196 | ) |
| 197 | ``` |
| 198 | |
| 199 | ### Image Optimization |
| 200 | ```dart |
| 201 | Image.network( |
| 202 | url, |
| 203 | cacheWidth: 200, // Resize in memory |
| 204 | cacheHeight: 200, |
| 205 | ) |
| 206 | ``` |
| 207 | |
| 208 | --- |
| 209 | |
| 210 | ## Testing |
| 211 | |
| 212 | ### Widget Testing |
| 213 | ```dart |
| 214 | testWidgets('Counter increments', (tester) async { |
| 215 | await tester.pumpWidget(MyApp()); |
| 216 | |
| 217 | expect(find.text('0'), findsOneWidget); |
| 218 | |
| 219 | await tester.tap(find.byIcon(Icons.add)); |
| 220 | await tester.pump(); |
| 221 | |
| 222 | expect(find.text('1'), findsOneWidget); |
| 223 | }); |
| 224 | ``` |
| 225 | |
| 226 | ### BLoC Testing |
| 227 | ```dart |
| 228 | blocTest<AuthBloc, AuthState>( |
| 229 | 'emits [loading, success] when login succeeds', |
| 230 | build: () => AuthBloc(), |
| 231 | act: (bloc) => bloc.add(LoginRequested('email', 'pass')), |
| 232 | expect: () => [AuthLoading(), isA<AuthSuccess>()], |
| 233 | ); |
| 234 | ``` |
| 235 | |
| 236 | --- |
| 237 | |
| 238 | ## Best Practices |
| 239 | |
| 240 | - ✅ Use const constructors |
| 241 | - ✅ Extract widgets to separate files |
| 242 | - ✅ Use named routes |
| 243 | - ✅ Implement proper error handling |
| 244 | - ✅ Use proper state management |
| 245 | - ❌ Avoid setState for complex state |
| 246 | - ❌ Don't nest too many widgets |
| 247 | - ❌ Avoid magic numbers |