ui_router 2.0.4 ui_router: ^2.0.4 copied to clipboard
Simple Router for Pages, Dialogs, Loading tasks. Powerful functions, interfaces will support your app.
import 'package:flutter/material.dart';
import 'package:ui_router/ui_router.dart';
final router = UiRouter(
pages: {
'/a': (params) => PageA(),
'/b': (params) => PageB(),
'/c/:msg': (params) => PageC(params['msg']!),
},
dialogs: {
'/x': (call) => DialogX(call),
},
);
void main() {
final widget = UiRouterWidget(router);
final app = MaterialApp(home: widget);
runApp(app);
}
//
// Page A
//
class PageA extends StatelessWidget {
push() {
router.push('/b');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Page A')),
body: ElevatedButton(onPressed: push, child: Text('Push To B')),
);
}
}
//
// Page B
//
class PageB extends StatelessWidget {
push() {
router.push('/c/Hello');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Page B')),
body: ElevatedButton(
onPressed: push, child: Text('Push to C with a message')),
);
}
}
//
// Page C
//
class PageC extends StatelessWidget {
final String message;
PageC(this.message);
openDialog() {
final call = router.open('/x');
call.onEvent((value) {
debugPrint(value);
router.close(call);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Page C')),
body: ElevatedButton(
onPressed: openDialog, child: Text('Received: $message')),
);
}
}
//
// Dialog X
//
class DialogX extends StatelessWidget {
final UiCall call;
DialogX(this.call);
event() {
call.event('Event from Dialog X');
}
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
width: 300,
height: 400,
padding: const EdgeInsets.all(50),
child: SizedBox(
width: 200,
height: 200,
child: ElevatedButton(
onPressed: event,
child: Text('OK'),
),
),
);
}
}