((Flutter)Textfield的自动对焦不会让导航器关闭键盘]]

问题描述 投票:0回答:1

当我在文本字段中使用autofocus: true并结合使用Navigator.push(context, SlideDownRoute(topPage: this.widget, bottomPage: Page2()));的动画的情况下:在新页面上不会关闭键盘。

导航前没有甚至没有通过使用FocusScope.of(context).unfocus();隐藏键盘的帮助。导航开始后,它将再次显示键盘。

enter image description here

我如何强制将其关闭?

这里是最小的示例代码:

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,
              ),

[当我使用自动对焦时:在我的Textfield中,结合使用Navigator.push(context,SlideDownRoute(topPage:this.widget,bottomPage:Page2()));动画在哪里:键盘是...

flutter animation stack navigator autofocus
1个回答
0
投票

我之前遇到过此问题。我在打电话给Navigator.pop()之前关闭了键盘来解决此问题。在onTap()方法中尝试此操作:

© www.soinside.com 2019 - 2024. All rights reserved.