虚拟键盘出现时,如何将内容上移

  • 本文关键字:键盘 虚拟 flutter
  • 更新时间 :
  • 英文 :


我创建了一个简单的项目来说明我的问题。

import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
// method call to show the modal
showModal(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
// This will call the class Modal
return const MyModal();
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => showModal(context),
child: const Text('Click To Show Modal'),
),
),
);
}
}
class MyModal extends StatelessWidget {
const MyModal({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// This get the screen height and width
final size = MediaQuery.of(context).size;
return Dialog(
child: Scaffold(
body: SizedBox(
height: size.height,
width: size.width,
child: Column(
children: const [
// This sizebox will simulate the space the header takes
SizedBox(
height: 400,
),
TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
)
],
),
),
),
);
}
}

MyModal中,您可以尝试将SizedBox包装在SingleChildScrollView中。

class MyModal extends StatelessWidget {
const MyModal({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// This get the screen height and width
final size = MediaQuery.of(context).size;
return Dialog(
child: Scaffold(
body: SingleChildScrollView( // <<<<< HERE
child: SizedBox(
height: size.height,
width: size.width,
child: Column(
children: const [
// This sizebox will simulate the space the header takes
SizedBox(
height: 400,
),
TextField(
obscureText: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
)
],
),
),
),
),
);
}
}

相关内容

  • 没有找到相关文章

最新更新