Flutter - SingleChildScrollView 干扰列

raf*_*b21 15 dart flutter

我创建了一个与列配合良好的屏幕,但由于键盘的原因我需要滚动。

当您插入SingleChildScrollViewListView属性时MainAxisAlignment.spaceBetween,它不再起作用。

有什么解决办法吗?

没有SingleChildScrollView屏幕的Gif不会滚动并且FloatingActionButton是在屏幕底部

在此处输入图片说明

带有SingleChildScrollView屏幕滚动的Gif并且他FloatingActionButton不在屏幕底部

在此处输入图片说明

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(      
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(backgroundColor: new Color(0xFF26C6DA)),        
      body: new SingleChildScrollView(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              new Container(
                child: new Column(
                  children: <Widget>[
                    new TextField(
                      decoration: const InputDecoration(
                        labelText: "Description",
                      ),
                      style: Theme.of(context).textTheme.title,
                      ),
                    new TextField(
                      decoration: const InputDecoration(
                        labelText: "Description",
                      ),
                      style: Theme.of(context).textTheme.title,
                    ),
                    new TextField(
                      decoration: const InputDecoration(
                        labelText: "Description",
                      ),
                      style: Theme.of(context).textTheme.title,
                    ),
                  ],
                )
              ),
              new Container(
                margin: new EdgeInsets.only(bottom: 16.0),
                child: new FloatingActionButton(
                  backgroundColor: new Color(0xFFE57373),
                  child: new Icon(Icons.check),
                  onPressed: (){}
                ),                    
              )
            ],
        ),
      )            
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Col*_*son 6

我建议不要以FloatingActionButton您在这里的方式使用。FloatingActionButton应该用于始终需要始终显示在屏幕上的操作。有关可滚动的其他按钮类型(如和 )的建议,请参阅按钮使用材料指南。您可以在此处使用 a ,但我认为最好使您的屏幕成为对话框并将保存操作放入我在您的其他问题中建议的RaisedButtonFlatButtonRaisedButtonAppBar

如果您决定使用 aRaisedButtonFlatButton,请记住滚动项通常不会根据其容器的大小更改其项目间距。除了使用MainAxisAlignment.spaceBetween,您可以Padding在您周围放置一些RaisedButton以将其与TextField元素分开。这将确保无论旋转、屏幕大小如何,也无论键盘是否向上,它们的间距都相同。

  • 但是,当“Column”位于“SingleChildScrollView”内部时,为什么“MainAxisAlignment.spaceBetween”对“Column”内的子级不起作用?有没有其他方法可以实现这一目标? (3认同)

raf*_*b21 5

请按照以下代码进行注册。

MainAxisAlignment.spaceBetween 已替换为动态填充,具体取决于屏幕大小。

import 'package:flutter/material.dart';
import 'dart:ui' as ui;

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(      
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final ui.Size logicalSize = MediaQuery.of(context).size;
    final double _height = logicalSize.height;
    return new Scaffold(
      appBar: new AppBar(backgroundColor: new Color(0xFF26C6DA)),        
      body: new SingleChildScrollView(
        child: new Column(
          //mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              new Container(
                height: 300.0,
                child: new Column(
                  children: <Widget>[
                    new TextField(
                      decoration: const InputDecoration(
                        labelText: "Description",
                      ),
                      style: Theme.of(context).textTheme.title,
                    ),
                    new TextField(
                      decoration: const InputDecoration(
                        labelText: "Description",
                      ),
                      style: Theme.of(context).textTheme.title,
                    ),
                    new TextField(
                      decoration: const InputDecoration(
                        labelText: "Description",
                      ),
                      style: Theme.of(context).textTheme.title,
                    ),
                  ],
                )
              ),
              new Container(
                padding: new EdgeInsets.only(top: (_height - 450.0)),
                margin: new EdgeInsets.only(bottom: 16.0),
                child: new FloatingActionButton(
                  backgroundColor: new Color(0xFFE57373),
                  child: new Icon(Icons.check),
                  onPressed: (){}
                ),
              )
            ],
        ),
      )
    );
  }
}
Run Code Online (Sandbox Code Playgroud)