-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamic_linking.dart
119 lines (106 loc) · 3.14 KB
/
dynamic_linking.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:qlevar_router/qlevar_router.dart';
void main() {
runApp(WishlistApp());
}
class Wishlist {
final String id;
Wishlist(this.id);
}
class AppState extends ChangeNotifier {
final List<Wishlist> wishlists = <Wishlist>[];
Wishlist getList() {
final id = QR.params['id'].toString();
if (!wishlists.any((element) => element.id == id)) {
wishlists.add(Wishlist(id));
notifyListeners();
}
return wishlists.firstWhere((element) => element.id == id);
}
}
class WishlistApp extends StatelessWidget {
final appState = AppState();
WishlistApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Wishlist App',
routerDelegate: QRouterDelegate([
QRoute(path: '/', builder: () => WishlistListScreen(appState)),
QRoute(
path: '/wishlist/:id',
builder: () => WishlistScreen(appState.getList()))
]),
routeInformationParser: const QRouteInformationParser(),
);
}
}
class WishlistListScreen extends StatefulWidget {
final AppState appState;
const WishlistListScreen(this.appState, {super.key});
@override
State<WishlistListScreen> createState() => _WishlistListScreenState();
}
class _WishlistListScreenState extends State<WishlistListScreen> {
List<Wishlist> wishlists = [];
@override
void initState() {
super.initState();
widget.appState.addListener(() {
setState(() {
wishlists = widget.appState.wishlists;
});
});
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('Wishlist')),
body: ListView(
children: [
const Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Navigate to /wishlist/<ID> in the URL bar to dynamically '
'create a new wishlist.'),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () => QR.to('wishlist/${Random().nextInt(1000)}'),
child: const Text('Create a new Wishlist'),
),
),
for (var i = 0; i < wishlists.length; i++)
ListTile(
title: Text('Wishlist ${i + 1}'),
subtitle: Text(wishlists[i].id),
onTap: () => QR.to('wishlist/${wishlists[i].id}'),
)
],
),
);
@override
void dispose() {
widget.appState.dispose();
super.dispose();
}
}
class WishlistScreen extends StatelessWidget {
final Wishlist wishlist;
const WishlistScreen(this.wishlist, {super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('ID: ${QR.params['id']}',
style: Theme.of(context).textTheme.titleLarge),
],
),
),
);
}