showDialog之后键盘关闭

时间:2020-01-13 09:28:26

标签: flutter flutter-layout

执行功能 showDialog 后,键盘关闭。 但是,我希望键盘保持打开状态,并且对话框在键盘上方

这是我的下面的代码。

PS:在this post中,它表明至少应该以某种方式与Flutter配合使用。

import 'package:flutter/material.dart';

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

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

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(controller: controller),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // keyboard closes after I press on this button...
          showDialog(
            context: context,
            child: AlertDialog(
              content: SingleChildScrollView(
                child: Text(
                  'How Would You Rate Our App?',
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                  textAlign: TextAlign.center,
                ),
              ),
            ),
          );
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:-1)

请尝试一下...

import 'package:flutter/material.dart';

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

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

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

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

class _MyHomePageState extends State<MyHomePage> {
final controller = TextEditingController();
FocusNode _focusNode = FocusNode();

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text(widget.title),
    ),
    body: Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          TextField(
            focusNode: _focusNode,
            controller: controller,
          autofocus: true,),
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: () {
        // keyboard closes after I press on this button...
        showDialog(
          context: context,
          child: AlertDialog(
            content: SingleChildScrollView(
              child: Text(
                'How Would You Rate Our App?',
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
            ),
          ),
        );
      },
      tooltip: 'Increment',
      child: Icon(Icons.add),
    ),
  );
 }
}