contentInsertionConfiguration property
Configuration of handler for media content inserted via the system input method.
Defaults to null in which case media content insertion will be disabled, and the system will display a message informing the user that the text field does not support inserting media content.
Set ContentInsertionConfiguration.onContentInserted to provide a handler. Additionally, set ContentInsertionConfiguration.allowedMimeTypes to limit the allowable mime types for inserted content.
This example shows how to access the data for inserted content in your
TextField.
To see it in action, copy and run this code snippet on DartPad.
import 'dart:typed_data';
import 'package:flutter/material.dart';
/// Flutter code sample for [EditableText.contentInsertionConfiguration].
void main() => runApp(const KeyboardInsertedContentApp());
class KeyboardInsertedContentApp extends StatelessWidget {
const KeyboardInsertedContentApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(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: .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!),
],
),
);
}
}
If contentInsertionConfiguration is not provided, by default an empty list of mime types will be sent to the Flutter Engine. A handler function must be provided in order to customize the allowable mime types for inserted content.
If rich content is inserted without a handler, the system will display a message informing the user that the current text input does not support inserting rich content.
Implementation
final ContentInsertionConfiguration? contentInsertionConfiguration;