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

Commit

Permalink
Migrate tools to null safety
Browse files Browse the repository at this point in the history
  • Loading branch information
amanv8060 committed Feb 20, 2022
1 parent 5da082d commit c450edb
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 97 deletions.
80 changes: 39 additions & 41 deletions lib/data/gallery_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'dart:async';

import 'package:flutter/material.dart';
Expand All @@ -29,30 +27,32 @@ const List<String> rtlLanguages = <String>[
// Fake locale to represent the system Locale option.
const systemLocaleOption = Locale('system');

Locale _deviceLocale;
Locale get deviceLocale => _deviceLocale;
set deviceLocale(Locale locale) {
Locale? _deviceLocale;

Locale? get deviceLocale => _deviceLocale;

set deviceLocale(Locale? locale) {
_deviceLocale ??= locale;
}

class GalleryOptions {
const GalleryOptions({
this.themeMode,
double textScaleFactor,
double? textScaleFactor,
this.customTextDirection,
Locale locale,
Locale? locale,
this.timeDilation,
this.platform,
this.isTestMode,
}) : _textScaleFactor = textScaleFactor,
this.isTestMode = false,
}) : _textScaleFactor = textScaleFactor ?? 1.0,
_locale = locale;

final ThemeMode themeMode;
final ThemeMode? themeMode;
final double _textScaleFactor;
final CustomTextDirection customTextDirection;
final Locale _locale;
final double timeDilation;
final TargetPlatform platform;
final CustomTextDirection? customTextDirection;
final Locale? _locale;
final double? timeDilation;
final TargetPlatform? platform;
final bool isTestMode; // True for integration tests.

// We use a sentinel value to indicate the system text scale option. By
Expand All @@ -68,15 +68,15 @@ class GalleryOptions {
}
}

Locale get locale => _locale ?? deviceLocale;
Locale? get locale => _locale ?? deviceLocale;

/// Returns a text direction based on the [CustomTextDirection] setting.
/// If it is based on locale and the locale cannot be determined, returns
/// null.
TextDirection resolvedTextDirection() {
TextDirection? resolvedTextDirection() {
switch (customTextDirection) {
case CustomTextDirection.localeBased:
final language = locale?.languageCode?.toLowerCase();
final language = locale?.languageCode.toLowerCase();
if (language == null) return null;
return rtlLanguages.contains(language)
? TextDirection.rtl
Expand Down Expand Up @@ -112,13 +112,13 @@ class GalleryOptions {
}

GalleryOptions copyWith({
ThemeMode themeMode,
double textScaleFactor,
CustomTextDirection customTextDirection,
Locale locale,
double timeDilation,
TargetPlatform platform,
bool isTestMode,
ThemeMode? themeMode,
double? textScaleFactor,
CustomTextDirection? customTextDirection,
Locale? locale,
double? timeDilation,
TargetPlatform? platform,
bool? isTestMode,
}) {
return GalleryOptions(
themeMode: themeMode ?? this.themeMode,
Expand Down Expand Up @@ -155,20 +155,20 @@ class GalleryOptions {

static GalleryOptions of(BuildContext context) {
final scope =
context.dependOnInheritedWidgetOfExactType<_ModelBindingScope>();
context.dependOnInheritedWidgetOfExactType<_ModelBindingScope>()!;
return scope.modelBindingState.currentModel;
}

static void update(BuildContext context, GalleryOptions newModel) {
final scope =
context.dependOnInheritedWidgetOfExactType<_ModelBindingScope>();
context.dependOnInheritedWidgetOfExactType<_ModelBindingScope>()!;
scope.modelBindingState.updateModel(newModel);
}
}

// Applies text GalleryOptions to a widget
class ApplyTextOptions extends StatelessWidget {
const ApplyTextOptions({Key key, @required this.child}) : super(key: key);
const ApplyTextOptions({Key? key, required this.child}) : super(key: key);

final Widget child;

Expand Down Expand Up @@ -198,11 +198,10 @@ class ApplyTextOptions extends StatelessWidget {

class _ModelBindingScope extends InheritedWidget {
const _ModelBindingScope({
Key key,
@required this.modelBindingState,
Widget child,
}) : assert(modelBindingState != null),
super(key: key, child: child);
Key? key,
required this.modelBindingState,
required Widget child,
}) : super(key: key, child: child);

final _ModelBindingState modelBindingState;

Expand All @@ -212,11 +211,10 @@ class _ModelBindingScope extends InheritedWidget {

class ModelBinding extends StatefulWidget {
const ModelBinding({
Key key,
Key? key,
this.initialModel = const GalleryOptions(),
this.child,
}) : assert(initialModel != null),
super(key: key);
required this.child,
}) : super(key: key);

final GalleryOptions initialModel;
final Widget child;
Expand All @@ -226,8 +224,8 @@ class ModelBinding extends StatefulWidget {
}

class _ModelBindingState extends State<ModelBinding> {
GalleryOptions currentModel;
Timer _timeDilationTimer;
late GalleryOptions currentModel;
Timer? _timeDilationTimer;

@override
void initState() {
Expand All @@ -246,15 +244,15 @@ class _ModelBindingState extends State<ModelBinding> {
if (currentModel.timeDilation != newModel.timeDilation) {
_timeDilationTimer?.cancel();
_timeDilationTimer = null;
if (newModel.timeDilation > 1) {
if (newModel.timeDilation! > 1) {
// We delay the time dilation change long enough that the user can see
// that UI has started reacting and then we slam on the brakes so that
// they see that the time is in fact now dilated.
_timeDilationTimer = Timer(const Duration(milliseconds: 150), () {
timeDilation = newModel.timeDilation;
timeDilation = newModel.timeDilation!;
});
} else {
timeDilation = newModel.timeDilation;
timeDilation = newModel.timeDilation!;
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions lib/layout/text_scale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'dart:math';

import 'package:flutter/material.dart';
Expand Down
2 changes: 0 additions & 2 deletions tool/codeviewer_cli/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9

import 'dart:io';

import 'package:args/args.dart';
Expand Down
Loading

0 comments on commit c450edb

Please sign in to comment.