This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[url_launcher] Update README to use code excerpts. #6042
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1248e12
Update README.md
New-dev0 7226977
Add build.excerpt.yaml and deps. Update CHANGELOG.
ditman 8d3c9ea
Configure excerpter so it can find Android XML files
ditman 8d4d5d2
Add docregions
ditman 8938f70
Update README with extracted excerpts.
ditman 0773c55
Enable excerpt analysis in CI
ditman 8a5704c
Update plugin registrants
ditman d8aaa1d
dart format + update excerpts
ditman 2bf3dc6
Address PR comments and issues.
ditman 9212fed
Update path dep to ^1.8.0
ditman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<?code-excerpt path-base="excerpts/packages/url_launcher_example"?> | ||
|
||
# url_launcher | ||
|
||
[![pub package](https://img.shields.io/pub/v/url_launcher.svg)](https://pub.dev/packages/url_launcher) | ||
|
@@ -14,6 +16,7 @@ To use this plugin, add `url_launcher` as a [dependency in your pubspec.yaml fil | |
|
||
### Example | ||
|
||
<?code-excerpt "basic.dart (basic-example)"?> | ||
``` dart | ||
import 'package:flutter/material.dart'; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
@@ -24,7 +27,7 @@ void main() => runApp( | |
const MaterialApp( | ||
home: Material( | ||
child: Center( | ||
child: RaisedButton( | ||
child: ElevatedButton( | ||
onPressed: _launchUrl, | ||
child: Text('Show Flutter homepage'), | ||
), | ||
|
@@ -33,8 +36,10 @@ void main() => runApp( | |
), | ||
); | ||
|
||
void _launchUrl() async { | ||
if (!await launchUrl(_url)) throw 'Could not launch $_url'; | ||
Future<void> _launchUrl() async { | ||
if (!await launchUrl(_url)) { | ||
throw 'Could not launch $_url'; | ||
} | ||
} | ||
``` | ||
|
||
|
@@ -65,7 +70,10 @@ on Android 11 (API 30) or higher. A `<queries>` | |
element must be added to your manifest as a child of the root element. | ||
|
||
Example: | ||
|
||
<?code-excerpt "../../android/app/src/main/AndroidManifest.xml (android-queries)" plaster="none"?> | ||
``` xml | ||
<!-- Provide required visibility configuration for API level 30 and above --> | ||
<queries> | ||
<!-- If your app checks for SMS support --> | ||
<intent> | ||
|
@@ -133,22 +141,37 @@ due to [a bug](https://github.com/dart-lang/sdk/issues/43838) in the way `Uri` | |
encodes query parameters. Using `queryParameters` will result in spaces being | ||
converted to `+` in many cases. | ||
|
||
<?code-excerpt "encoding.dart (encode-query-parameters)"?> | ||
```dart | ||
String? encodeQueryParameters(Map<String, String> params) { | ||
return params.entries | ||
.map((e) => '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}') | ||
.map((MapEntry<String, String> e) => | ||
'${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}') | ||
.join('&'); | ||
} | ||
// ··· | ||
final Uri emailLaunchUri = Uri( | ||
scheme: 'mailto', | ||
path: '[email protected]', | ||
query: encodeQueryParameters(<String, String>{ | ||
'subject': 'Example Subject & Symbols are allowed!', | ||
}), | ||
); | ||
|
||
launchUrl(emailLaunchUri); | ||
``` | ||
|
||
final Uri emailLaunchUri = Uri( | ||
scheme: 'mailto', | ||
path: '[email protected]', | ||
query: encodeQueryParameters(<String, String>{ | ||
'subject': 'Example Subject & Symbols are allowed!' | ||
}), | ||
); | ||
Encoding for `sms` is slightly different: | ||
|
||
launchUrl(emailLaunchUri); | ||
<?code-excerpt "encoding.dart (sms)"?> | ||
```dart | ||
final Uri smsLaunchUri = Uri( | ||
scheme: 'sms', | ||
path: '0118 999 881 999 119 7253', | ||
queryParameters: <String, String>{ | ||
'body': Uri.encodeComponent('Example Subject & Symbols are allowed!'), | ||
}, | ||
); | ||
``` | ||
|
||
### URLs not handled by `Uri` | ||
|
@@ -168,14 +191,17 @@ original APIs. | |
We recommend checking first whether the directory or file exists before calling `launchUrl`. | ||
|
||
Example: | ||
|
||
<?code-excerpt "files.dart (file)"?> | ||
```dart | ||
var filePath = '/path/to/file'; | ||
final String filePath = testFile.absolute.path; | ||
final Uri uri = Uri.file(filePath); | ||
|
||
if (await File(uri.toFilePath()).exists()) { | ||
if (!await launchUrl(uri)) { | ||
throw 'Could not launch $uri'; | ||
} | ||
if (!File(uri.toFilePath()).existsSync()) { | ||
throw '$uri does not exist!'; | ||
} | ||
if (!await launchUrl(uri)) { | ||
throw 'Could not launch $uri'; | ||
} | ||
``` | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,21 +7,29 @@ | |
--> | ||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<!--#docregion android-queries--> | ||
<!-- Provide required visibility configuration for API level 30 and above --> | ||
<queries> | ||
<!-- If your app checks for SMS support --> | ||
<intent> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably worth adding a comment saying this is only here for integration tests, and shouldn't be needed in most actual apps, so it doesn't confuse anyone else in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed; I've added a comment to the |
||
<action android:name="android.intent.action.VIEW" /> | ||
<data android:scheme="https" /> | ||
<data android:scheme="sms" /> | ||
</intent> | ||
<!-- If your app checks for call support --> | ||
<intent> | ||
<action android:name="android.intent.action.VIEW" /> | ||
<data android:scheme="tel" /> | ||
</intent> | ||
<!--#enddocregion android-queries--> | ||
<!-- The "https" scheme is only required for integration tests of this package. | ||
It shouldn't be needed in most actual apps, or show up in the README! --> | ||
<intent> | ||
<action android:name="android.intent.action.VIEW" /> | ||
<data android:scheme="sms" /> | ||
<data android:scheme="https" /> | ||
</intent> | ||
<!--#docregion android-queries--> | ||
</queries> | ||
<!--#enddocregion android-queries--> | ||
|
||
<application | ||
android:icon="@mipmap/ic_launcher" | ||
|
20 changes: 20 additions & 0 deletions
20
packages/url_launcher/url_launcher/example/build.excerpt.yaml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
targets: | ||
$default: | ||
sources: | ||
include: | ||
- lib/** | ||
- android/app/src/main/** | ||
# Some default includes that aren't really used here but will prevent | ||
# false-negative warnings: | ||
- $package$ | ||
- lib/$lib$ | ||
exclude: | ||
- '**/.*/**' | ||
- '**/build/**' | ||
- 'android/app/src/main/res/**' | ||
builders: | ||
code_excerpter|code_excerpter: | ||
enabled: true | ||
generate_for: | ||
- '**/*.dart' | ||
- android/**/*.xml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Run this example with: flutter run -t lib/basic.dart -d emulator | ||
|
||
// This file is used to extract code samples for the README.md file. | ||
// Run update-excerpts if you modify this file. | ||
|
||
// #docregion basic-example | ||
import 'package:flutter/material.dart'; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
||
final Uri _url = Uri.parse('https://flutter.dev'); | ||
|
||
void main() => runApp( | ||
const MaterialApp( | ||
home: Material( | ||
child: Center( | ||
child: ElevatedButton( | ||
onPressed: _launchUrl, | ||
child: Text('Show Flutter homepage'), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
|
||
Future<void> _launchUrl() async { | ||
if (!await launchUrl(_url)) { | ||
throw 'Could not launch $_url'; | ||
} | ||
} | ||
// #enddocregion basic-example |
70 changes: 70 additions & 0 deletions
70
packages/url_launcher/url_launcher/example/lib/encoding.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Run this example with: flutter run -t lib/encoding.dart -d emulator | ||
|
||
// This file is used to extract code samples for the README.md file. | ||
// Run update-excerpts if you modify this file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
||
/// Encode [params] so it produces a correct query string. | ||
/// Workaround for: https://github.com/dart-lang/sdk/issues/43838 | ||
// #docregion encode-query-parameters | ||
String? encodeQueryParameters(Map<String, String> params) { | ||
return params.entries | ||
.map((MapEntry<String, String> e) => | ||
'${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}') | ||
.join('&'); | ||
} | ||
// #enddocregion encode-query-parameters | ||
|
||
void main() => runApp( | ||
MaterialApp( | ||
home: Material( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: const <Widget>[ | ||
ElevatedButton( | ||
onPressed: _composeMail, | ||
child: Text('Compose an email'), | ||
), | ||
ElevatedButton( | ||
onPressed: _composeSms, | ||
child: Text('Compose a SMS'), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
|
||
void _composeMail() { | ||
// #docregion encode-query-parameters | ||
final Uri emailLaunchUri = Uri( | ||
scheme: 'mailto', | ||
path: '[email protected]', | ||
query: encodeQueryParameters(<String, String>{ | ||
'subject': 'Example Subject & Symbols are allowed!', | ||
}), | ||
); | ||
|
||
launchUrl(emailLaunchUri); | ||
// #enddocregion encode-query-parameters | ||
} | ||
|
||
void _composeSms() { | ||
// #docregion sms | ||
final Uri smsLaunchUri = Uri( | ||
scheme: 'sms', | ||
path: '0118 999 881 999 119 7253', | ||
queryParameters: <String, String>{ | ||
'body': Uri.encodeComponent('Example Subject & Symbols are allowed!'), | ||
}, | ||
); | ||
// #enddocregion sms | ||
|
||
launchUrl(smsLaunchUri); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// Run this example with: flutter run -t lib/files.dart -d linux | ||
|
||
// This file is used to extract code samples for the README.md file. | ||
// Run update-excerpts if you modify this file. | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:url_launcher/url_launcher.dart'; | ||
|
||
void main() => runApp( | ||
const MaterialApp( | ||
home: Material( | ||
child: Center( | ||
child: ElevatedButton( | ||
onPressed: _openFile, | ||
child: Text('Open File'), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
|
||
Future<void> _openFile() async { | ||
// Prepare a file within tmp | ||
final String tempFilePath = p.joinAll(<String>[ | ||
...p.split(Directory.systemTemp.path), | ||
'flutter_url_launcher_example.txt' | ||
]); | ||
final File testFile = File(tempFilePath); | ||
await testFile.writeAsString('Hello, world!'); | ||
// #docregion file | ||
final String filePath = testFile.absolute.path; | ||
final Uri uri = Uri.file(filePath); | ||
|
||
if (!File(uri.toFilePath()).existsSync()) { | ||
throw '$uri does not exist!'; | ||
} | ||
if (!await launchUrl(uri)) { | ||
throw 'Could not launch $uri'; | ||
} | ||
// #enddocregion file | ||
} |
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣
I was about to ask why this wasn't using some obviously fake number (like 123456789) when I realized what it was 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to find a cool "555" number, but ended up going for the easy to remember 0118 999 881 999 119 725... 3
(Turns out I'm almost 7 years late, yikes!)