简体   繁体   English

虚拟键盘出现时如何向上移动内容

[英]How to move the content up when the virtual keyboard shows up

I created a simple project illustrating my problem.我创建了一个简单的项目来说明我的问题。

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',
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

In MyModal , you can try wrapping SizedBox inside a SingleChildScrollView .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',
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM