From c8112c88acef9d6f172b680285e4abf195e9cba8 Mon Sep 17 00:00:00 2001 From: Brett Morgan Date: Tue, 8 Jun 2021 08:18:20 +1000 Subject: [PATCH] Add `flutter_lints` to `add_to_app/plugin/flutter_module_using_plugin` (#812) --- .../analysis_options.yaml | 19 ++++++++++ .../flutter_module_using_plugin/lib/cell.dart | 21 +++++----- .../flutter_module_using_plugin/lib/main.dart | 38 ++++++++++--------- .../flutter_module_using_plugin/pubspec.lock | 14 +++++++ .../flutter_module_using_plugin/pubspec.yaml | 1 + .../test/widget_test.dart | 9 +++-- 6 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 add_to_app/plugin/flutter_module_using_plugin/analysis_options.yaml diff --git a/add_to_app/plugin/flutter_module_using_plugin/analysis_options.yaml b/add_to_app/plugin/flutter_module_using_plugin/analysis_options.yaml new file mode 100644 index 000000000..85f6fbe91 --- /dev/null +++ b/add_to_app/plugin/flutter_module_using_plugin/analysis_options.yaml @@ -0,0 +1,19 @@ +include: package:flutter_lints/flutter.yaml + +analyzer: + strong-mode: + implicit-casts: false + implicit-dynamic: false + +linter: + rules: + avoid_types_on_closure_parameters: true + avoid_void_async: true + cancel_subscriptions: true + close_sinks: true + directives_ordering: true + package_api_docs: true + package_prefixed_library_names: true + test_types_in_equals: true + throw_in_finally: true + unnecessary_statements: true diff --git a/add_to_app/plugin/flutter_module_using_plugin/lib/cell.dart b/add_to_app/plugin/flutter_module_using_plugin/lib/cell.dart index 3715d684f..f4ca0b3d7 100644 --- a/add_to_app/plugin/flutter_module_using_plugin/lib/cell.dart +++ b/add_to_app/plugin/flutter_module_using_plugin/lib/cell.dart @@ -11,12 +11,14 @@ import 'package:sensors/sensors.dart'; // This is on alternate entrypoint for this module to display Flutter UI in // a (multi-)view integration scenario. void main() { - runApp(Cell()); + runApp(const Cell()); } class Cell extends StatefulWidget { + const Cell({Key key}) : super(key: key); + @override - State createState() => new _CellState(); + State createState() => _CellState(); } class _CellState extends State with WidgetsBindingObserver { @@ -29,8 +31,8 @@ class _CellState extends State with WidgetsBindingObserver { @override void initState() { - final channel = MethodChannel('dev.flutter.example/cell'); - channel.setMethodCallHandler((MethodCall call) async { + const channel = MethodChannel('dev.flutter.example/cell'); + channel.setMethodCallHandler((call) async { if (call.method == 'setCellNumber') { setState(() { cellNumber = call.arguments as int; @@ -73,10 +75,10 @@ class _CellState extends State with WidgetsBindingObserver { home: Container( color: Colors.white, child: Builder( - builder: (BuildContext context) { + builder: (context) { return Card( // Mimic the platform Material look. - margin: EdgeInsets.symmetric(horizontal: 36, vertical: 24), + margin: const EdgeInsets.symmetric(horizontal: 36, vertical: 24), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(16), ), @@ -103,15 +105,14 @@ class _CellState extends State with WidgetsBindingObserver { bottom: 0, child: Opacity( opacity: 0.2, - child: StreamBuilder( + child: StreamBuilder( // Don't continuously rebuild for nothing when the // cell isn't visible. stream: appLifecycleState == AppLifecycleState.resumed ? accelerometerEvents : Stream.value(defaultPosition), initialData: defaultPosition, - builder: (BuildContext context, - AsyncSnapshot snapshot) { + builder: (context, snapshot) { return Transform( // Figure out the phone's orientation relative // to gravity's direction. Ignore the z vector. @@ -120,7 +121,7 @@ class _CellState extends State with WidgetsBindingObserver { ..multiply(Matrix4.rotationY( snapshot.data.x / gravity * pi / 2)), alignment: Alignment.center, - child: FlutterLogo(size: 72)); + child: const FlutterLogo(size: 72)); }, ), ), diff --git a/add_to_app/plugin/flutter_module_using_plugin/lib/main.dart b/add_to_app/plugin/flutter_module_using_plugin/lib/main.dart index bcdb18031..388821fed 100644 --- a/add_to_app/plugin/flutter_module_using_plugin/lib/main.dart +++ b/add_to_app/plugin/flutter_module_using_plugin/lib/main.dart @@ -20,7 +20,7 @@ void main() { runApp( ChangeNotifierProvider.value( value: model, - child: MyApp(), + child: const MyApp(), ), ); } @@ -30,7 +30,7 @@ void main() { // This is unfortunately in this file due to // https://github.com/flutter/flutter/issues/72630. void showCell() { - runApp(Cell()); + runApp(const Cell()); } /// A simple model that uses a [MethodChannel] as the source of truth for the @@ -43,17 +43,17 @@ void showCell() { class CounterModel extends ChangeNotifier { CounterModel() { _channel.setMethodCallHandler(_handleMessage); - _channel.invokeMethod('requestCounter'); + _channel.invokeMethod('requestCounter'); } - final _channel = MethodChannel('dev.flutter.example/counter'); + final _channel = const MethodChannel('dev.flutter.example/counter'); int _count = 0; int get count => _count; void increment() { - _channel.invokeMethod('incrementCounter'); + _channel.invokeMethod('incrementCounter'); } Future _handleMessage(MethodCall call) async { @@ -69,13 +69,15 @@ class CounterModel extends ChangeNotifier { /// It offers two routes, one suitable for displaying as a full screen and /// another designed to be part of a larger UI. class MyApp extends StatelessWidget { + const MyApp({Key key}) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Module Title', routes: { - '/': (context) => FullScreenView(), - '/mini': (context) => Contents(), + '/': (context) => const FullScreenView(), + '/mini': (context) => const Contents(), }, ); } @@ -84,11 +86,13 @@ class MyApp extends StatelessWidget { /// Wraps [Contents] in a Material [Scaffold] so it looks correct when displayed /// full-screen. class FullScreenView extends StatelessWidget { + const FullScreenView({Key key}) : super(key: key); + @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('Full-screen Flutter with plugin'), + title: const Text('Full-screen Flutter with plugin'), ), body: const Contents(showExit: true), ); @@ -104,7 +108,7 @@ class FullScreenView extends StatelessWidget { class Contents extends StatelessWidget { final bool showExit; - const Contents({this.showExit = false}); + const Contents({this.showExit = false, Key key}) : super(key: key); @override Widget build(BuildContext context) { @@ -120,7 +124,7 @@ class Contents extends StatelessWidget { ), ), ), - Positioned.fill( + const Positioned.fill( child: Opacity( opacity: .25, child: FittedBox( @@ -138,7 +142,7 @@ class Contents extends StatelessWidget { '${mediaInfo.size.height.toStringAsFixed(1)}', style: Theme.of(context).textTheme.headline5, ), - SizedBox(height: 16), + const SizedBox(height: 16), Consumer( builder: (context, model, child) { return Text( @@ -147,12 +151,12 @@ class Contents extends StatelessWidget { ); }, ), - SizedBox(height: 16), + const SizedBox(height: 16), Consumer( builder: (context, model, child) { return ElevatedButton( onPressed: () => model.increment(), - child: Text('Tap me!'), + child: const Text('Tap me!'), ); }, ), @@ -160,18 +164,18 @@ class Contents extends StatelessWidget { onPressed: () async { // Use the url_launcher plugin to open the Flutter docs in // a browser. - final url = 'https://flutter.dev/docs'; + const url = 'https://flutter.dev/docs'; if (await launcher.canLaunch(url)) { launcher.launch(url); } }, - child: Text('Open Flutter Docs'), + child: const Text('Open Flutter Docs'), ), if (showExit) ...[ - SizedBox(height: 16), + const SizedBox(height: 16), ElevatedButton( onPressed: () => SystemNavigator.pop(), - child: Text('Exit this screen'), + child: const Text('Exit this screen'), ), ], ], diff --git a/add_to_app/plugin/flutter_module_using_plugin/pubspec.lock b/add_to_app/plugin/flutter_module_using_plugin/pubspec.lock index 2126b87b8..259fc4996 100644 --- a/add_to_app/plugin/flutter_module_using_plugin/pubspec.lock +++ b/add_to_app/plugin/flutter_module_using_plugin/pubspec.lock @@ -55,6 +55,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_lints: + dependency: "direct dev" + description: + name: flutter_lints + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" flutter_test: dependency: "direct dev" description: flutter @@ -72,6 +79,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.6.3" + lints: + dependency: transitive + description: + name: lints + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" matcher: dependency: transitive description: diff --git a/add_to_app/plugin/flutter_module_using_plugin/pubspec.yaml b/add_to_app/plugin/flutter_module_using_plugin/pubspec.yaml index 2613ff379..e8a1fe0fe 100644 --- a/add_to_app/plugin/flutter_module_using_plugin/pubspec.yaml +++ b/add_to_app/plugin/flutter_module_using_plugin/pubspec.yaml @@ -16,6 +16,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter + flutter_lints: ^1.0.0 flutter: uses-material-design: true diff --git a/add_to_app/plugin/flutter_module_using_plugin/test/widget_test.dart b/add_to_app/plugin/flutter_module_using_plugin/test/widget_test.dart index 40aceb1db..72807e9b7 100644 --- a/add_to_app/plugin/flutter_module_using_plugin/test/widget_test.dart +++ b/add_to_app/plugin/flutter_module_using_plugin/test/widget_test.dart @@ -6,16 +6,17 @@ // tree, read text, and verify that the values of widget properties are correct. import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; - import 'package:flutter_module_using_plugin/main.dart'; +import 'package:flutter_test/flutter_test.dart'; import 'package:provider/provider.dart'; class MockCounterModel extends ChangeNotifier implements CounterModel { int _count = 0; + @override int get count => _count; + @override void increment() { _count++; notifyListeners(); @@ -23,13 +24,13 @@ class MockCounterModel extends ChangeNotifier implements CounterModel { } void main() { - testWidgets('MiniView smoke test', (WidgetTester tester) async { + testWidgets('MiniView smoke test', (tester) async { // Build our app and trigger a frame. await tester.pumpWidget( MaterialApp( home: ChangeNotifierProvider.value( value: MockCounterModel(), - child: Contents(), + child: const Contents(), ), ), );