我想创建一个GlobalMessageUtils类,无需传递构建上下文即可打开材料快餐栏或对话框。这个想法是,每当出现任何错误(无网络、错误请求等)时,我都可以打开小吃店并将消息转发给用户。是否有 global 的概念context?
我正在考虑让我的GlobalMessageUtils班级成为一个单例,接受一个构建context并在MaterialApp级别实例化它,但我还没有让它起作用。任何机构有任何想法?这在颤振中甚至是一个很好的模式吗?如果没有,你们如何处理全局级别的错误处理?
StatefulWidget我正在尝试使用以下函数来测试 a onPressed。
floatingActionButton: FloatingActionButton(
child: Icon(Icons.camera_alt),
onPressed: () async {
try {
final filename = widget.randomStringGenerator?.call() ?? '';
final path = join(
(await getTemporaryDirectory()).path,
'${filename}.png',
);
await widget.cameraController?.takePicture(path);
final file = File(path);
final image = await file.readAsBytes();
widget.onPhotoTaken(image);
await file.delete();
} catch (e) {
print(e);
}
},
),
Run Code Online (Sandbox Code Playgroud)
以及以下小部件测试:
void main() {
Directory directory;
group('PhotoWidget test', () {
setUp(() async {
// Create a temporary directory.
directory = await Directory.systemTemp.createTemp();
// Mock out the MethodChannel for the …Run Code Online (Sandbox Code Playgroud)