单击文本字段后如何强制屏幕向下滚动?

时间:2019-06-27 14:06:54

标签: flutter

我有此代码,当我单击文本字段时,它确实出现在键盘上方的屏幕中间,但它与该位置紧密配合,并且下面的文本不出现。我如何才能做到这一点,以便在单击文本字段时滚动也足以在其下方显示文本?

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

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

class _MyHomePageState extends State<MyHomePage> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              SizedBox(height: 600,),
              TextField(
              ),
              SizedBox(height: 30,),
              Text("I want this text to appear ")
            ],
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

enter image description here

没有确切的解决方案,但是您可以使用ScrollController滚动SingleChildScrollView中的其余区域

ScrollController _scrollController = ScrollController();

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SingleChildScrollView(
      controller: _scrollController,
      child: Column(
        children: <Widget>[
          SizedBox(height: 600),
          TextField(
            onTap: () {
              Timer(Duration(milliseconds: 200), () {
                _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
              });
            },
          ),
          SizedBox(height: 30),
          Text("I want this text to appear "),
        ],
      ),
    ),
  );
}.