From 92e3436fe7b06de25375e4064ad291f4db9f9e0d Mon Sep 17 00:00:00 2001 From: Abhishek Ghaskata Date: Thu, 10 Jun 2021 03:59:04 +0530 Subject: [PATCH] migrate complex layout to null safety (#83894) --- dev/benchmarks/complex_layout/lib/main.dart | 70 +++++++++---------- dev/benchmarks/complex_layout/pubspec.yaml | 2 +- .../test/measure_scroll_smoothness.dart | 15 ++-- .../measure_scroll_smoothness_test.dart | 2 +- .../test_driver/scroll_perf_test.dart | 2 +- .../test_driver/semantics_perf_test.dart | 10 +-- .../test_memory/scroll_perf.dart | 6 +- 7 files changed, 52 insertions(+), 55 deletions(-) diff --git a/dev/benchmarks/complex_layout/lib/main.dart b/dev/benchmarks/complex_layout/lib/main.dart index 0f1fcdbc8c..ab5249fc64 100644 --- a/dev/benchmarks/complex_layout/lib/main.dart +++ b/dev/benchmarks/complex_layout/lib/main.dart @@ -14,12 +14,12 @@ void main() { enum ScrollMode { complex, tile } class ComplexLayoutApp extends StatefulWidget { - const ComplexLayoutApp({Key key}) : super(key: key); + const ComplexLayoutApp({Key? key}) : super(key: key); @override ComplexLayoutAppState createState() => ComplexLayoutAppState(); - static ComplexLayoutAppState of(BuildContext context) => context.findAncestorStateOfType(); + static ComplexLayoutAppState? of(BuildContext context) => context.findAncestorStateOfType(); } class ComplexLayoutAppState extends State { @@ -55,7 +55,7 @@ class ComplexLayoutAppState extends State { } class TileScrollLayout extends StatelessWidget { - const TileScrollLayout({ Key key }) : super(key: key); + const TileScrollLayout({ Key? key }) : super(key: key); @override Widget build(BuildContext context) { @@ -81,12 +81,12 @@ class TileScrollLayout extends StatelessWidget { } class ComplexLayout extends StatefulWidget { - const ComplexLayout({ Key key }) : super(key: key); + const ComplexLayout({ Key? key }) : super(key: key); @override ComplexLayoutState createState() => ComplexLayoutState(); - static ComplexLayoutState of(BuildContext context) => context.findAncestorStateOfType(); + static ComplexLayoutState? of(BuildContext context) => context.findAncestorStateOfType(); } class ComplexLayoutState extends State { @@ -129,7 +129,7 @@ class ComplexLayoutState extends State { } class TopBarMenu extends StatelessWidget { - const TopBarMenu({Key key}) : super(key: key); + const TopBarMenu({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -182,7 +182,7 @@ class TopBarMenu extends StatelessWidget { } class MenuItemWithIcon extends StatelessWidget { - const MenuItemWithIcon(this.icon, this.title, this.subtitle, {Key key}) : super(key: key); + const MenuItemWithIcon(this.icon, this.title, this.subtitle, {Key? key}) : super(key: key); final IconData icon; final String title; @@ -204,7 +204,7 @@ class MenuItemWithIcon extends StatelessWidget { } class FancyImageItem extends StatelessWidget { - const FancyImageItem(this.index, {Key key}) : super(key: key); + const FancyImageItem(this.index, {Key? key}) : super(key: key); final int index; @@ -228,7 +228,7 @@ class FancyImageItem extends StatelessWidget { } class FancyGalleryItem extends StatelessWidget { - const FancyGalleryItem(this.index, {Key key}) : super(key: key); + const FancyGalleryItem(this.index, {Key? key}) : super(key: key); final int index; @override @@ -250,7 +250,7 @@ class FancyGalleryItem extends StatelessWidget { } class InfoBar extends StatelessWidget { - const InfoBar({Key key}) : super(key: key); + const InfoBar({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -268,7 +268,7 @@ class InfoBar extends StatelessWidget { } class IconBar extends StatelessWidget { - const IconBar({Key key}) : super(key: key); + const IconBar({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -287,7 +287,7 @@ class IconBar extends StatelessWidget { } class IconWithText extends StatelessWidget { - const IconWithText(this.icon, this.title, {Key key}) : super(key: key); + const IconWithText(this.icon, this.title, {Key? key}) : super(key: key); final IconData icon; final String title; @@ -308,7 +308,7 @@ class IconWithText extends StatelessWidget { } class MiniIconWithText extends StatelessWidget { - const MiniIconWithText(this.icon, this.title, {Key key}) : super(key: key); + const MiniIconWithText(this.icon, this.title, {Key? key}) : super(key: key); final IconData icon; final String title; @@ -337,7 +337,7 @@ class MiniIconWithText extends StatelessWidget { } class FatDivider extends StatelessWidget { - const FatDivider({Key key}) : super(key: key); + const FatDivider({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -349,7 +349,7 @@ class FatDivider extends StatelessWidget { } class UserHeader extends StatelessWidget { - const UserHeader(this.userName, {Key key}) : super(key: key); + const UserHeader(this.userName, {Key? key}) : super(key: key); final String userName; @@ -384,7 +384,7 @@ class UserHeader extends StatelessWidget { Row( children: [ Text('Yesterday at 11:55 • ', style: Theme.of(context).textTheme.caption), - Icon(Icons.people, size: 16.0, color: Theme.of(context).textTheme.caption.color), + Icon(Icons.people, size: 16.0, color: Theme.of(context).textTheme.caption!.color), ], ), ], @@ -398,7 +398,7 @@ class UserHeader extends StatelessWidget { } class ItemDescription extends StatelessWidget { - const ItemDescription({Key key}) : super(key: key); + const ItemDescription({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -410,7 +410,7 @@ class ItemDescription extends StatelessWidget { } class ItemImageBox extends StatelessWidget { - const ItemImageBox({Key key}) : super(key: key); + const ItemImageBox({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -491,7 +491,7 @@ class ItemImageBox extends StatelessWidget { } class ItemGalleryBox extends StatelessWidget { - const ItemGalleryBox(this.index, {Key key}) : super(key: key); + const ItemGalleryBox(this.index, {Key? key}) : super(key: key); final int index; @@ -521,7 +521,7 @@ class ItemGalleryBox extends StatelessWidget { child: Container( color: Theme.of(context).primaryColor, child: Center( - child: Text(tabName, style: Theme.of(context).textTheme.headline5.copyWith(color: Colors.white)), + child: Text(tabName, style: Theme.of(context).textTheme.headline5!.copyWith(color: Colors.white)), ), ), ), @@ -560,7 +560,7 @@ class ItemGalleryBox extends StatelessWidget { } class BottomBar extends StatelessWidget { - const BottomBar({Key key}) : super(key: key); + const BottomBar({Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -588,7 +588,7 @@ class BottomBar extends StatelessWidget { } class BottomBarButton extends StatelessWidget { - const BottomBarButton(this.icon, this.title, {Key key}) : super(key: key); + const BottomBarButton(this.icon, this.title, {Key? key}) : super(key: key); final IconData icon; final String title; @@ -611,19 +611,19 @@ class BottomBarButton extends StatelessWidget { } class GalleryDrawer extends StatelessWidget { - const GalleryDrawer({ Key key }) : super(key: key); + const GalleryDrawer({ Key? key }) : super(key: key); void _changeTheme(BuildContext context, bool value) { - ComplexLayoutApp.of(context).lightTheme = value; + ComplexLayoutApp.of(context)?.lightTheme = value; } void _changeScrollMode(BuildContext context, ScrollMode mode) { - ComplexLayoutApp.of(context).scrollMode = mode; + ComplexLayoutApp.of(context)?.scrollMode = mode; } @override Widget build(BuildContext context) { - final ScrollMode currentMode = ComplexLayoutApp.of(context).scrollMode; + final ScrollMode currentMode = ComplexLayoutApp.of(context)!.scrollMode; return Drawer( // Note: for real apps, see the Gallery material Drawer demo. More // typically, a drawer would have a fixed header with a scrolling body @@ -646,22 +646,22 @@ class GalleryDrawer extends StatelessWidget { leading: const Icon(Icons.brightness_5), title: const Text('Light'), onTap: () { _changeTheme(context, true); }, - selected: ComplexLayoutApp.of(context).lightTheme, + selected: ComplexLayoutApp.of(context)!.lightTheme, trailing: Radio( value: true, - groupValue: ComplexLayoutApp.of(context).lightTheme, - onChanged: (bool value) { _changeTheme(context, value); }, + groupValue: ComplexLayoutApp.of(context)!.lightTheme, + onChanged: (bool? value) { _changeTheme(context, value!); }, ), ), ListTile( leading: const Icon(Icons.brightness_7), title: const Text('Dark'), onTap: () { _changeTheme(context, false); }, - selected: !ComplexLayoutApp.of(context).lightTheme, + selected: !ComplexLayoutApp.of(context)!.lightTheme, trailing: Radio( value: false, - groupValue: ComplexLayoutApp.of(context).lightTheme, - onChanged: (bool value) { _changeTheme(context, value); }, + groupValue: ComplexLayoutApp.of(context)!.lightTheme, + onChanged: (bool? value) { _changeTheme(context, value!); }, ), ), const Divider(), @@ -669,10 +669,10 @@ class GalleryDrawer extends StatelessWidget { leading: const Icon(Icons.hourglass_empty), title: const Text('Animate Slowly'), selected: timeDilation != 1.0, - onTap: () { ComplexLayoutApp.of(context).toggleAnimationSpeed(); }, + onTap: () { ComplexLayoutApp.of(context)!.toggleAnimationSpeed(); }, trailing: Checkbox( value: timeDilation != 1.0, - onChanged: (bool value) { ComplexLayoutApp.of(context).toggleAnimationSpeed(); }, + onChanged: (bool? value) { ComplexLayoutApp.of(context)!.toggleAnimationSpeed(); }, ), ), ], @@ -682,7 +682,7 @@ class GalleryDrawer extends StatelessWidget { } class FancyDrawerHeader extends StatelessWidget { - const FancyDrawerHeader({Key key}) : super(key: key); + const FancyDrawerHeader({Key? key}) : super(key: key); @override Widget build(BuildContext context) { diff --git a/dev/benchmarks/complex_layout/pubspec.yaml b/dev/benchmarks/complex_layout/pubspec.yaml index 31a2398abe..d50c9042ea 100644 --- a/dev/benchmarks/complex_layout/pubspec.yaml +++ b/dev/benchmarks/complex_layout/pubspec.yaml @@ -2,7 +2,7 @@ name: complex_layout description: A benchmark of a relatively complex layout. environment: - sdk: ">=2.2.2 <3.0.0" + sdk: ">=2.12.0 <3.0.0" dependencies: flutter: diff --git a/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart b/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart index a5f15ba67c..26ae2f2375 100644 --- a/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart +++ b/dev/benchmarks/complex_layout/test/measure_scroll_smoothness.dart @@ -66,7 +66,7 @@ class ResampleFlagVariant extends TestVariant { @override final Set values = Set.from(TestScenario.values); - TestScenario currentValue; + late TestScenario currentValue; bool get resample { switch(currentValue) { case TestScenario.resampleOn90Hz: @@ -76,7 +76,6 @@ class ResampleFlagVariant extends TestVariant { case TestScenario.resampleOff59Hz: return false; } - throw ArgumentError; } double get frequency { switch(currentValue) { @@ -87,10 +86,9 @@ class ResampleFlagVariant extends TestVariant { case TestScenario.resampleOff59Hz: return 59.0; } - throw ArgumentError; } - Map result; + Map? result; @override String describeValue(TestScenario value) { @@ -104,7 +102,6 @@ class ResampleFlagVariant extends TestVariant { case TestScenario.resampleOff59Hz: return 'resample off with 59Hz input'; } - throw ArgumentError; } @override @@ -118,7 +115,7 @@ class ResampleFlagVariant extends TestVariant { @override Future tearDown(TestScenario value, bool memento) async { binding.resamplingEnabled = memento; - binding.reportData[describeValue(value)] = result; + binding.reportData![describeValue(value)] = result; } } @@ -134,16 +131,16 @@ Future main() async { await tester.pumpAndSettle(); final Finder scrollerFinder = find.byKey(const ValueKey('complex-scroll')); final ListView scroller = tester.widget(scrollerFinder); - final ScrollController controller = scroller.controller; + final ScrollController? controller = scroller.controller; final List frameTimestamp = []; final List scrollOffset = []; final List delays = []; binding.addPersistentFrameCallback((Duration timeStamp) { - if (controller.hasClients) { + if (controller?.hasClients == true) { // This if is necessary because by the end of the test the widget tree // is destroyed. frameTimestamp.add(timeStamp.inMicroseconds); - scrollOffset.add(controller.offset); + scrollOffset.add(controller!.offset); } }); diff --git a/dev/benchmarks/complex_layout/test_driver/measure_scroll_smoothness_test.dart b/dev/benchmarks/complex_layout/test_driver/measure_scroll_smoothness_test.dart index 74abeb9525..9dd60ce3d9 100644 --- a/dev/benchmarks/complex_layout/test_driver/measure_scroll_smoothness_test.dart +++ b/dev/benchmarks/complex_layout/test_driver/measure_scroll_smoothness_test.dart @@ -8,7 +8,7 @@ import 'package:integration_test/integration_test_driver.dart' as driver; Future main() => driver.integrationDriver( timeout: const Duration(minutes: 5), - responseDataCallback: (Map data) async { + responseDataCallback: (Map? data) async { await driver.writeResponseData( data, testOutputFilename: 'scroll_smoothness_test', diff --git a/dev/benchmarks/complex_layout/test_driver/scroll_perf_test.dart b/dev/benchmarks/complex_layout/test_driver/scroll_perf_test.dart index 7c1d3b71b3..f7d5d1e1b2 100644 --- a/dev/benchmarks/complex_layout/test_driver/scroll_perf_test.dart +++ b/dev/benchmarks/complex_layout/test_driver/scroll_perf_test.dart @@ -9,7 +9,7 @@ import 'package:test/test.dart' hide TypeMatcher, isInstanceOf; void main() { group('scrolling performance test', () { - FlutterDriver driver; + late FlutterDriver driver; setUpAll(() async { driver = await FlutterDriver.connect(); diff --git a/dev/benchmarks/complex_layout/test_driver/semantics_perf_test.dart b/dev/benchmarks/complex_layout/test_driver/semantics_perf_test.dart index 1609b90a8a..cc7828c1b5 100644 --- a/dev/benchmarks/complex_layout/test_driver/semantics_perf_test.dart +++ b/dev/benchmarks/complex_layout/test_driver/semantics_perf_test.dart @@ -12,7 +12,7 @@ import 'package:test/test.dart' hide TypeMatcher, isInstanceOf; void main() { group('semantics performance test', () { - FlutterDriver driver; + late FlutterDriver driver; setUpAll(() async { driver = await FlutterDriver.connect(printCommunication: true); @@ -33,10 +33,10 @@ void main() { expect(await driver.setSemantics(true), isTrue); }); - final Iterable semanticsEvents = timeline.events.where((TimelineEvent event) => event.name == 'Semantics'); - if (semanticsEvents.length != 2) - fail('Expected exactly two semantics events, got ${semanticsEvents.length}'); - final Duration semanticsTreeCreation = Duration(microseconds: semanticsEvents.last.timestampMicros - semanticsEvents.first.timestampMicros); + final Iterable? semanticsEvents = timeline.events?.where((TimelineEvent event) => event.name == 'Semantics'); + if (semanticsEvents?.length != 2) + fail('Expected exactly two semantics events, got ${semanticsEvents?.length}'); + final Duration semanticsTreeCreation = Duration(microseconds: semanticsEvents!.last.timestampMicros! - semanticsEvents.first.timestampMicros!); final String jsonEncoded = json.encode({'initialSemanticsTreeCreation': semanticsTreeCreation.inMilliseconds}); File(p.join(testOutputsDirectory, 'complex_layout_semantics_perf.json')).writeAsStringSync(jsonEncoded); diff --git a/dev/benchmarks/complex_layout/test_memory/scroll_perf.dart b/dev/benchmarks/complex_layout/test_memory/scroll_perf.dart index b05397dace..47dff1ac12 100644 --- a/dev/benchmarks/complex_layout/test_memory/scroll_perf.dart +++ b/dev/benchmarks/complex_layout/test_memory/scroll_perf.dart @@ -32,7 +32,7 @@ Future main() async { child: ComplexLayoutApp(), ), )); - await SchedulerBinding.instance.endOfFrame; + await SchedulerBinding.instance?.endOfFrame; /// Wait 50ms to allow the raster thread to actually put up the frame. (The /// endOfFrame future ends when we send the data to the engine, before @@ -50,9 +50,9 @@ Future main() async { child: ComplexLayoutApp(), ), )); - await SchedulerBinding.instance.endOfFrame; + await SchedulerBinding.instance?.endOfFrame; - final WidgetController controller = LiveWidgetController(WidgetsBinding.instance); + final WidgetController controller = LiveWidgetController(WidgetsBinding.instance!); // Scroll down for (int iteration = 0; iteration < maxIterations; iteration += 1) {