Flutter Driver目前仍缺少触发onFieldSubmitted
的功能。 我遵循这里提到的解决方法来触发TextInputAction
。这个解决方法适用于integration_test——Flutter中当前推荐的编写集成测试的方法。
让我们以以下方式编写目标测试脚本:
即位于/integration_test/foo_test.dart
中。
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:sample55101120/main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
testWidgets("TextFormField Test", (WidgetTester tester) async {
// Build the app and trigger a frame.
await tester.pumpWidget(app.MyApp());
/// Find TextFormField widget by Key and enter text
await tester.enterText(find.byKey(Key('search-email-search-box')), 'a@b.c');
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();
expect(find.text('a@b.c'), findsOneWidget);
});
}
让test_driver/integration_test.dart
为
import 'package:integration_test/integration_test_driver.dart';
Future<void> main() => integrationDriver();
关于我使用的示例应用:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void onSearchEmail(String value){
debugPrint('onSearchEmail $value');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
key: Key('search-email-search-box'),
textInputAction: TextInputAction.search,
onFieldSubmitted: (String value) => onSearchEmail(value),
),
],
),
),
);
}
}
要运行这个示例:
flutter drive --target=integration_test/foo_test.dart --driver=test_driver/integration_test.dart
searchEmailSearchBoxFinder
的吗? - DarshanTextField
上的onSubmitted
方法。 - OliverDeLange