Considering that Navigator.of(context).push()
returns a Future<void>
, is it a good idea to be await
ing them inside of buttons?
IconButton(
icon: Icon(Icons.search),
onPressed: () => AutoRouter.of(context).push(
SearchPageRoute(),
),
)
// -->
IconButton(
icon: Icon(Icons.search),
onPressed: () async {
await Navigator.of(context).push(
SearchPageRoute(),
);
},
)
Would it make any difference on a practical level? I'm asking because I recently made a AsyncButtonBuilder
that handles the loading state and disabled state of buttons but am curious if Navigation operations would ever have the latency required to even show a loading indicator.
For example, is this next piece of code pointless considering Navigator is almost synchronous -- at least as it appears to me?
class Example extends StatefulWidget {
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
var isLoading = false;
@override
Widget build(BuildContext context) {
return IconButton(
icon: isLoading ? CircularProgressIndicator() : Icon(Icons.search),
onPressed: isLoading
? null
: () async {
setState(() {
isLoading = true;
});
await Navigator.of(context).push(SearchPageRoute());
if (mounted) {
setState(() {
isLoading = false;
});
}
},
);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…