颤振:弹出 2 个警报对话框时应用程序崩溃



如果快速单击两张卡,将出现黑屏,并且必须重新启动应用程序

颤振 2.0.5 • 通道稳定 • https://github.com/flutter/flutter.git 框架 • 修订版 adc687823a (2 周前) • 2021-04-16 09:40:20 -0700 发动机 • 修订版 b09f014e96 工具 • 飞镖 2.12.3

脚手架

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Column(
children: [
GestureDetector(
child: Container(
child: GFCard('The quick brown fox jumps over the lazy dog')),
onTap: Alerts.correct(context),
),
GestureDetector(
child: GFCard('The quick brown fox jumps over the lazy dog'),
onTap: Alerts.wrong(context),
),
],
),
));
}
}

警报

import 'package:flutter/material.dart';
import 'dart:async';
class Alerts {
static correct(BuildContext context) {
return () => _alert(context,
backgroundColor: Color(0xFF25C047), message: 'Correct   ');
}
static wrong(BuildContext context) {
return () => _alert(context,
backgroundColor: Color(0xFFC22121), message: 'Wrong :(');
}
static _alert(BuildContext context, {Color backgroundColor, String message}) {
return showDialog(
barrierColor: Colors.transparent,
context: context,
builder: (BuildContext context) {
Timer(Duration(milliseconds: 500), () => Navigator.of(context).pop());
return AlertDialog(
insetPadding: EdgeInsets.all(35),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16))),
content: Text(
message,
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
textAlign: TextAlign.center,
),
elevation: 24,
backgroundColor: backgroundColor,
// shape: RoundedRectangleBorder(),
);
},
);
}
}

错误

E/flutter ( 5535): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter ( 5535): At this point the state of the widget's element tree is no longer stable.
E/flutter ( 5535): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.```

要解决此问题,您应该在showDialog()中设置barrierDismissible: false

最新更新