我看到了一些解决方案并实施了它们。在模拟器上看起来不错,但在真实设备上不起作用(参见屏幕)。因此,当我单击文本字段时,键盘会根据焦点文本字段向上移动,并且我可以滚动,但在实际设备中不会发生这种情况。预先感谢您的宝贵时间。
BottomSheet 在 FloatinActionButton 上推出
showModalBottomSheet(
isScrollControlled: true,
backgroundColor: Colors.transparent,
context: context,
builder: (context) => SingleChildScrollView(
child: Container(
child: PostAd(),
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
)),
),
);
PostAD方法在Container中显示多个页面(取决于用户选择的索引)
return Center(
child: Container(
height: MediaQuery.of(context).size.height / 1.5,
child: Center(
child: Container(
child: Column(
children: [
Center(child: radioButtonCreate()), // Radio Buttons to select Page Index
Expanded(child: kadFormList[formIndex]), //Pages i.e forms that has text field
],
),
),
),
import 'package:flutter/material.dart';
import 'package:keyboard_visibility/keyboard_visibility.dart';
main() => runApp(MaterialApp(
home: Scaffold(
body: MyForm(),
),
));
class MyForm extends StatefulWidget {
MyForm() : super();
@override
State<StatefulWidget> createState() => MyFormState();
}
class MyFormState extends State<MyForm> {
bool isKeyboardVisible = false;
@override
void initState() {
super.initState();
KeyboardVisibilityNotification().addNewListener(
onChange: (bool visible) {
if (!visible) {
setState(() {
FocusScope.of(context).unfocus();
});
}
},
);
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(20.0, 20, 20, 0),
child: SingleChildScrollView(
reverse: isKeyboardVisible ? true : false,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 100),
child: Column(
children: <Widget>[
for (int i=0; i<100; i++) TextField()
],
),
),
),
);
}
}
小部件结构应该是这样的:
---SingleChildScrollView
---Padding : EdgeInsets.only(bottom: context.mediaQuery.viewInsets.bottom)
---孩子