键盘处于活动状态时如何避免背景图像缩小?

时间:2018-09-29 21:53:33

标签: dart flutter

我想要一个带有文本输入的背景图像,但是我不知道应该使用哪个小部件来避免键盘处于活动状态时背景图像缩小。

在这里您可以找到问题的两个屏幕截图以及我的代码:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var backgroundImage = new BoxDecoration(
        image: new DecorationImage(
            image: new AssetImage('assets/forest.jpg'), fit: BoxFit.cover));

    return new MaterialApp(
        home: new Scaffold(
            body: new Stack(
      children: <Widget>[
        new Container(
          decoration: backgroundImage,
        ),
        new TextField()
      ],
    )));
  }
}

closed keyboard active keyboard

3 个答案:

答案 0 :(得分:3)

您可以使用resizeToAvoidBottomPadding中的属性Scaffold

Scaffold(
     resizeToAvoidBottomPadding: false,
     ...

答案 1 :(得分:0)

将容器放置在脚手架外部,并使用背景图像将脚手架作为容器的子代。

return Container(
  decoration: BoxDecoration(
    image: DecorationImage(
      image: AssetImage('assets/bg.png'),
      fit: BoxFit.cover,
    ),
  ),
  child: Scaffold(
    backgroundColor: Colors.transparent,
    body: ListView(
      children: <Widget>[
        TextFormField(
          decoration: InputDecoration(
            hintText: "Email",
          ),
        ),
      ],
    ),
  ),
);

答案 2 :(得分:0)

颤动是将UI保持在键盘上方的默认功能,因此,如果您设置package TEST; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import java.util.Arrays; public class Data { //function started public static void main(String[] args) throws Exception { //Get Files String doc ="MyFileNew"; String dump=""; int number =200; for (int i=1; i<=201; i++) { number ++; dump = doc+number; String fileName= "/root/MyFiles/" + dump + ".xml"; Document document = getDocument(fileName); ; FileWriter fw = null; BufferedWriter bw = null; PrintWriter pw = null; //Using Document Builder DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document doc1 = documentBuilder.parse(fileName); /*******Get attribute values using xpath******/ XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); try { fw = new FileWriter("/root/Results/" + dump + ".txt"); bw = new BufferedWriter(fw); pw = new PrintWriter(bw); //Printing Name tags pw.println( "Name"+ evaluateXPath(document, "/xml/item/item[@key='Name']/text()") ); //Counting Name tags XPathExpression expr1 = xpath.compile("count(/xml/item/item[@key='Name']/)"); Number result1 = (Number) expr1.evaluate(doc1, XPathConstants.NUMBER); int n = result1.intValue(); //Printing FamilyName tags pw.println( "FamilyName: " + evaluateXPath(document, "/xml/item/item[@key='FamilyName']/text() \n") ); //Printing Age tags pw.println( "Age: " + evaluateXPath(document, "/xml/item/item[@key='Age']/text() \n") ); //Repeating Date based on counting name tags String[] strArray = new String[0]; for (int q=0; q<n;q++){ List<String> strArraytmp = evaluateXPath(document,"/xml/item/item[@key='date']/text()"); String[] strings = strArraytmp.stream().toArray(String[]::new); strArray= Stream.of(strArray, strings ).flatMap(Stream::of).toArray(String[]::new); } pw.println("date: " + Arrays.toString(strArray)); System.out.println("this file goes to path:" + "/root/Results/Data/" + dump + ".txt"); pw.flush(); } catch (IOException e) { e.printStackTrace(); } } } private static List<String> evaluateXPath(Document document, String xpathExpression) throws Exception { // Create XPathFactory object XPathFactory xpathFactory = XPathFactory.newInstance(); // Create XPath object XPath xpath = xpathFactory.newXPath(); List<String> values = new ArrayList<>(); try { // Create XPathExpression object XPathExpression expr = xpath.compile(xpathExpression); // Evaluate expression result on XML document NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { values.add(nodes.item(i).getNodeValue()); } } catch (XPathExpressionException e) { e.printStackTrace(); } return values; } private static Document getDocument(String fileName) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(fileName); return doc; } } ,则会失去将UI组件保持在键盘上方的功能

在应用程序中具有背景图像,而不是缩小背景图像 更好的方法是这样

resizeToAvoidBottomInset : false

必要设置脚手架backgroundColor:Colors.transparent 否则,您将看不到背景图片