Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Defer load demos #423

Merged
merged 34 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9bd8db2
Defer load study examples
ferhatb Feb 4, 2021
b03acd3
defer cupertino demos
ferhatb Feb 4, 2021
221dadb
defer material demos
ferhatb Feb 4, 2021
48b0aa7
Defer demos
ferhatb Feb 5, 2021
11a95bf
address reviewer comment/StarterApp
ferhatb Feb 5, 2021
485a4e8
move DeferredWidget to new file, update copyright on routes.dart files
ferhatb Feb 5, 2021
0e3caba
address reviewer comments
ferhatb Feb 5, 2021
23fd5b5
Add copyright
ferhatb Feb 5, 2021
3e3cab9
Fix analyzer errors
ferhatb Feb 5, 2021
57a8ece
update screenshots
ferhatb Feb 5, 2021
e7365c9
dartfmt
ferhatb Feb 5, 2021
b1c4a2f
revert defer Reply/StartApp until FakeAsync loadlibrary is resolved
ferhatb Feb 5, 2021
4669964
Fix libraries for DeferredWidgets. Undefer Reply until restoration_te…
ferhatb Feb 8, 2021
39695c0
update DeferredWidget for local lint setting (removing type)
ferhatb Feb 8, 2021
a27e8d5
update golden screenshots
ferhatb Feb 8, 2021
4ef8758
dartfmt
ferhatb Feb 8, 2021
cb6b06a
update goldens
ferhatb Feb 8, 2021
1bb969d
remove zone experiment
ferhatb Feb 8, 2021
5f108d8
update goldens
ferhatb Feb 8, 2021
100e889
update web_benchmark dep to 0.0.3
ferhatb Feb 8, 2021
9bdce0d
Add shrine routes copyright
ferhatb Feb 8, 2021
d9daaec
await loading of material demos in restoration_test
ferhatb Feb 8, 2021
0d3514a
Fix unused import false positive. skip restoration_test case
ferhatb Feb 8, 2021
027c25d
await deferred libraries in screenshot test
ferhatb Feb 9, 2021
25d6327
merge
ferhatb Feb 9, 2021
ec89b56
Fix deferred widget state
ferhatb Feb 9, 2021
d02366d
update demo_test to wait for defer load
ferhatb Feb 9, 2021
422e6c5
remove unused import
ferhatb Feb 9, 2021
50b8c37
dartfmt
ferhatb Feb 9, 2021
5b25ae3
Implement pumpDeferredLibraries
ferhatb Feb 9, 2021
a2f2e14
pumpDeferredLibraries in benchmarks
ferhatb Feb 9, 2021
66ccaa3
Update code_segments
ferhatb Feb 9, 2021
52a6681
Remove unused runAsync
ferhatb Feb 9, 2021
be3d3a9
Update route links to use routes.dart
ferhatb Feb 9, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions golden_test/demo_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void main() {
group('mobile', () {
testWidgets('demo page matches golden screenshot', (tester) async {
await setUpBinding(tester);
await tester.runAsync<void>(pumpDeferredLibraries);
await pumpWidgetWithImages(
tester,
const GalleryApp(initialRoute: demoBannerRoute),
Expand All @@ -33,6 +34,7 @@ void main() {

testWidgets('dark demo page matches golden screenshot', (tester) async {
await setUpBinding(tester, brightness: Brightness.dark);
await tester.runAsync<void>(pumpDeferredLibraries);
await pumpWidgetWithImages(
tester,
const GalleryApp(initialRoute: demoBannerRoute),
Expand All @@ -50,6 +52,7 @@ void main() {
group('desktop', () {
testWidgets('demo page matches golden screenshot', (tester) async {
await setUpBinding(tester, size: desktopSize);
await tester.runAsync<void>(pumpDeferredLibraries);
await pumpWidgetWithImages(
tester,
const GalleryApp(initialRoute: demoBannerRoute),
Expand All @@ -69,6 +72,7 @@ void main() {
size: desktopSize,
brightness: Brightness.dark,
);
await tester.runAsync<void>(pumpDeferredLibraries);
await pumpWidgetWithImages(
tester,
const GalleryApp(initialRoute: demoBannerRoute),
Expand Down
345 changes: 251 additions & 94 deletions lib/codeviewer/code_segments.dart

Large diffs are not rendered by default.

495 changes: 351 additions & 144 deletions lib/data/demos.dart

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions lib/deferred_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2019 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'package:flutter/material.dart';

typedef LibraryLoader = Future<void> Function();
typedef DeferredWidgetBuilder = Widget Function();

/// Wraps the child inside a deferred module loader.
///
/// The child is created and a single instance of the Widget is maintained in
/// state as long as closure to create widget stays the same.
///
class DeferredWidget extends StatefulWidget {
DeferredWidget(this.libraryLoader, this.createWidget, {Key key})
: super(key: key);

final LibraryLoader libraryLoader;
final DeferredWidgetBuilder createWidget;
static final Map<LibraryLoader, Future<void>> _moduleLoaders = {};
static final Set<LibraryLoader> _loadedModules = {};

static Future<void> preload(LibraryLoader loader) {
if (!_moduleLoaders.containsKey(loader)) {
_moduleLoaders[loader] = loader().then((dynamic _) {
_loadedModules.add(loader);
});
}
return _moduleLoaders[loader];
}

@override
_DeferredWidgetState createState() => _DeferredWidgetState();
}

class _DeferredWidgetState extends State<DeferredWidget> {
_DeferredWidgetState();
Widget _loadedChild;
DeferredWidgetBuilder _loadedCreator;

@override
void initState() {
/// If module was already loaded immediately create widget instead of
/// waiting for future or zone turn.
if (DeferredWidget._loadedModules.contains(widget.libraryLoader)) {
_onLibraryLoaded();
} else {
DeferredWidget.preload(widget.libraryLoader)
.then((dynamic _) => _onLibraryLoaded());
}
super.initState();
}

void _onLibraryLoaded() {
setState(() {
_loadedCreator = widget.createWidget;
_loadedChild = _loadedCreator();
});
}

@override
Widget build(BuildContext context) {
/// If closure to create widget changed, create new instance, otherwise
/// treat as const Widget.
if (_loadedCreator != widget.createWidget && _loadedCreator != null) {
_loadedCreator = widget.createWidget;
_loadedChild = _loadedCreator();
}
return _loadedChild ?? Container();
}
}
9 changes: 1 addition & 8 deletions lib/demos/cupertino/cupertino_alert_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ import 'package:flutter/cupertino.dart';

import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'package:gallery/data/gallery_options.dart';
import 'package:gallery/demos/cupertino/demo_types.dart';

// BEGIN cupertinoAlertDemo

enum AlertDemoType {
alert,
alertTitle,
alertButtons,
alertButtonsOnly,
actionSheet,
}

class CupertinoAlertDemo extends StatefulWidget {
const CupertinoAlertDemo({
Key key,
Expand Down
12 changes: 12 additions & 0 deletions lib/demos/cupertino/cupertino_demos.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export 'package:gallery/demos/cupertino/cupertino_activity_indicator_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_alert_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_button_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_context_menu_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_navigation_bar_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_picker_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_refresh_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_segmented_control_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_slider_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_switch_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_tab_bar_demo.dart';
export 'package:gallery/demos/cupertino/cupertino_text_field_demo.dart';
7 changes: 7 additions & 0 deletions lib/demos/cupertino/demo_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum AlertDemoType {
alert,
alertTitle,
alertButtons,
alertButtonsOnly,
actionSheet,
}
6 changes: 1 addition & 5 deletions lib/demos/material/bottom_navigation_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import 'package:flutter/material.dart';
import 'package:animations/animations.dart';

import 'package:gallery/demos/material/material_demo_types.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

// BEGIN bottomNavigationDemo

enum BottomNavigationDemoType {
withLabels,
withoutLabels,
}

class BottomNavigationDemo extends StatefulWidget {
const BottomNavigationDemo({
Key key,
Expand Down
6 changes: 1 addition & 5 deletions lib/demos/material/bottom_sheet_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

enum BottomSheetDemoType {
persistent,
modal,
}
import 'package:gallery/demos/material/material_demo_types.dart';

class BottomSheetDemo extends StatelessWidget {
const BottomSheetDemo({
Expand Down
9 changes: 1 addition & 8 deletions lib/demos/material/button_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

enum ButtonDemoType {
text,
elevated,
outlined,
toggle,
floating,
}
import 'package:gallery/demos/material/material_demo_types.dart';

class ButtonDemo extends StatelessWidget {
const ButtonDemo({Key key, this.type}) : super(key: key);
Expand Down
8 changes: 1 addition & 7 deletions lib/demos/material/chip_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

enum ChipDemoType {
action,
choice,
filter,
input,
}
import 'package:gallery/demos/material/material_demo_types.dart';

class ChipDemo extends StatelessWidget {
const ChipDemo({
Expand Down
8 changes: 1 addition & 7 deletions lib/demos/material/dialog_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
import 'package:flutter/material.dart';

import 'package:gallery/data/gallery_options.dart';
import 'package:gallery/demos/material/material_demo_types.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

// BEGIN dialogDemo

enum DialogDemoType {
alert,
alertTitle,
simple,
fullscreen,
}

class DialogDemo extends StatelessWidget {
DialogDemo({Key key, @required this.type}) : super(key: key);

Expand Down
6 changes: 1 addition & 5 deletions lib/demos/material/divider_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:gallery/demos/material/material_demo_types.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

enum DividerDemoType {
horizontal,
vertical,
}

class DividerDemo extends StatelessWidget {
const DividerDemo({Key key, this.type}) : super(key: key);

Expand Down
7 changes: 1 addition & 6 deletions lib/demos/material/grid_list_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'package:gallery/demos/material/material_demo_types.dart';

// BEGIN gridListsDemo

enum GridListDemoType {
imageOnly,
header,
footer,
}

class GridListDemo extends StatelessWidget {
const GridListDemo({Key key, this.type}) : super(key: key);

Expand Down
6 changes: 1 addition & 5 deletions lib/demos/material/list_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'package:gallery/demos/material/material_demo_types.dart';

// BEGIN listDemo

enum ListDemoType {
oneLine,
twoLine,
}

class ListDemo extends StatelessWidget {
const ListDemo({Key key, this.type}) : super(key: key);

Expand Down
82 changes: 82 additions & 0 deletions lib/demos/material/material_demo_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
enum BottomNavigationDemoType {
withLabels,
withoutLabels,
}

enum BottomSheetDemoType {
persistent,
modal,
}

enum ButtonDemoType {
text,
elevated,
outlined,
toggle,
floating,
}

enum ChipDemoType {
action,
choice,
filter,
input,
}

enum DialogDemoType {
alert,
alertTitle,
simple,
fullscreen,
}

enum GridListDemoType {
imageOnly,
header,
footer,
}

enum ListDemoType {
oneLine,
twoLine,
}

enum MenuDemoType {
contextMenu,
sectionedMenu,
simpleMenu,
checklistMenu,
}

enum PickerDemoType {
date,
time,
range,
}

enum ProgressIndicatorDemoType {
circular,
linear,
}

enum SelectionControlsDemoType {
checkbox,
radio,
switches,
}

enum SlidersDemoType {
sliders,
rangeSliders,
customSliders,
}

enum TabsDemoType {
scrollable,
nonScrollable,
}

enum DividerDemoType {
horizontal,
vertical,
}
24 changes: 24 additions & 0 deletions lib/demos/material/material_demos.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export 'package:gallery/demos/material/banner_demo.dart';
export 'package:gallery/demos/material/bottom_app_bar_demo.dart';
export 'package:gallery/demos/material/bottom_navigation_demo.dart';
export 'package:gallery/demos/material/bottom_sheet_demo.dart';
export 'package:gallery/demos/material/button_demo.dart';
export 'package:gallery/demos/material/cards_demo.dart';
export 'package:gallery/demos/material/chip_demo.dart';
export 'package:gallery/demos/material/data_table_demo.dart';
export 'package:gallery/demos/material/dialog_demo.dart';
export 'package:gallery/demos/material/divider_demo.dart';
export 'package:gallery/demos/material/grid_list_demo.dart';
export 'package:gallery/demos/material/list_demo.dart';
export 'package:gallery/demos/material/menu_demo.dart';
export 'package:gallery/demos/material/navigation_drawer.dart';
export 'package:gallery/demos/material/navigation_rail_demo.dart';
export 'package:gallery/demos/material/picker_demo.dart';
export 'package:gallery/demos/material/progress_indicator_demo.dart';
export 'package:gallery/demos/material/selection_controls_demo.dart';
export 'package:gallery/demos/material/sliders_demo.dart';
export 'package:gallery/demos/material/snackbar_demo.dart';
export 'package:gallery/demos/material/tabs_demo.dart';
export 'package:gallery/demos/material/text_field_demo.dart';
export 'package:gallery/demos/material/tooltip_demo.dart';
export 'package:gallery/demos/material/app_bar_demo.dart';
8 changes: 1 addition & 7 deletions lib/demos/material/menu_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

enum MenuDemoType {
contextMenu,
sectionedMenu,
simpleMenu,
checklistMenu,
}
import 'package:gallery/demos/material/material_demo_types.dart';

enum SimpleValue {
one,
Expand Down
Loading