From 4f9b19e25a03c0b7a36cb9575f371b0c54070f50 Mon Sep 17 00:00:00 2001 From: Alexandre Ardhuin Date: Wed, 14 Sep 2022 20:14:47 +0200 Subject: [PATCH] fix for upcoming use_build_context_synchronously changes (#111539) --- .../flutter_gallery/lib/gallery/demo.dart | 27 +++++++++++-------- .../flutter_gallery/lib/gallery/updater.dart | 8 +++--- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart b/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart index c01ac5d157..fe29b67dc0 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/demo.dart @@ -41,7 +41,7 @@ class ComponentDemoTabData { int get hashCode => Object.hash(tabName, description, documentationUrl); } -class TabbedComponentDemoScaffold extends StatelessWidget { +class TabbedComponentDemoScaffold extends StatefulWidget { const TabbedComponentDemoScaffold({ super.key, this.title, @@ -57,8 +57,13 @@ class TabbedComponentDemoScaffold extends StatelessWidget { final bool isScrollable; final bool showExampleCodeAction; + @override + State createState() => _TabbedComponentDemoScaffoldState(); +} + +class _TabbedComponentDemoScaffoldState extends State { void _showExampleCode(BuildContext context) { - final String? tag = demos![DefaultTabController.of(context)!.index].exampleCodeTag; + final String? tag = widget.demos![DefaultTabController.of(context)!.index].exampleCodeTag; if (tag != null) { Navigator.push(context, MaterialPageRoute( builder: (BuildContext context) => FullScreenCodeDialog(exampleCodeTag: tag) @@ -67,7 +72,7 @@ class TabbedComponentDemoScaffold extends StatelessWidget { } Future _showApiDocumentation(BuildContext context) async { - final String? url = demos![DefaultTabController.of(context)!.index].documentationUrl; + final String? url = widget.demos![DefaultTabController.of(context)!.index].documentationUrl; if (url == null) { return; } @@ -75,7 +80,7 @@ class TabbedComponentDemoScaffold extends StatelessWidget { final Uri uri = Uri.parse(url); if (await canLaunchUrl(uri)) { await launchUrl(uri); - } else { + } else if (mounted) { showDialog( context: context, builder: (BuildContext context) { @@ -96,12 +101,12 @@ class TabbedComponentDemoScaffold extends StatelessWidget { @override Widget build(BuildContext context) { return DefaultTabController( - length: demos!.length, + length: widget.demos!.length, child: Scaffold( appBar: AppBar( - title: Text(title!), + title: Text(widget.title!), actions: [ - ...?actions, + ...?widget.actions, Builder( builder: (BuildContext context) { return IconButton( @@ -110,7 +115,7 @@ class TabbedComponentDemoScaffold extends StatelessWidget { ); }, ), - if (showExampleCodeAction) + if (widget.showExampleCodeAction) Builder( builder: (BuildContext context) { return IconButton( @@ -122,12 +127,12 @@ class TabbedComponentDemoScaffold extends StatelessWidget { ), ], bottom: TabBar( - isScrollable: isScrollable, - tabs: demos!.map((ComponentDemoTabData data) => Tab(text: data.tabName)).toList(), + isScrollable: widget.isScrollable, + tabs: widget.demos!.map((ComponentDemoTabData data) => Tab(text: data.tabName)).toList(), ), ), body: TabBarView( - children: demos!.map((ComponentDemoTabData demo) { + children: widget.demos!.map((ComponentDemoTabData demo) { return SafeArea( top: false, bottom: false, diff --git a/dev/integration_tests/flutter_gallery/lib/gallery/updater.dart b/dev/integration_tests/flutter_gallery/lib/gallery/updater.dart index b9cb37c3f7..57175d98a3 100644 --- a/dev/integration_tests/flutter_gallery/lib/gallery/updater.dart +++ b/dev/integration_tests/flutter_gallery/lib/gallery/updater.dart @@ -35,9 +35,11 @@ class UpdaterState extends State { _lastUpdateCheck = DateTime.now(); final String? updateUrl = await widget.updateUrlFetcher(); - final bool? wantsUpdate = await showDialog(context: context, builder: _buildDialog); - if (wantsUpdate != null && updateUrl != null && wantsUpdate) { - launchUrl(Uri.parse(updateUrl)); + if (mounted) { + final bool? wantsUpdate = await showDialog(context: context, builder: _buildDialog); + if (wantsUpdate != null && updateUrl != null && wantsUpdate) { + launchUrl(Uri.parse(updateUrl)); + } } }