按下按钮后会出现连续的警报对话框。当第二个对话框显示时,会出现一个简短的“FLT:RenderFlex 底部溢出了 46 个像素”。瞬间消失。这似乎仅在第二个对话框的垂直尺寸大于第一个对话框时才会发生。如果第二个对话框比第一个对话框小,则不会出现错误。
我认为我正确地弹出了第一个对话框。第一个对话框方法引用了一个 TextEditingController,它是父窗口小部件类的一部分,我相信我正确地清除了它。
我不明白为什么会出现这个简短的溢出错误。它立即消失。否则一切都会正常。我在 android 模拟器和调试控制台中看到它。这是一个颤振错误还是我做了一些愚蠢的事情。
基本上我就是这么做的
class _ParentWidgetState extends State<ParentWidget> {
late TextEditingController controller;
// init, dispose, build, blah blah...
ElevatedButton(
blahBlah: ...
onPressed: () async {
someText = await firstDialog();
await secondDialog(someText);
}
}
Future<String?> firstDialog() {
return showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Enter something'),
content: TextField(
decoration: const InputDecoration(
hintText: 'Enter something'),
autofocus: true,
controller: controller,
onSubmitted: (_) {
Navigator.of(context).pop(controller.text);
controller.clear();
}),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(controller.text);
controller.clear();
},
child: const Text('SUBMIT'))
],
),
);
}
Future<bool?> secondDialog(String someText) => showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(someText),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Blah blah blah'),
const SizedBox(
height: 8,
),
const Text('Blah blah blah'),
const SizedBox(
height: 8,
),
const Text('Blah blah blah'),
const SizedBox(
height: 8,
),
const Text('Blah blah blah'),
],
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: const Text('Continue')),
],
);
});
我做了一个例子,没有出现这个错误,也许这个例子可以帮助你。我没有收到你提到的错误。
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
TextEditingController controller = TextEditingController();
Future<String?> firstDialog() {
return showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Enter something'),
content: TextField(
decoration: const InputDecoration(hintText: 'Enter something'),
autofocus: true,
controller: controller,
onSubmitted: (_) {
Navigator.of(context).pop(controller.text);
controller.clear();
}),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(controller.text);
controller.clear();
},
child: const Text('SUBMIT'))
],
),
);
}
Future<bool?> secondDialog(String someText) => showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(someText),
content: const Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Blah blah blah'),
SizedBox(
height: 8,
),
Text('Blah blah blah'),
SizedBox(
height: 8,
),
Text('Blah blah blah'),
SizedBox(
height: 8,
),
Text('Blah blah blah'),
],
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop(true);
},
child: const Text('Continue')),
],
);
});
return Scaffold(
appBar: AppBar(
title: const Text('Material App Bar'),
),
body: Center(
child: TextButton(
onPressed: () async {
String someText = await firstDialog() ?? '';
await secondDialog(someText);
},
child: const Text("test dialog")),
),
);
}
}