forked from firka/flutter
Add support for image insertion on Android (#110052)
* Add support for image insertion on Android * Fix checks * Use proper Dart syntax on snippet * Specify type annotation on list * Fix nits, add some asserts, and improve example code * Add missing import * Fix nullsafety error * Fix nullsafety error * Remove reference to contentCommitMimeTypes in docs * Fix nits * Fix warnings and import * Add test for content commit in editable_text_test.dart * Check that URIs are equal in test * Fix nits and rename functions / classes to be more self-explanatory * Fix failing debugFillProperties tests * Add empty implementation to `insertContent` in TextInputClient * Tweak documentation slightly * Improve docs for contentInsertionMimeTypes and fix assert * Rework contentInsertionMimeType asserts * Add test for onContentInserted example * Switch implementation to a configuration class for more granularity in setting mime types * Fix nits * Improve docs and fix doc tests * Fix more nits (LongCatIsLooong) * Fix failing tests * Make parameters (guaranteed by platform to be non-nullable) non-nullable * Fix analysis issues
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// Copyright 2014 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.
|
||||
|
||||
// Flutter code sample for EditableText.onContentInserted
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const KeyboardInsertedContentApp());
|
||||
|
||||
class KeyboardInsertedContentApp extends StatelessWidget {
|
||||
const KeyboardInsertedContentApp({super.key});
|
||||
|
||||
static const String _title = 'Keyboard Inserted Content Sample';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
title: _title,
|
||||
home: KeyboardInsertedContentDemo(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class KeyboardInsertedContentDemo extends StatefulWidget {
|
||||
const KeyboardInsertedContentDemo({super.key});
|
||||
|
||||
@override
|
||||
State<KeyboardInsertedContentDemo> createState() => _KeyboardInsertedContentDemoState();
|
||||
}
|
||||
|
||||
class _KeyboardInsertedContentDemoState extends State<KeyboardInsertedContentDemo> {
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
Uint8List? bytes;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Keyboard Inserted Content Sample')),
|
||||
body: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
const Text("Here's a text field that supports inserting only png or gif content:"),
|
||||
TextField(
|
||||
controller: _controller,
|
||||
contentInsertionConfiguration: ContentInsertionConfiguration(
|
||||
allowedMimeTypes: const <String>['image/png', 'image/gif'],
|
||||
onContentInserted: (KeyboardInsertedContent data) async {
|
||||
if (data.data != null) {
|
||||
setState(() {
|
||||
bytes = data.data;
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
if (bytes != null)
|
||||
const Text("Here's the most recently inserted content:"),
|
||||
if (bytes != null)
|
||||
Image.memory(bytes!),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright 2014 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 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_api_samples/widgets/editable_text/editable_text.on_content_inserted.0.dart' as example;
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Image.memory displays inserted content', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const example.KeyboardInsertedContentApp(),
|
||||
);
|
||||
|
||||
expect(find.text('Keyboard Inserted Content Sample'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byType(EditableText));
|
||||
await tester.enterText(find.byType(EditableText), 'test');
|
||||
await tester.idle();
|
||||
|
||||
const String uri = 'content://com.google.android.inputmethod.latin.fileprovider/test.png';
|
||||
const List<int> kBlueSquarePng = <int>[
|
||||
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49,
|
||||
0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x32, 0x08, 0x06,
|
||||
0x00, 0x00, 0x00, 0x1e, 0x3f, 0x88, 0xb1, 0x00, 0x00, 0x00, 0x48, 0x49, 0x44,
|
||||
0x41, 0x54, 0x78, 0xda, 0xed, 0xcf, 0x31, 0x0d, 0x00, 0x30, 0x08, 0x00, 0xb0,
|
||||
0x61, 0x63, 0x2f, 0xfe, 0x2d, 0x61, 0x05, 0x34, 0xf0, 0x92, 0xd6, 0x41, 0x23,
|
||||
0x7f, 0xf5, 0x3b, 0x20, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x36, 0x06, 0x03, 0x6e, 0x69, 0x47, 0x12, 0x8e, 0xea, 0xaa,
|
||||
0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
|
||||
];
|
||||
final ByteData? messageBytes = const JSONMessageCodec().encodeMessage(<String, dynamic>{
|
||||
'args': <dynamic>[
|
||||
-1,
|
||||
'TextInputAction.commitContent',
|
||||
jsonDecode('{"mimeType": "image/png", "data": $kBlueSquarePng, "uri": "$uri"}'),
|
||||
],
|
||||
'method': 'TextInputClient.performAction',
|
||||
});
|
||||
|
||||
try {
|
||||
await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
|
||||
'flutter/textinput',
|
||||
messageBytes,
|
||||
(ByteData? _) {},
|
||||
);
|
||||
} catch (_) {}
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(Image), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user