mirror of https://github.com/flutter/samples.git
[platform_channels] adds Platform Image demo (#475)
parent
70976eeb28
commit
ecb5aab94d
After Width: | Height: | Size: 70 KiB |
After Width: | Height: | Size: 70 KiB |
@ -0,0 +1,28 @@
|
||||
// Copyright 2020 The Flutter team. 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:typed_data';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// This class manages a [BasicMessageChannel] that can return an image loaded
|
||||
/// from a native asset. The [BasicMessageChannel] uses [StandardMessageCodec]
|
||||
/// since it supports [Uint8List], which is used to transport the image data.
|
||||
class PlatformImageFetcher {
|
||||
static final _basicMessageChannel = const BasicMessageChannel<dynamic>(
|
||||
'platformImageDemo', StandardMessageCodec());
|
||||
|
||||
/// Method responsible for providing the platform image.
|
||||
static Future<Uint8List> getImage() async {
|
||||
final reply = await _basicMessageChannel.send('getImage') as Uint8List;
|
||||
if (reply == null) {
|
||||
throw PlatformException(
|
||||
code: 'Error',
|
||||
message: 'Failed to load Platform Image',
|
||||
details: null,
|
||||
);
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
// Copyright 2020 The Flutter team. 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:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:platform_channels/src/image_basic_message_channel.dart';
|
||||
|
||||
/// Demonstrates how to use [BasicMessageChannel] to get an image from platform.
|
||||
///
|
||||
/// The widget uses [Image.memory] to display the image obtained from the
|
||||
/// platform.
|
||||
class PlatformImageDemo extends StatefulWidget {
|
||||
@override
|
||||
_PlatformImageDemoState createState() => _PlatformImageDemoState();
|
||||
}
|
||||
|
||||
class _PlatformImageDemoState extends State<PlatformImageDemo> {
|
||||
Future<Uint8List> imageData;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Platform Image Demo'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Flexible(
|
||||
child: FractionallySizedBox(
|
||||
widthFactor: 1,
|
||||
heightFactor: 0.6,
|
||||
child: FutureBuilder<Uint8List>(
|
||||
future: imageData,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.none) {
|
||||
return Placeholder();
|
||||
} else if (snapshot.hasError) {
|
||||
return Center(
|
||||
child: Text(snapshot.error.toString()),
|
||||
);
|
||||
} else if (snapshot.connectionState ==
|
||||
ConnectionState.done) {
|
||||
return Image.memory(
|
||||
snapshot.data,
|
||||
fit: BoxFit.fill,
|
||||
);
|
||||
}
|
||||
return CircularProgressIndicator();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 16,
|
||||
),
|
||||
RaisedButton(
|
||||
onPressed: imageData != null
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
imageData = PlatformImageFetcher.getImage();
|
||||
});
|
||||
},
|
||||
child: Text('Get Image'),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:platform_channels/src/platform_image_demo.dart';
|
||||
|
||||
void main() {
|
||||
group('Platform Image Demo tests', () {
|
||||
setUpAll(() {
|
||||
// Register a mock for MessageHandler.
|
||||
BasicMessageChannel<dynamic>('platformImageDemo', StandardMessageCodec())
|
||||
.setMockMessageHandler((dynamic message) async {
|
||||
var byteData = await rootBundle.load('assets/eat_new_orleans.jpg');
|
||||
return byteData.buffer.asUint8List();
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('Platform Image test', (tester) async {
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: PlatformImageDemo(),
|
||||
));
|
||||
|
||||
// Initially a PlaceHolder is displayed when imageData is null.
|
||||
expect(find.byType(Placeholder), findsOneWidget);
|
||||
expect(find.byType(Image), findsNothing);
|
||||
|
||||
// Tap on RaisedButton to get Image.
|
||||
await tester.tap(find.byType(RaisedButton));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(Placeholder), findsNothing);
|
||||
expect(find.byType(Image), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue