Repository navigation
Expand file tree
/
Copy pathmain.dart
More file actions
175 lines (157 loc) · 5.02 KB
/
Copy pathmain.dart
File metadata and controls
175 lines (157 loc) · 5.02 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import 'dart:io' show Platform;
import 'package:material_ui/material_ui.dart';
import 'package:flutter/services.dart';
import 'package:protocol_handler/protocol_handler.dart';
/// The scheme the example declares in its Info.plist files and Android
/// manifest, and registers on Windows.
const kScheme = 'myprotocol';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Writes the registration on Windows; elsewhere it reports whether the app
// package declares the scheme.
await protocolHandler.register(kScheme);
runApp(const ExampleApp());
}
class ExampleApp extends StatelessWidget {
const ExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'protocol_handler example',
theme: ThemeData(colorSchemeSeed: const Color(0xff416ff4)),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with ProtocolListener {
bool? _isRegistered;
String? _initialUrl;
final List<String> _receivedUrls = [];
@override
void initState() {
super.initState();
protocolHandler.addListener(this);
_init();
}
@override
void dispose() {
// Listener cleanup: a removed listener is never called again.
protocolHandler.removeListener(this);
super.dispose();
}
Future<void> _init() async {
final isRegistered = await protocolHandler.register(kScheme);
final initialUrl = await protocolHandler.getInitialUrl();
if (!mounted) return;
setState(() {
_isRegistered = isRegistered;
_initialUrl = initialUrl;
});
}
@override
void onProtocolUrlReceived(String url) {
// A launch URL the system delivered after _init read it arrives here too.
protocolHandler.getInitialUrl().then((initialUrl) {
if (mounted) setState(() => _initialUrl = initialUrl);
});
setState(() => _receivedUrls.insert(0, url));
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Url received: $url')));
}
void _setListening(bool isListening) {
if (isListening) {
// URLs that arrived while no listener was registered are delivered now.
protocolHandler.addListener(this);
} else {
protocolHandler.removeListener(this);
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('protocol_handler example')),
body: ListView(
children: [
ListTile(
title: const Text('register'),
subtitle: Text('$kScheme → ${_isRegistered ?? '…'}'),
),
ListTile(
title: const Text('getInitialUrl'),
subtitle: Text(_initialUrl ?? '(none)'),
onTap: _init,
),
SwitchListTile(
title: const Text('Listening'),
subtitle: Text(
protocolHandler.hasListeners
? '${protocolHandler.listeners.length} listener(s)'
: 'URLs are queued until a listener is added',
),
value: protocolHandler.listeners.contains(this),
onChanged: _setListening,
),
const _SectionTitle('Test commands (tap to copy)'),
for (final command in _testCommands())
ListTile(
dense: true,
title: Text(
command,
style: const TextStyle(fontFamily: 'monospace'),
),
onTap: () {
Clipboard.setData(ClipboardData(text: command));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Copied to clipboard')),
);
},
),
const _SectionTitle('Received urls'),
for (final url in _receivedUrls) ListTile(title: Text(url)),
],
),
);
}
}
class _SectionTitle extends StatelessWidget {
const _SectionTitle(this.text);
final String text;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 24, 16, 8),
child: Text(text, style: Theme.of(context).textTheme.titleSmall),
);
}
}
/// Commands that open a URL of [kScheme] on the current platform.
List<String> _testCommands() {
var prefix = '';
var suffix = '';
if (Platform.isIOS) {
prefix = 'xcrun simctl openurl booted';
} else if (Platform.isAndroid) {
prefix =
"adb shell 'am start -a android.intent.action.VIEW"
' -c android.intent.category.BROWSABLE -d';
suffix = "'";
} else if (Platform.isMacOS) {
prefix = 'open';
} else if (Platform.isWindows) {
prefix = 'start ""';
} else {
return const [];
}
const urls = [
'$kScheme://host/path/subpath',
'$kScheme://example.com/path/portion/?uid=123&token=abc',
'$kScheme://example.com/?addr=1%20Nowhere%20Rd&emoji=%F0%9F%98%82',
];
return [for (final url in urls) '$prefix "$url"$suffix'];
}