我是Flutter的新手,这个问题确实困扰着我,我已经在Internet上进行了搜索,但是没有一个结果令我满意:
我尝试使用软件包中的“进度对话框”:
import 'package:progress_dialog/progress_dialog.dart';
我的MyApp
文件中的类main.dart
像这样:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Declare and decorate my Progress Dialog.
ProgressDialog pr = ProgressDialog(
context,
type: ProgressDialogType.Normal,
isDismissible: false,
);
pr.style(
message: 'Fetching Something...',
borderRadius: 50.0,
elevation: 5.0,
);
// TODO: implement build method
return MaterialApp(
home: Scaffold(
body: RaisedButton.icon(
onPressed: () async {
pr.show();
await fetchData();
pr.hide();
},
icon: Icon(Icons.clear),
label: Text('Fetch Data'),
),
),
);
}
}
我的示例fetchData()
函数是这样的(当然,Firestore函数的打包和安装步骤已通过验证):
Future<void> fetchData() async {
// Just an example of really fetching something.
await Firestore.instance
.collection('users')
.document('0')
.delete();
}
我想要的是,每次单击按钮时,都会显示一个加载微调器,并在fetchData()
函数完成后立即隐藏该微调器。这会在第一次单击中产生正确的流程,但是,如果我第二次单击该按钮,则不会显示微调器(fetchData()
函数仍会正确执行)。并且在终端中显示警告(不是错误):
I/flutter (17942): Exception while showing the dialog
I/flutter (17942): Looking up a deactivated widget's ancestor is unsafe.
I/flutter (17942): At this point the state of the widget's element tree is no longer stable.
I/flutter (17942): 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.
dependOnInheritedWidgetOfExactType()
上的文档非常有限,难以理解。所以我仍然不知道如何正确解决这个问题。
我们非常感谢您的帮助。谢谢。
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
您可以使用await pr.show();
和await pr.hide();
代码段
onPressed: () async {
await pr.show();
await fetchData();
await pr.hide();
},
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:progress_dialog/progress_dialog.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
Future<void> fetchData() async {
await Future.delayed(Duration(seconds: 3), () {});
setState(() {});
}
@override
Widget build(BuildContext context) {
ProgressDialog pr = ProgressDialog(
context,
type: ProgressDialogType.Normal,
isDismissible: false,
);
pr.style(
message: 'Fetching Something...',
borderRadius: 50.0,
elevation: 5.0,
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton.icon(
onPressed: () async {
await pr.show();
await fetchData();
await pr.hide();
},
icon: Icon(Icons.clear),
label: Text('Fetch Data'),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}