麻烦老师帮看看,如果用监听键盘的方式来退出浮层,我的写法有没有问题?

来源:11-9 基于HiOverlay实现弹幕输入界面

qq_夜雨肖潇_0

2022-08-30

class BarrageInput extends StatefulWidget {
final VoidCallback onTabClose;

const BarrageInput({Key key, @required this.onTabClose}) : super(key: key);

@override
State createState() => BarrageInputState();
}

class BarrageInputState extends State
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}

@override
void didChangeMetrics() {
super.didChangeMetrics();
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
if (MediaQuery.of(context).viewInsets.bottom == 0) {
//关闭键盘
print(‘hide----’);
if (widget.onTabClose != null) {
widget.onTabClose();
}
Navigator.of(context).pop();
} else {
//显示键盘
print(‘show----’);
}
});
});
}

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

@override
Widget build(BuildContext context) {
//输入框控制器
TextEditingController editingController = TextEditingController();
return Scaffold(
backgroundColor: Colors.transparent,
body: Column(
children: [
//空白区域点击关闭弹框
Expanded(
child: GestureDetector(
onTap: () {
if (widget.onTabClose != null) {
closeKeyboard(context);
// Navigator.of(context).pop();
}
},
child: Container(
color: Colors.transparent,
),
)),
//输入框
SafeArea(
child: Container(
color: Colors.white,
child: Row(
children: [
hiSpace(width: 15),
_buildInput(editingController, context),
_buildSendBtn(editingController, context)
],
),
)),
],
),
);
}

void closeKeyboard(BuildContext context) {
FocusScopeNode currentFocus = FocusScope.of(context);

/// 键盘是否是弹起状态
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
  FocusManager.instance.primaryFocus.unfocus();
}

}

///输入框
_buildInput(TextEditingController editingController, BuildContext context) {

}

///发送弹幕
void _send(String text, BuildContext context) {

}

///发送按钮
_buildSendBtn(TextEditingController editingController, BuildContext context) {

}
}

写回答

1回答

CrazyCodeBoy

2022-08-31

没看出问题,现在运行是遇到什么问题了吗
0
1
qq_夜雨肖潇_0
运行没有问题,就是不确定写着是否有隐患之类的,哈哈哈,感谢老师的回答
2022-08-31
共1条回复

Flutter高级进阶实战-仿哔哩哔哩-掌握Flutter高阶技能

一次性掌握Flutter高阶技能+商业级复杂项目架构设计与开发方案

1722 学习 · 870 问题

查看课程