局部控制键盘收回
import 'package:flutter/material.dart'; import 'package:TLDF/main.dart'; class KeyboardBack { static BuildContext context = navigatorKey.currentState.overlay.context; static FocusScopeNode currentFocus = FocusScope.of(context); static void keyboardBack() { if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) { FocusManager.instance.primaryFocus.unfocus(); } } }
//需要的地方执行 KeyboardBack.KeyboardBack();
全局控制键盘收回
在main.dart child: MaterialApp( title: KString.mainTitle, //Flutter商城 debugShowCheckedModeBanner: false, navigatorKey: navigatorKey, onGenerateRoute: Application.router.generator, //主题 theme: ThemeData( primaryColor: KColor.primaryColor, ///全局解决ios键盘默认颜色dark brightness: Brightness.light, //指定亮度主题,有白色/黑色两种可选。 primaryColorBrightness: Brightness.light, accentColorBrightness: Brightness.light), home: SplashScreen(), navigatorObservers: [MyApp.routeObserver], //全局监听键盘 builder: (context, child) => Scaffold( body: Listener( onPointerDown: (PointerDownEvent event){ _event=event; print("按下"+_event.timeStamp.toString()); }, onPointerUp: (PointerUpEvent event) { int interval=event.timeStamp.inSeconds-_event.timeStamp.inSeconds;
//防止复制粘贴时触发 if (interval<1) { FocusScopeNode currentFocus = FocusScope.of(context); if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) { FocusManager.instance.primaryFocus.unfocus(); } } }, child: child, ), ),