gpt4 book ai didi

flutter - showDialog 后键盘关闭

转载 作者:行者123 更新时间:2023-12-05 08:06:39 29 4
gpt4 key购买 nike

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

下面是我的代码。

附言:在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),
),
);
}
}

最佳答案

请试试这个...

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),
),
);
}
}

关于flutter - showDialog 后键盘关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59713730/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com