ui_router 3.0.0 ui_router: ^3.0.0 copied to clipboard
GoRouter Alternative, without context
import 'package:flutter/material.dart';
import 'package:ui_router/ui_router.dart';
void main() {
final view = UiRouterView(router);
final app = MaterialApp(home: view);
runApp(app);
}
final router = UiRouter(
pages: [
UiPage(
path: '/index',
build: (state) => const MyIndexPage(),
),
UiBase(
path: '/base',
build: (state, child) => MyBasePage(child: child),
pages: [
UiPage(
path: '/index',
build: (state) => const MyIndexPage(),
),
UiPage(
path: '/end',
build: (state) => const MyEndPage(),
),
],
),
],
dialogs: [
UiDialog(
name: 'x',
build: (state) => MyDialogX(state),
),
],
redirect: (state) {
if (state.path == '/secret') {
return '/index';
} else {
return null;
}
},
);
//
//
//
// --- Example Widgets ---
//
//
//
// Root
class MyIndexPage extends StatelessWidget {
const MyIndexPage({super.key});
@override
Widget build(BuildContext context) {
final buttons = [
//
// Button
//
ElevatedButton(
onPressed: () {
router.push('/base');
},
child: const Text('Push To Base'),
),
//
// Button
//
ElevatedButton(
onPressed: () {
router.base('/base').push('/c');
},
child: const Text('Push To C'),
),
//
// Button
//
ElevatedButton(
onPressed: () async {
final answer = await router.openDialog('x');
if (answer.isOk) {
debugPrint('user tapped OK');
}
router.closeDialog();
},
child: const Text('Open X'),
),
];
return Scaffold(
appBar: AppBar(title: const Text('Page A')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: buttons,
),
),
);
}
}
// Base
class MyBasePage extends StatelessWidget {
const MyBasePage({
super.key,
required this.child,
});
final Widget child;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Base')),
body: Center(
child: SizedBox(
width: 300,
height: 500,
child: child,
),
),
);
}
}
// Base
class MyEndPage extends StatelessWidget {
const MyEndPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('End')),
);
}
}
// P
class MyPageP extends StatelessWidget {
const MyPageP({super.key});
@override
Widget build(BuildContext context) {
final buttons = [
//
// Button
//
ElevatedButton(
onPressed: () {
// pageRouter.pop();
},
child: const Text('Pop'),
),
];
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: buttons,
),
),
);
}
}
// Q
class MyPageQ extends StatelessWidget {
const MyPageQ({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text('Page Q'),
),
);
}
}
// X
class MyDialogX extends StatelessWidget {
const MyDialogX(this.state, {super.key});
final UiDialogState state;
@override
Widget build(BuildContext context) {
return Container(
width: 300,
height: 300,
color: Colors.white,
alignment: Alignment.center,
child: ElevatedButton(
onPressed: () {
/* send OK */
state.sendAnswer(UiOkAnswer());
},
child: const Text('OK'),
),
);
}
}