写点什么

Flutter 开发中的一些 Tips(二),2021 年网易 Android 岗面试必问

用户头像
Android架构
关注
发布于: 刚刚

本问题详细的代码见:点击查看

3.addPostFrameCallback

addPostFrameCallback回调方法在 Widget 渲染完成时触发,所以一般我们在获取页面中的 Widget 大小、位置时使用到。


前面第二点我有说到我会在接口请求前弹出 loading。如果我将请求方法放在了initState方法中,异常如下:


inheritFromWidgetOfExactType(_InheritedTheme) or inheritFromElement() was called before initState() completed. When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget. Typically references to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.


原因:弹出一个 DIalog 的showDialog方法会调用Theme.of(context, shadowThemeOnly: true),而这个方法会通过inheritFromWidgetOfExactType来跨组件获取 Theme 对象。



inheritFromWidgetOfExactType方法调用inheritFromElement



但是在_StateLifecyclecreateddefunct 时是无法跨组件拿到数据的,也就是initState()时和dispose()后。所以错误信息提示我们在 didChangeDependencies 调用。


然而放在didChangeDependencies后,新的异常:


setState(


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


) or markNeedsBuild() called during build. This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.


提示我们页面在 build 时不能调用setState()markNeedsBuild()方法。所以我们需要在 build 完成后,才可以去创建这个新的组件(这里就是 Dialog)。


所以解决方法就是使用addPostFrameCallback回调方法,等待页面 build 完成后在请求数据:


@overridevoid initState() {WidgetsBinding.instance.addPostFrameCallback((_){/// 接口请求});}


导致这类问题的场景很多,但是大体解决思路就是上述的办法。


本问题详细的代码见:点击查看

4.删除 emoji

不多哔哔,直接看图:



简单说就是删除一个 emoji 表情,一般需要点击删除两次。碰到个别的 emoji,需要删除 11 次!!其实这问题,也别吐槽 Flutter,基本 emoji 在各个平台上都或多或少有点问题。


原因就是:



这个问题我发现在 Flutter 的1.5.4+hotfix.2版本,解决方法可以参考:github.com/flutter/eng… 虽然只适用于长度为 2 位的 emoji。


幸运的是在最新的稳定版1.7.8+hotfix.3中修复了这个问题。不幸的是我发现了其他的问题,比如在我小米 MIX 2s 上删除文字时,有时会程序崩溃,其他一些机型正常。异常如下图:



我也在 Flutter 上发现了同样的问题 Issue,具体情况可以关注这个 Issue :github.com/flutter/flu… ,据 Flutter 团队的人员的回复,这个问题修复后不太可能进入 1.7 的稳定版本。。



所以建议大家谨慎升级,尤其是用于生产环境。那么这个问题暂时只能搁置下来了,等待更稳定的版本。。。


19.07.20 更新,官方发布了1.7.8+hotfix.4,修复了此问题。经过测试问题修复,大家可以放心使用了。


5.键盘

1.是否弹起

MediaQuery.of(context).viewInsets.bottom > 0


viewInsets.bottom就是键盘的顶部距离底部的高度,也就是弹起的键盘高度。如果你想实时过去键盘的弹出状态,配合使用didChangeMetrics。完整如下:


import 'package:flutter/material.dart';


typedef KeyboardShowCallback = void Function(bool isKeyboardShowing);


class KeyboardDetector extends StatefulWidget {


KeyboardShowCallback keyboardShowCallback;


Widget content;


KeyboardDetector({this.keyboardShowCallback, @required this.content});


@override_KeyboardDetectorState createState() => _KeyboardDetectorState();}


class _KeyboardDetectorState extends State<KeyboardDetector>with WidgetsBindingObserver {@overridevoid initState() {WidgetsBinding.instance.addObserver(this);super.initState();}


@overridevoid didChangeMetrics() {super.didChangeMetrics();WidgetsBinding.instance.addPostFrameCallback((_) {print(MediaQuery.of(context).viewInsets.bottom);setState(() {widget.keyboardShowCallback?.call(MediaQuery.of(context).viewInsets.bottom > 0);});});}


@overridevoid dispose() {WidgetsBinding.instance.removeObserver(this);super.dispose();}


@overrideWidget build(BuildContext context) {return widget.content;}}


代码来自项目 GSYFlutterDemo:https://github.com/CarGuo/GSYFlutterDemo

2.弹出键盘

if (MediaQuery.of(context).viewInsets.bottom == 0){final focusScope = FocusScope.of(context);focusScope.requestFocus(FocusNode());Future.delayed(Duration.zero, () => focusScope.requestFocus(_focusNode));}


其中_focusNode是对应的TextFieldfocusNode属性。

3.关闭键盘

FocusScope.of(context).requestFocus(FocusNode());/// 1.7.8 开始推荐以下方式(https://github.com/flutter/flutter/issues/7247)FocusScope.of(context).unfocus();


这里提一下关闭,一般来说即使键盘弹出,点击返回页面关闭,键盘就会自动收起。但是顺序是:


页面关闭 --> 键盘关闭


这样会导致键盘短暂的出现在你的上一页面,也就会出现短暂的部件溢出(关于溢出可见上篇)。



所以这时你就需要在页面关闭前手动调用关闭键盘的代码了。按道理是要放到deactivate或者dispose中处理的,可谁让context已经为 null 了,所以,老办法,拦截返回键:


@overrideWidget build(BuildContext context) {return WillPopScope(onWillPop: () async {// 拦截返回键 FocusScope.of(context).unfocus();return Future.value(true);},child: Container());}


当然如果你是自己代码调用的返回,可以在返回页面之前关闭键盘:


FocusScope.of(context).unfocus();Navigator.pop(context);


本问题详细的代码见:点击查看

6.Android 9.0 适配

话说现在新建的 Flutter 项目,Android 的 targetSdkVersion 默认都是 28。所以不可避免的就是 Android 9.0 的适配甚至 6,7,8 的适配,那我碰到的一个问题是接入的高德 2D 地图在 9.0 的机子上显示不出来。


问题的主要原因是 Android 9.0 要求默认使用加密连接,简单地说就是不允许使用 http 请求,要求使用 https。高德的 2D 地图 sdk 怀疑是使用了 http 请求,所以会加载不出。


解决方法两个:

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Flutter开发中的一些Tips(二),2021年网易Android岗面试必问