当我在文本字段中将autofocus: true
与Navigator.push(context, SlideDownRoute(topPage: this.widget, bottomPage: Page2()));
结合使用时,其中的动画位于堆栈中:键盘不会在新页面上关闭。
在导航进入帮助之前,甚至都没有使用FocusScope.of(context).unfocus();
隐藏键盘。导航开始后,它将再次显示键盘。
如何强制将其关闭?
这是最小的示例代码:
main.dart:
import 'package:experiment18/page2.dart';
import 'package:flutter/material.dart';
import 'slide_down_route.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: FirstPage(),
);
}
}
class FirstPage extends StatefulWidget {
@override
_FirstPageState createState() => _FirstPageState();
}
class _FirstPageState extends State<FirstPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Page'),
),
body: Center(
child: TextField(
autofocus: true,
onChanged: (_) {
Navigator.push(context, SlideDownRoute(topPage: this.widget, bottomPage: Page2()));
//Navigator.push(context, MaterialPageRoute(builder: (context) => Page2()));
},
)),
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
);
}
}
slide_down_route.dart
import 'package:flutter/material.dart';
class SlideDownRoute extends PageRouteBuilder {
final Widget topPage;
final Widget bottomPage;
SlideDownRoute({this.bottomPage, this.topPage})
: super(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
topPage,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
Stack(
children: <Widget>[
SlideTransition(
position: new Tween<Offset>(
begin: const Offset(0.0, 0.0),
end: const Offset(0.0, 0.0),
).animate(animation),
child: bottomPage,
),
SlideTransition(
position: new Tween<Offset>(
begin: const Offset(0.0, 0.0),
end: const Offset(0.0, 1.0),
).animate(CurvedAnimation(parent: animation, curve: Curves.easeInQuint)),
child: topPage,
),
],
),
);
}
现在什么时候解雇?
1。当我不使用自动对焦时
2。当我仅使用一个简单的动画时
Navigator.push(context, MaterialPageRoute(builder:(context) => Page2()));
slide_down_route.dart
中删除/注释这些行时:SlideTransition(
position: new Tween<Offset>(
begin: const Offset(0.0, 0.0),
end: const Offset(0.0, 1.0),
).animate(CurvedAnimation(parent: animation, curve: Curves.easeInQuint)),
child: topPage,
),