Created
May 21, 2021 17:29
-
-
Save esDotDev/e87526ec5b37f2511f7640fb63c21a0e 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
class DeeplinkExample extends StatefulWidget { | |
@override | |
_DeeplinkExampleState createState() => _DeeplinkExampleState(); | |
} | |
class _DeeplinkExampleState extends State<DeeplinkExample> { | |
late NavStackController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
RawKeyboard.instance.addListener((value) { | |
if (value is RawKeyDownEvent) { | |
if (value.logicalKey == LogicalKeyboardKey.digit1) _controller.path = "/"; | |
if (value.logicalKey == LogicalKeyboardKey.digit2) _controller.path = "/family/sells"; | |
if (value.logicalKey == LogicalKeyboardKey.digit3) _controller.path = "/family/sells/person/chris"; | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) => MaterialApp.router( | |
routeInformationParser: NavStackParser(), | |
routerDelegate: NavStackDelegate( | |
onGenerateStack: (context, nav) { | |
_controller = NavStackController.of(context); | |
return PathStack( | |
routes: { | |
const ['/']: StackRouteBuilder( | |
builder: (context, args) => FamiliesPage(), | |
), | |
const ['/family/:fid']: StackRouteBuilder( | |
builder: (context, args) { | |
final fid = args['fid']; | |
//final family = families.singleWhereOrNull((f) => f.id == fid); | |
//if (family == null) return Four04Page(message: 'unknown family $fid'); | |
return FamilyPage(fid: fid); | |
}, | |
), | |
const ['/family/:fid/person/:pid']: StackRouteBuilder( | |
builder: (context, args) { | |
final fid = args['fid']; | |
//final family = families.singleWhereOrNull((f) => f.id == fid); | |
//if (family == null) return Four04Page(message: 'unknown family $fid'); | |
final pid = args['pid']; | |
//final person = family.people.singleWhereOrNull((p) => p.id == pid); | |
//if (person == null) return Four04Page(message: 'unknown person $pid for family $fid'); | |
return PersonPage(fid: fid, pid: pid); | |
}, | |
), | |
}, | |
); | |
}, | |
), | |
title: 'Flutter Deep Linking Demo', | |
); | |
} | |
class Four04Page extends StatelessWidget { | |
const Four04Page({Key? key, required this.message}) : super(key: key); | |
final String message; | |
@override | |
Widget build(BuildContext context) { | |
return Center(child: Text("404")); | |
} | |
} | |
class PersonPage extends StatelessWidget { | |
const PersonPage({Key? key, required this.fid, required this.pid}) : super(key: key); | |
final String? fid; | |
final String? pid; | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Text("Person Page, fid=$fid, pid=$pid"), | |
); | |
} | |
} | |
class FamiliesPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Text("Families Page"), | |
); | |
} | |
} | |
class FamilyPage extends StatelessWidget { | |
const FamilyPage({Key? key, required this.fid}) : super(key: key); | |
final String? fid; | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Text("FamilyPage, fid: $fid"), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment