gpt4 book ai didi

flutter - flutter :如何检测键盘?

转载 作者:行者123 更新时间:2023-12-03 03:30:17 28 4
gpt4 key购买 nike

我要检测键盘。并希望在键盘可见或不可见时向其显示其他文本。
但是我的代码无法正常工作。
这是我的代码。

class Search extends StatefulWidget {
@override
_SearchState createState() => _SearchState();
}

class _SearchState extends State<Search> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.white,
child: Column(
children: [
Expanded(
flex: 1,
child: ListView(
children: [
MediaQuery.of(context).viewInsets.bottom !=0 ? Center(child: Text("true"),) : Center(child: Text("false"),)
],
),
)
],
),
),
);
}
}

最佳答案

正如@Anas Mohammed所提到的,您可以使用 keyboard_visibility 软件包来实现。 Here是一个完整的示例:

import 'package:flutter/material.dart';
import 'package:keyboard_visibility/keyboard_visibility.dart';


void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Keyboard visibility example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: KeyboardVisibilityExample(),
);
}
}

class KeyboardVisibilityExample extends StatefulWidget {
KeyboardVisibilityExample({Key key}) : super(key: key);


@override
_KeyboardVisibilityExampleState createState() => _KeyboardVisibilityExampleState();
}

class _KeyboardVisibilityExampleState extends State<KeyboardVisibilityExample> {

KeyboardVisibilityNotification _keyboardVisibility = new KeyboardVisibilityNotification();
int _keyboardVisibilitySubscriberId;
bool _keyboardState;

@protected
void initState() {
super.initState();

_keyboardState = _keyboardVisibility.isKeyboardVisible;

_keyboardVisibilitySubscriberId = _keyboardVisibility.addNewListener(
onChange: (bool visible) {
setState(() {
_keyboardState = visible;
});
},
);
}

@override
void dispose() {
_keyboardVisibility.removeListener(_keyboardVisibilitySubscriberId);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Keyboard visibility example'),
),
body: Center(
child: Padding(
padding: EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: 'Input box for keyboard test',
),
),
Container(height: 60.0),
Text(
'The current state of the keyboard is: ' + (_keyboardState ? 'VISIBLE' : 'NOT VISIBLE'),
),
],
)
),
),
);
}
}

关于flutter - flutter :如何检测键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64411407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com