forked from flutter/packages
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[image_picker] Adopt code excerpts in README (flutter#5523)
Improves image_picker_for_web README example and updates it to use code excerpts. Part of [flutter/flutter#102679](flutter/flutter#102679)
- Loading branch information
Showing
6 changed files
with
110 additions
and
9 deletions.
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
65 changes: 65 additions & 0 deletions
65
...ages/image_picker/image_picker_for_web/example/integration_test/readme_excerpts_test.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,65 @@ | ||
// 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. | ||
|
||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:image_picker_for_web_integration_tests/readme_excerpts.dart'; | ||
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
|
||
void main() { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
testWidgets('getImageFromPath loads image from XFile path', | ||
(WidgetTester tester) async { | ||
final XFile file = createXFileWeb(); | ||
|
||
// Use the excerpt code to get an Image from the XFile path. | ||
final Image image = getImageFromPath(file); | ||
|
||
await pumpImage(tester, image); | ||
|
||
// Check if Image widget is present. | ||
expect(find.byType(Image), findsOneWidget); | ||
}); | ||
|
||
testWidgets('getImageFromBytes loads image from XFile bytes', | ||
(WidgetTester tester) async { | ||
final XFile file = createXFileWeb(); | ||
|
||
// Use the excerpt code to get an Image from the XFile byte data. | ||
final Image image = await getImageFromBytes(file); | ||
|
||
await pumpImage(tester, image); | ||
|
||
// Check if Image widget is present. | ||
expect(find.byType(Image), findsOneWidget); | ||
}); | ||
} | ||
|
||
/// Creates an XFile with a 1x1 png file. | ||
XFile createXFileWeb() { | ||
const String pixel = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR' | ||
'42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='; | ||
final Uint8List data = base64Decode(pixel); | ||
return XFile.fromData( | ||
data, | ||
name: 'identity.png', | ||
mimeType: 'image/png', | ||
lastModified: DateTime.now(), | ||
); | ||
} | ||
|
||
/// Pumps an [image] widget into a [tester]. | ||
Future<void> pumpImage(WidgetTester tester, Image image) async { | ||
await tester.pumpWidget(MaterialApp( | ||
home: Scaffold( | ||
body: image, | ||
), | ||
)); | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/image_picker/image_picker_for_web/example/lib/readme_excerpts.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,35 @@ | ||
// 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. | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; | ||
|
||
/// Demonstrates creating an Image widget from an XFile's path. | ||
Image getImageFromPath(XFile pickedFile) { | ||
final Image image; | ||
|
||
// #docregion ImageFromPath | ||
if (kIsWeb) { | ||
image = Image.network(pickedFile.path); | ||
} else { | ||
image = Image.file(File(pickedFile.path)); | ||
} | ||
// #enddocregion ImageFromPath | ||
|
||
return image; | ||
} | ||
|
||
/// Demonstrates creating an Image widget from an XFile's bytes. | ||
Future<Image> getImageFromBytes(XFile pickedFile) async { | ||
final Image image; | ||
|
||
// #docregion ImageFromBytes | ||
image = Image.memory(await pickedFile.readAsBytes()); | ||
// #enddocregion ImageFromBytes | ||
|
||
return image; | ||
} |
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