【问题标题】:List view is not Scroll able when I put this property resizeToAvoidBottomInset : false,当我放置此属性 resizeToAvoidBottomInset 时,列表视图无法滚动:false,
【发布时间】:2020-01-03 18:30:03
【问题描述】:

我正在创建一个页面,在其中创建一些 TextFormField 并将它们放入列表视图中,并且该列表视图位于堆栈小部件中。当我点击TextFormField 时,键盘出现并且背景图像(我放入堆栈中的中心标签)根据剩余的屏幕进行调整。我使用resizeToAvoidBottomInset : false,以避免调整屏幕大小,但是通过放置这个我无法滚动没有此属性resizeToAvoidBottomInset : false 时可滚动的列表视图。

import 'package:flutter/material.dart';
import 'main.dart';
import 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(MaterialApp(
    home: Contactus(),
  ));
}

class Contactus extends StatelessWidget {
  static final _formkey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    return Scaffold(
        resizeToAvoidBottomInset: false,
        body: Stack(
          children: <Widget>[
            Center(
              child: Image.asset(
                'imges/bg.png',
                fit: BoxFit.fill,
              ),
            ),
            Center(
              child: ListView(
                children: <Widget>[
                  Container(
                      child: Stack(
                    children: <Widget>[
                      Form(
                        key: _formkey,
                        child: Padding(
                          padding: const EdgeInsets.only(
                              right: 80, top: 10, bottom: 10, left: 80),
                          child: Column(
                            children: <Widget>[
                              Stack(
                                children: <Widget>[
                                  Center(
                                    child: Container(
                                      margin: EdgeInsets.only(
                                          left: 20, right: 20, top: 0),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: <Widget>[
                                          Expanded(
                                            child: Container(
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(12.0)),
                                                color: Colors.white,
                                              ),
                                              child: TextFormField(
                                                textAlign: TextAlign.center,
                                                style: TextStyle(
                                                    color: Colors.white,
                                                    fontWeight:
                                                        FontWeight.w300),
                                                decoration: InputDecoration(
                                                  hintText: "Your First Name",
                                                  hintStyle: TextStyle(
                                                      fontWeight:
                                                          FontWeight.w300,
                                                      color: Colors.black),
                                                  prefixIcon:
                                                      Icon(Icons.account_box),
                                                  errorStyle: TextStyle(
                                                    color: Colors.yellowAccent,
                                                    fontSize: 15,
                                                  ),
                                                  border: OutlineInputBorder(
//                      borderRadius: BorderRadius.circular(25.0),
                                                    borderSide: BorderSide(),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                          SizedBox(
                                            width: 20.0,
                                          ),
                                          Expanded(
                                            child: Container(
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(12.0)),
                                                color: Colors.white,
                                              ),
                                              child: TextFormField(
                                                textAlign: TextAlign.center,
                                                style: TextStyle(
                                                    color: Colors.white,
                                                    fontWeight:
                                                        FontWeight.w300),
                                                decoration: InputDecoration(
                                                  hintText: "Your Last Name",
                                                  hintStyle: TextStyle(
                                                      fontWeight:
                                                          FontWeight.w300,
                                                      color: Colors.black),
                                                  prefixIcon:
                                                      Icon(Icons.account_box),
                                                  errorStyle: TextStyle(
                                                    color: Colors.yellowAccent,
                                                    fontSize: 15,
                                                  ),
                                                  border: OutlineInputBorder(
//                      borderRadius: BorderRadius.circular(25.0),
                                                    borderSide: BorderSide(),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                    height: 10.0,
                                  ),
                                  Center(
                                    child: Container(
                                      margin: EdgeInsets.only(
                                          left: 20, right: 20, top: 60),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: <Widget>[
                                          Expanded(
                                            child: Container(
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(12.0)),
                                                color: Colors.white,
                                              ),
                                              child: TextFormField(
                                                textAlign: TextAlign.center,
                                                style: TextStyle(
                                                    color: Colors.white,
                                                    fontWeight:
                                                        FontWeight.w300),
                                                decoration: InputDecoration(
                                                  hintText: "Email ADDRESS",
                                                  hintStyle: TextStyle(
                                                      fontWeight:
                                                          FontWeight.w300,
                                                      color: Colors.black),
                                                  prefixIcon: Icon(Icons.email),
                                                  errorStyle: TextStyle(
                                                    color: Colors.yellowAccent,
                                                    fontSize: 15,
                                                  ),
                                                  border: OutlineInputBorder(
//                      borderRadius: BorderRadius.circular(25.0),
                                                    borderSide: BorderSide(),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                          SizedBox(
                                            width: 20.0,
                                          ),
                                          Expanded(
                                            child: Container(
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(12.0)),
                                                color: Colors.white,
                                              ),
                                              child: TextFormField(
                                                textAlign: TextAlign.center,
                                                style: TextStyle(
                                                    color: Colors.white,
                                                    fontWeight:
                                                        FontWeight.w300),
                                                decoration: InputDecoration(
                                                  hintText: "Phone Number",
                                                  hintStyle: TextStyle(
                                                      fontWeight:
                                                          FontWeight.w300,
                                                      color: Colors.black),
                                                  prefixIcon: Icon(Icons.phone),
                                                  errorStyle: TextStyle(
                                                    color: Colors.yellowAccent,
                                                    fontSize: 15,
                                                  ),
                                                  border: OutlineInputBorder(
//                      borderRadius: BorderRadius.circular(25.0),
                                                    borderSide: BorderSide(),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                    height: 10.0,
                                  ),
                                  Center(
                                    child: Container(
                                      margin: EdgeInsets.only(
                                          left: 20, right: 20, top: 120),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: <Widget>[
                                          Expanded(
                                            child: Container(
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(15.0)),
                                                color: Colors.white,
                                              ),
                                              child: TextFormField(
                                                style: TextStyle(
                                                  color: Colors.white,
                                                  fontWeight: FontWeight.w300,
                                                ),
                                                validator: (String value) {
                                                  if (value.isEmpty) {
                                                    return 'please add some value';
                                                  }
                                                },
                                                maxLines: 8,
                                                decoration: InputDecoration(
                                                  hintText:
                                                      "Your message starts here.....",
                                                  focusedBorder:
                                                      OutlineInputBorder(
                                                          borderSide: BorderSide(
                                                              color: Colors
                                                                  .transparent)),
                                                  hintStyle: TextStyle(
                                                      fontWeight:
                                                          FontWeight.w300,
                                                      color: Colors.black),
                                                  prefixIcon: Padding(
                                                    padding:
                                                        const EdgeInsetsDirectional
                                                            .only(top: 0),
                                                    child: Icon(Icons
                                                        .message), // myIcon is a 48px-wide widget.
                                                  ),
                                                  errorStyle: TextStyle(
                                                    color: Colors.yellowAccent,
                                                    fontSize: 15,
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                    height: 10.0,
                                  ),
                                  Center(
                                    child: Container(
                                      margin:
                                          EdgeInsets.only(right: 20, top: 290),
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.end,
                                        children: <Widget>[
                                          Container(
                                            width: 100,
                                            height: 56,
                                            decoration: BoxDecoration(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(12.0)),
                                              color: Colors.white,
                                            ),
                                            child: FlatButton(
                                              color: Colors.white,
                                              onPressed: () {
                                                if (_formkey.currentState
                                                    .validate()) {
                                                  _launchURL(
                                                      'd.m.javid95@gmail.com',
                                                      'Flutter Email Test',
                                                      'Hello Flutter');
                                                }
                                              },
                                              child: Text('Send mail'),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              )
                            ],
                          ),
                        ),
                      ),
                    ],
                  ))
                ],
              ),
            ),
            FlatButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Icon(
                Icons.arrow_back,
                color: Colors.white,
                size: 30,
              ),
//                color: Colors.transparent,
            ),
          ],
        ));
  }
}

_launchURL(String toMailId, String subject, String body) async {
  var url = 'mailto:$toMailId?subject=$subject&body=$body';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

我希望列表视图能够滚动,而无需在打开键时调整屏幕大小。

【问题讨论】:

  • 你有没有接受过任何答案,请保持SO精神。

标签: flutter dart flutter-layout flutter-listview


【解决方案1】:

原因是屏幕高度足以显示您的ListView 项目,设置resizeToAvoidBottomInset : false 并没有阻止您的列表运行。您可以通过在列表中添加更多项目来确认这一点(在我的情况下,我添加了您的 Container 的第二个副本)并且列表运行良好。

【讨论】:

  • 谢谢,是的,我确实通过添加多个字段进行了检查,但是隐藏在键盘后面的最后 3 或 4 个字段无论如何都可以解决这个问题。
  • 您可以通过使用MediaQuery添加一个与屏幕高度成比例的空Container来解决此问题
  • 但是当我没有点击文本字段时,该容器将在主屏幕上有空间,它看起来很尴尬不是吗?
  • 您的项目将正常显示,因为它们在它前面,只有滚动后才会注意到。
  • 就像我有 6 个横向文本字段,它们适合屏幕,无需滚动,但是当我点击文本字段时,我需要滚动。正如你所说,将空容器向下添加该空容器也允许在键盘关闭时滚动,这是我不想拥有的。请问有什么解决办法吗?
猜你喜欢
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
  • 2014-12-12
  • 1970-01-01
  • 2017-09-15
  • 2014-04-20
相关资源
最近更新 更多