1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 14:28:51 +00:00

Tidying up add-to-app samples (#160)

This commit is contained in:
Andrew Brogdon
2019-11-05 09:21:39 -08:00
committed by GitHub
parent e184a46ce6
commit 8155d8a777
17 changed files with 146 additions and 38 deletions

View File

@@ -7,7 +7,10 @@ import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart' as launcher;
/// The entrypoint for the flutter module.
void main() {
// This call ensures the Flutter binding has been set up before creating the
// MethodChannel-based model.
WidgetsFlutterBinding.ensureInitialized();
final model = CounterModel();
@@ -20,6 +23,13 @@ void main() {
);
}
/// A simple model that uses a [MethodChannel] as the source of truth for the
/// state of the counter.
///
/// Rather than storing app state data within the Flutter module itself (where
/// the native portions of the app can't access it), this module passes messages
/// back to the containing app whenever it needs to increment or retrieve the
/// value of the counter.
class CounterModel extends ChangeNotifier {
CounterModel() {
_channel.setMethodCallHandler(_handleMessage);
@@ -44,6 +54,10 @@ class CounterModel extends ChangeNotifier {
}
}
/// The "app" displayed by this module.
///
/// 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 {
@override
Widget build(BuildContext context) {
@@ -57,6 +71,8 @@ class MyApp extends StatelessWidget {
}
}
/// Wraps [Contents] in a Material [Scaffold] so it looks correct when displayed
/// full-screen.
class FullScreenView extends StatelessWidget {
@override
Widget build(BuildContext context) {
@@ -69,6 +85,12 @@ class FullScreenView extends StatelessWidget {
}
}
/// The actual content displayed by the module.
///
/// This widget displays info about the state of a counter and how much room (in
/// logical pixels) it's been given. It also offers buttons to increment the
/// counter, opening the Flutter documentation via the url_launcher plugin, and
/// (optionally) close the Flutter view.
class Contents extends StatelessWidget {
final bool showExit;
@@ -126,6 +148,8 @@ class Contents extends StatelessWidget {
),
RaisedButton(
onPressed: () async {
// Use the url_launcher plugin to open the Flutter docs in
// a browser.
final url = 'https://flutter.dev/docs';
if (await launcher.canLaunch(url)) {
launcher.launch(url);