import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; /// Commits [text] through the platform text input, as an IME, dead key, /// the emoji picker, dictation or a soft keyboard does: appended to what /// the focused client last told the platform, the caret at its end. Future commitText(WidgetTester tester, String text) async { final before = '${tester.testTextInput.editingState?['text'] ?? ''}'; final value = '$before$text'; tester.testTextInput.updateEditingValue( TextEditingValue( text: value, selection: TextSelection.collapsed(offset: value.length), ), ); await tester.pump(); } /// Sends [text] as composing (marked) text: drawn, not yet committed. Future composeText(WidgetTester tester, String text) async { final before = '${tester.testTextInput.editingState?['text'] ?? ''}'; final value = '$before$text'; tester.testTextInput.updateEditingValue( TextEditingValue( text: value, selection: TextSelection.collapsed(offset: value.length), composing: TextRange(start: before.length, end: value.length), ), ); await tester.pump(); } /// Presses [key] as a desktop platform does: the key event first, and only /// if no handler took it, its [character] (default: the key's label in /// lower case) through the text input. A key the terminal encoded is never /// typed a second time. Future typeKey( WidgetTester tester, LogicalKeyboardKey key, { String? character, }) async { final handled = await tester.sendKeyEvent(key, character: character); if (!handled) { final text = character ?? key.keyLabel.toLowerCase(); if (text.isNotEmpty) await commitText(tester, text); } await tester.pump(); } /// Taps the key toolbar's key [id] (`chat-shell-key-`), scrolling the /// touch key row to it first. Future tapKey(WidgetTester tester, String id) async { final key = find.byKey(Key('chat-shell-key-$id')); await tester.ensureVisible(key); await tester.pump(); await tester.tap(key); await tester.pump(); }