Last active
April 16, 2021 21:56
-
-
Save obadajasm/aa29b7c612d98bbba5d3cd4f799f083a to your computer and use it in GitHub Desktop.
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
import 'package:flutter/material.dart'; | |
import 'package:qlevar_router/qlevar_router.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router( | |
routeInformationParser: QRouteInformationParser(), | |
routerDelegate: QRouterDelegate( | |
AppRoutes().routes, | |
initPath: '/products', | |
), | |
); | |
} | |
} | |
class ProductsPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Material( | |
child: Column( | |
children: [ | |
const SizedBox(height: 30), | |
Text('ProductsPage'), | |
const SizedBox(height: 30), | |
Wrap( | |
children: List.generate(10, (index) => index.toString()) | |
.map( | |
(e) => TextButton( | |
child: Padding( | |
padding: const EdgeInsets.all(32.0), | |
child: Text(e), | |
), | |
onPressed: () { | |
/// Navigate to the singleProductPage route using | |
/// QR.to('/products/$e'); | |
/// or | |
QR.toName(AppRoutes.singleProductPage, params: {'id': e}); | |
}, | |
), | |
) | |
.toList(), | |
) | |
], | |
), | |
); | |
} | |
} | |
class SingleProductPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Material( | |
child: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
Text('SingleProductPage with id = ${QR.params['id']}'), | |
const SizedBox(height: 10), | |
///show only when you recive the `lang` query paramter | |
if (QR.params['lang'] != null) | |
Text('Selected Langauge with Lang = ${QR.params['lang']}'), | |
], | |
), | |
), | |
); | |
} | |
} | |
class AppRoutes { | |
static String productsPage = 'ProductsPage'; | |
static String singleProductPage = 'SingleProductPage'; | |
final routes = [ | |
QRoute( | |
name: productsPage, | |
path: '/products', | |
builder: () => ProductsPage(), | |
children: [ | |
QRoute( | |
///use the name to route using `QR.toName` | |
name: singleProductPage, | |
path: '/:id', | |
builder: () => SingleProductPage(), | |
), | |
], | |
), | |
]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment