简体   繁体   English

如何在 Flutter 中让 Form 出现在键盘上方?

[英]How to make a Form appear above the keyboard in Flutter?

I have created a addNewProducts function which contains my showModalBottomSheet .我创建了一个addNewProducts函数,其中包含我的showModalBottomSheet I used for my Add a new product form I add a Form which contains different TextFormField for taking input value.我用于添加新产品表单我添加了一个包含不同TextFormField表单以获取输入值。 When I click on my floatingActionButton it shows the form but when I want to write something the input keyboard cover the Form area so I can not add anything.当我单击我的浮动操作按钮时,它会显示表单,但是当我想写一些东西时,输入键盘会覆盖表单区域,因此我无法添加任何内容。 it's not scrolling.它没有滚动。

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

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

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

class _UserScreenState extends State<UserScreen> {
  void addNewProducts(BuildContext ctx) {
    showModalBottomSheet(
      context: ctx,
      builder: (_) {
        return GestureDetector(
          onTap: () {},
          behavior: HitTestBehavior.opaque,
          child: Card(
            child: SingleChildScrollView(
              child: Form(
                child: Column(
                  children: <Widget>[
                    TextFormField(
                      keyboardType: TextInputType.emailAddress,
                      decoration: InputDecoration(
                          labelText: 'Product Name',
                          icon: Icon(Icons.insert_chart)),
                    ),
                    TextFormField(
                      keyboardType: TextInputType.emailAddress,
                      decoration: InputDecoration(
                          labelText: 'Product Description',
                          icon: Icon(Icons.description)),
                    ),
                    TextFormField(
                      keyboardType: TextInputType.emailAddress,
                      decoration: InputDecoration(
                          labelText: 'Minimum Bid Price',
                          icon: Icon(Icons.price_check)),
                    ),
                    TextFormField(
                      keyboardType: TextInputType.emailAddress,
                      decoration: InputDecoration(
                          labelText: 'Auction End DateTime',
                          icon: Icon(Icons.data_saver_off)),
                    ),
                    SizedBox(
                      height: 20,
                    ),
                    ElevatedButton(
                      onPressed: () {},
                      child: Text('Add Product'),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('User Screen'),
        actions: [
          DropdownButton(
            items: [
              DropdownMenuItem(
                child: Container(
                  child: Row(
                    children: <Widget>[
                      Icon(
                        Icons.exit_to_app,
                        color: Theme.of(context).indicatorColor,
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Text('Logout')
                    ],
                  ),
                ),
                value: 'logout',
              ),
            ],
            onChanged: (itemIdentifire) {
              if (itemIdentifire == 'logout') {
                FirebaseAuth.instance.signOut();
              }
            },
          )
        ],
      ),
      body: Card(),
      // ListView.builder(
      //   itemCount: 10,
      //   itemBuilder: (ctx, index) => Container(
      //     padding: EdgeInsets.all(8),
      //     child: Text('This Works!'),
      //   ),
      // ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          addNewProducts(context);
        },
      ),
    );
  }
}

这是表单,当我点击我的浮动操作按钮时它会出现,但是当我想在 TextFormField 上写一些东西时,输入键盘会覆盖整个区域

当我单击 TextFormField 以获取输入数据时,就会发生这种情况。键盘覆盖整个表格

I have changed my Column to ListView now its perfectly work and its now scrollable.我已将我的Column更改为ListView现在它可以完美工作并且现在可以滚动。

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/rendering.dart';

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

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

class _UserScreenState extends State<UserScreen> {
  bool isFabVisibale = false;
  void addNewProducts(BuildContext ctx) {
    showModalBottomSheet(
      isScrollControlled: true,
      context: ctx,
      builder: (_) {
        return GestureDetector(
          onTap: () {},
          behavior: HitTestBehavior.opaque,
          child: Form(
            child: ListView(
              children: <Widget>[
                TextFormField(
                  keyboardType: TextInputType.emailAddress,
                  decoration: InputDecoration(
                      labelText: 'Product Name',
                      icon: Icon(Icons.insert_chart)),
                ),
                TextFormField(
                  keyboardType: TextInputType.emailAddress,
                  decoration: InputDecoration(
                      labelText: 'Product Description',
                      icon: Icon(Icons.description)),
                ),
                TextFormField(
                  keyboardType: TextInputType.emailAddress,
                  decoration: InputDecoration(
                      labelText: 'Minimum Bid Price',
                      icon: Icon(Icons.price_check)),
                ),
                TextFormField(
                  keyboardType: TextInputType.emailAddress,
                  decoration: InputDecoration(
                      labelText: 'Auction End DateTime',
                      icon: Icon(Icons.data_saver_off)),
                ),
                SizedBox(
                  height: 20,
                ),
                ElevatedButton(
                  onPressed: () {},
                  child: Text('Add Product'),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('User Screen'),
        actions: [
          DropdownButton(
            items: [
              DropdownMenuItem(
                child: Container(
                  child: Row(
                    children: <Widget>[
                      Icon(
                        Icons.exit_to_app,
                        color: Theme.of(context).indicatorColor,
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Text('Logout')
                    ],
                  ),
                ),
                value: 'logout',
              ),
            ],
            onChanged: (itemIdentifire) {
              if (itemIdentifire == 'logout') {
                FirebaseAuth.instance.signOut();
              }
            },
          ),

          // DropdownButton(
          //   items: [
          //     DropdownMenuItem(
          //       child: Container(
          //         child: Row(
          //           children: <Widget>[
          //             Icon(
          //               Icons.add,
          //               color: Theme.of(context).indicatorColor,
          //             ),
          //             SizedBox(
          //               width: 8,
          //             ),
          //             Text('Add New Items')
          //           ],
          //         ),
          //       ),
          //       value: 'AddProduct',
          //     ),
          //   ],
          //   onChanged: (itemIdentifire) {
          //     if (itemIdentifire == 'AddProduct') {
          //       addNewProducts(context);
          //     }
          //   },
          // ),
        ],
      ),
      body: NotificationListener<UserScrollNotification>(
        onNotification: (notification) {
          if (notification.direction == ScrollDirection.forward) {
            setState(() {
              isFabVisibale = true;
            });
          }
          if (notification.direction == ScrollDirection.reverse) {
            setState(() {
              isFabVisibale = false;
            });
          }
          return true;
        },
        child: ListView.builder(
          itemCount: 100,
          itemBuilder: (ctx, index) => Container(
            padding: EdgeInsets.all(8),
            child: Text('This Works!'),
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: isFabVisibale
          ? FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                addNewProducts(context);
              },
            )
          : null,
    );
  }
}

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

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