Keep TextFields visible when keyboard comes up (#18291)

Fixes #10826.

Also in this PR: If you focus a text field, scroll it out of view and then start typing it will scroll back into view.
This commit is contained in:
Michael Goderbauer
2018-06-15 10:11:10 -07:00
committed by GitHub
parent e713b3349a
commit 409007827b
7 changed files with 345 additions and 12 deletions

View File

@@ -25,6 +25,7 @@ Future<TaskResult> runEndToEndTests() async {
'lib/keyboard_resize.dart',
'lib/driver.dart',
'lib/screenshot.dart',
'lib/keyboard_textfield.dart',
];
for (final String entryPoint in entryPoints) {

View File

@@ -0,0 +1,72 @@
// Copyright 2018 The Chromium 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 'package:flutter/material.dart';
import 'package:flutter_driver/driver_extension.dart';
import 'keys.dart' as keys;
void main() {
enableFlutterDriverExtension();
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Keyboard & TextField',
theme: new ThemeData(primarySwatch: Colors.blue),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ScrollController _controller = new ScrollController();
double offset = 0.0;
@override
void initState() {
super.initState();
_controller.addListener(() {
setState(() {
offset = _controller.offset;
});
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Column(
children: <Widget>[
new Text('$offset',
key: const ValueKey<String>(keys.kOffsetText),
),
new Expanded(
child: new ListView(
key: const ValueKey<String>(keys.kListView),
controller: _controller,
children: <Widget>[
new Container(
height: MediaQuery.of(context).size.height,
),
const TextField(
key: const ValueKey<String>(keys.kDefaultTextField),
),
],
),
),
],
),
);
}
}

View File

@@ -5,3 +5,5 @@
const String kDefaultTextField = 'default_textfield';
const String kHeightText = 'height_text';
const String kUnfocusButton = 'unfocus_button';
const String kOffsetText = 'offset_text';
const String kListView = 'list_view';

View File

@@ -0,0 +1,53 @@
// Copyright 2018 The Chromium 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:async';
import 'package:integration_ui/keys.dart' as keys;
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
void main() {
group('end-to-end test', () {
FlutterDriver driver;
setUpAll(() async {
driver = await FlutterDriver.connect();
});
tearDownAll(() async {
driver?.close();
});
test('Textfield scrolls back into view after covered by keyboard', () async {
await driver.setTextEntryEmulation(enabled: false); // we want the keyboard to come up
final SerializableFinder listViewFinder = find.byValueKey(keys.kListView);
final SerializableFinder textFieldFinder = find.byValueKey(keys.kDefaultTextField);
final SerializableFinder offsetFinder = find.byValueKey(keys.kOffsetText);
// Align TextField with bottom edge to ensure it would be covered when keyboard comes up.
await driver.waitForAbsent(textFieldFinder);
await driver.scrollUntilVisible(
listViewFinder,
textFieldFinder,
alignment: 1.0,
dyScroll: -20.0,
);
await driver.waitFor(textFieldFinder);
final double scrollOffsetWithoutKeyboard = double.parse(await driver.getText(offsetFinder));
// Bring up keyboard
await driver.tap(textFieldFinder);
await new Future<Null>.delayed(const Duration(seconds: 1));
// Ensure that TextField is visible again
await driver.waitFor(textFieldFinder);
final double scrollOffsetWithKeyboard = double.parse(await driver.getText(offsetFinder));
// Ensure the scroll offset changed appropriately when TextField scrolled back into view.
expect(scrollOffsetWithKeyboard, greaterThan(scrollOffsetWithoutKeyboard));
});
});
}