|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +import '/df_router.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + runApp(const MyApp()); |
| 7 | +} |
| 8 | + |
| 9 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 10 | + |
| 11 | +final class HomeRouteState extends RouteState { |
| 12 | + HomeRouteState() : super.parse('/home'); |
| 13 | +} |
| 14 | + |
| 15 | +final class MessagesRouteState extends RouteState { |
| 16 | + MessagesRouteState() : super.parse('/messages'); |
| 17 | +} |
| 18 | + |
| 19 | +final class MessagesRouteState1 extends RouteState { |
| 20 | + MessagesRouteState1() : super.parse('/messages?key1=value1'); |
| 21 | +} |
| 22 | + |
| 23 | +final class MessagesRouteState2 extends RouteState { |
| 24 | + MessagesRouteState2() : super.parse('/messages?key1=value1', queryParameters: {'key2': 'value2'}); |
| 25 | +} |
| 26 | + |
| 27 | +class MyApp extends StatelessWidget { |
| 28 | + const MyApp({super.key}); |
| 29 | + |
| 30 | + @override |
| 31 | + Widget build(BuildContext context) { |
| 32 | + return MaterialApp( |
| 33 | + color: Colors.white, |
| 34 | + builder: (context, child) { |
| 35 | + return Material( |
| 36 | + child: RouteManager( |
| 37 | + fallbackRouteState: () => HomeRouteState(), |
| 38 | + // wrapper: (context, child) { |
| 39 | + // return Column( |
| 40 | + // crossAxisAlignment: CrossAxisAlignment.stretch, |
| 41 | + // children: [ |
| 42 | + // // Persistent app header. |
| 43 | + // Container( |
| 44 | + // color: Colors.blueGrey, |
| 45 | + // padding: const EdgeInsets.all(16.0), |
| 46 | + // child: const Text( |
| 47 | + // 'df_router Example', |
| 48 | + // style: TextStyle(color: Colors.white, fontSize: 24.0), |
| 49 | + // ), |
| 50 | + // ), |
| 51 | + // // Main content area. |
| 52 | + // Expanded(child: child), |
| 53 | + // // Persistent app footer with navigation buttons. |
| 54 | + // Container( |
| 55 | + // color: Colors.indigo, |
| 56 | + // padding: const EdgeInsets.all(16.0), |
| 57 | + // child: Row( |
| 58 | + // children: [ |
| 59 | + // IconButton( |
| 60 | + // onPressed: () { |
| 61 | + // final controller = RouteController.of(context); |
| 62 | + // controller.push(HomeRouteState()); |
| 63 | + // }, |
| 64 | + // icon: Text( |
| 65 | + // 'HOME', |
| 66 | + // style: TextStyle( |
| 67 | + // color: |
| 68 | + // RouteController.of(context).routeState.matchPath(HomeRouteState()) |
| 69 | + // ? Colors.grey |
| 70 | + // : Colors.white, |
| 71 | + // ), |
| 72 | + // ), |
| 73 | + // ), |
| 74 | + // IconButton( |
| 75 | + // onPressed: () { |
| 76 | + // final controller = RouteController.of(context); |
| 77 | + // controller.push(RouteState.parse('/chat')); |
| 78 | + // }, |
| 79 | + // icon: Text( |
| 80 | + // 'CHAT', |
| 81 | + // style: TextStyle( |
| 82 | + // color: |
| 83 | + // RouteController.of(context).routeState.path == '/chat' |
| 84 | + // ? Colors.grey |
| 85 | + // : Colors.white, |
| 86 | + // ), |
| 87 | + // ), |
| 88 | + // ), |
| 89 | + // ], |
| 90 | + // ), |
| 91 | + // ), |
| 92 | + // ], |
| 93 | + // ); |
| 94 | + // }, |
| 95 | + // transitionBuilder: (context, params) { |
| 96 | + // // For iOS. |
| 97 | + // // return Expanded( |
| 98 | + // // child: HorizontalSlideFadeTransition( |
| 99 | + // // // Prev is a capture of the previous page. |
| 100 | + // // prev: params.prev, |
| 101 | + // // controller: params.controller, |
| 102 | + // // duration: const Duration(milliseconds: 300), |
| 103 | + // // child: params.child, |
| 104 | + // // ), |
| 105 | + // // ); |
| 106 | + // // For Android. |
| 107 | + // return VerticalSlideFadeTransition( |
| 108 | + // prev: params.prevSnapshot, |
| 109 | + // controller: params.controller, |
| 110 | + // duration: const Duration(milliseconds: 300), |
| 111 | + // child: params.child, |
| 112 | + // ); |
| 113 | + // }, |
| 114 | + builders: [ |
| 115 | + RouteBuilder( |
| 116 | + routeState: HomeRouteState(), |
| 117 | + builder: (context, state) { |
| 118 | + return HomeScreen(routeState: state); |
| 119 | + }, |
| 120 | + ), |
| 121 | + RouteBuilder( |
| 122 | + routeState: MessagesRouteState(), |
| 123 | + // Preserves the RouteState when navigating away. This means it will |
| 124 | + // be kept in memory and not disposed until manually disposed. |
| 125 | + //shouldPreserve: true, |
| 126 | + builder: (context, state) { |
| 127 | + return MessagesScreen(routeState: state); |
| 128 | + }, |
| 129 | + ), |
| 130 | + RouteBuilder<String>( |
| 131 | + routeState: RouteState<String>.parse('/chat'), |
| 132 | + // Pre-builds the widget even if the RouteState is not at the top of |
| 133 | + // the stack. This is useful for RouteStates that are frequently |
| 134 | + // navigated to or that takes some time to build. |
| 135 | + //shouldPrebuild: true, |
| 136 | + builder: (context, state) { |
| 137 | + return ChatScreen(routeState: state); |
| 138 | + }, |
| 139 | + ), |
| 140 | + RouteBuilder( |
| 141 | + routeState: RouteState.parse('/detail'), |
| 142 | + builder: (context, state) { |
| 143 | + return HomeDetailScreen(routeState: state); |
| 144 | + }, |
| 145 | + ), |
| 146 | + ], |
| 147 | + ), |
| 148 | + ); |
| 149 | + }, |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 155 | + |
| 156 | +class MessagesScreen extends StatefulWidget with RouteWidgetMixin { |
| 157 | + @override |
| 158 | + final RouteState routeState; |
| 159 | + |
| 160 | + const MessagesScreen({super.key, required this.routeState}); |
| 161 | + |
| 162 | + @override |
| 163 | + State<MessagesScreen> createState() => _MessagesScreenState(); |
| 164 | +} |
| 165 | + |
| 166 | +class _MessagesScreenState extends State<MessagesScreen> { |
| 167 | + int counter = 0; |
| 168 | + |
| 169 | + @override |
| 170 | + void initState() { |
| 171 | + super.initState(); |
| 172 | + debugPrint('INIT STATE MESSAGES - Params: ${widget.routeState.uri}'); |
| 173 | + } |
| 174 | + |
| 175 | + @override |
| 176 | + void dispose() { |
| 177 | + debugPrint('MessagesScreen disposed - Params: ${widget.routeState.uri}'); |
| 178 | + super.dispose(); |
| 179 | + } |
| 180 | + |
| 181 | + @override |
| 182 | + Widget build(BuildContext context) { |
| 183 | + final controller = RouteController.of(context); |
| 184 | + return Container( |
| 185 | + color: Colors.lightGreen, |
| 186 | + child: Center( |
| 187 | + child: Column( |
| 188 | + spacing: 8.0, |
| 189 | + mainAxisAlignment: MainAxisAlignment.center, |
| 190 | + children: [ |
| 191 | + Text('Extra: ${widget.routeState.extra}'), |
| 192 | + Text(counter.toString()), |
| 193 | + FilledButton( |
| 194 | + onPressed: () => setState(() => counter++), |
| 195 | + child: const Text('Increment'), |
| 196 | + ), |
| 197 | + FilledButton( |
| 198 | + onPressed: () => controller.push(HomeRouteState()), |
| 199 | + child: const Text('Go to Home'), |
| 200 | + ), |
| 201 | + FilledButton( |
| 202 | + onPressed: () => controller.push(MessagesRouteState()), |
| 203 | + child: const Text('Go to Messages (No Query)'), |
| 204 | + ), |
| 205 | + FilledButton( |
| 206 | + onPressed: |
| 207 | + () => controller.push( |
| 208 | + RouteState.parse('/messages?key1=value1').copyWith(shouldAnimate: true), |
| 209 | + ), |
| 210 | + child: const Text('Go to Messages (key1=value1)'), |
| 211 | + ), |
| 212 | + |
| 213 | + FilledButton( |
| 214 | + onPressed: |
| 215 | + () => controller.push( |
| 216 | + MessagesRouteState2().copyWith( |
| 217 | + extra: 'HELLO THERE HOW ARE YOU?', |
| 218 | + shouldAnimate: true, |
| 219 | + ), |
| 220 | + ), |
| 221 | + child: const Text('Go to Messages (key2=value2)'), |
| 222 | + ), |
| 223 | + ], |
| 224 | + ), |
| 225 | + ), |
| 226 | + ); |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 231 | + |
| 232 | +class HomeScreen extends StatelessWidget with RouteWidgetMixin { |
| 233 | + @override |
| 234 | + final RouteState routeState; |
| 235 | + |
| 236 | + const HomeScreen({super.key, required this.routeState}); |
| 237 | + |
| 238 | + @override |
| 239 | + Widget build(BuildContext context) { |
| 240 | + final controller = RouteController.of(context); |
| 241 | + debugPrint('INIT STATE HOME'); |
| 242 | + return Container( |
| 243 | + color: Colors.yellow, |
| 244 | + child: Center( |
| 245 | + child: Column( |
| 246 | + spacing: 8.0, |
| 247 | + mainAxisAlignment: MainAxisAlignment.center, |
| 248 | + children: [ |
| 249 | + FilledButton( |
| 250 | + onPressed: () => controller.push(RouteState.parse('/messages')), |
| 251 | + child: const Text('Go to Messages (No Query)'), |
| 252 | + ), |
| 253 | + FilledButton( |
| 254 | + onPressed: () => controller.push(RouteState.parse('/messages?key1=value1')), |
| 255 | + child: const Text('Go to Messages (key1=value1)'), |
| 256 | + ), |
| 257 | + FilledButton( |
| 258 | + onPressed: () => controller.push(RouteState.parse('/messages?key2=value2')), |
| 259 | + child: const Text('Go to Messages (key2=value2)'), |
| 260 | + ), |
| 261 | + FilledButton( |
| 262 | + onPressed: () => controller.push(RouteState.parse('/detail')), |
| 263 | + child: const Text('Go to Home Detail'), |
| 264 | + ), |
| 265 | + FilledButton( |
| 266 | + onPressed: |
| 267 | + () => controller.push( |
| 268 | + RouteState.parse('/chat').copyWith(extra: 'Hello from Home!'), |
| 269 | + ), |
| 270 | + child: const Text('Go to Chat'), |
| 271 | + ), |
| 272 | + ], |
| 273 | + ), |
| 274 | + ), |
| 275 | + ); |
| 276 | + } |
| 277 | +} |
| 278 | + |
| 279 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 280 | + |
| 281 | +class ChatScreen extends StatefulWidget with RouteWidgetMixin<String> { |
| 282 | + @override |
| 283 | + final RouteState<String?> routeState; |
| 284 | + |
| 285 | + const ChatScreen({super.key, required this.routeState}); |
| 286 | + |
| 287 | + @override |
| 288 | + State<ChatScreen> createState() => _ChatScreenState(); |
| 289 | +} |
| 290 | + |
| 291 | +class _ChatScreenState extends State<ChatScreen> { |
| 292 | + @override |
| 293 | + void initState() { |
| 294 | + super.initState(); |
| 295 | + debugPrint('INIT STATE CHAT - Params: ${widget.routeState}'); |
| 296 | + } |
| 297 | + |
| 298 | + @override |
| 299 | + void dispose() { |
| 300 | + debugPrint('ChatScreen disposed - Params: ${widget.routeState}'); |
| 301 | + super.dispose(); |
| 302 | + } |
| 303 | + |
| 304 | + @override |
| 305 | + Widget build(BuildContext context) { |
| 306 | + final controller = RouteController.of(context); |
| 307 | + return Container( |
| 308 | + color: Colors.blue, |
| 309 | + child: Center( |
| 310 | + child: Column( |
| 311 | + spacing: 8.0, |
| 312 | + mainAxisAlignment: MainAxisAlignment.center, |
| 313 | + children: [ |
| 314 | + Text(widget.routeState.extra.toString()), |
| 315 | + FilledButton( |
| 316 | + onPressed: () => controller.push(RouteState.parse('/home')), |
| 317 | + child: const Text('Go to Home'), |
| 318 | + ), |
| 319 | + FilledButton( |
| 320 | + onPressed: |
| 321 | + () => controller.push( |
| 322 | + RouteState.parse('/chat').copyWith(extra: 'Hello from Chat!'), |
| 323 | + ), |
| 324 | + child: const Text('Go to Chat (No ID)'), |
| 325 | + ), |
| 326 | + FilledButton( |
| 327 | + onPressed: |
| 328 | + () => controller.push( |
| 329 | + RouteState.parse( |
| 330 | + '/chat?id=123', |
| 331 | + ).copyWith(queryParameters: {'dude': '22'}, extra: 'Hello from Chat!'), |
| 332 | + ), |
| 333 | + child: const Text('Go to Chat (ID=123)'), |
| 334 | + ), |
| 335 | + ], |
| 336 | + ), |
| 337 | + ), |
| 338 | + ); |
| 339 | + } |
| 340 | +} |
| 341 | + |
| 342 | +// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ |
| 343 | + |
| 344 | +class HomeDetailScreen extends StatelessWidget with RouteWidgetMixin { |
| 345 | + @override |
| 346 | + final RouteState? routeState; |
| 347 | + |
| 348 | + const HomeDetailScreen({super.key, this.routeState}); |
| 349 | + |
| 350 | + @override |
| 351 | + Widget build(BuildContext context) { |
| 352 | + final controller = RouteController.of(context); |
| 353 | + debugPrint('INIT STATE HOME DETAIL'); |
| 354 | + return Container( |
| 355 | + color: Colors.green, |
| 356 | + child: Center( |
| 357 | + child: Column( |
| 358 | + spacing: 8.0, |
| 359 | + mainAxisAlignment: MainAxisAlignment.center, |
| 360 | + children: [ |
| 361 | + FilledButton( |
| 362 | + onPressed: () => controller.push(RouteState.parse('/home')), |
| 363 | + child: const Text('Back to Home'), |
| 364 | + ), |
| 365 | + FilledButton( |
| 366 | + onPressed: () => controller.push(RouteState.parse('/messages')), |
| 367 | + child: const Text('Go to Messages'), |
| 368 | + ), |
| 369 | + ], |
| 370 | + ), |
| 371 | + ), |
| 372 | + ); |
| 373 | + } |
| 374 | +} |
0 commit comments