mirror of
https://github.com/flutter/samples.git
synced 2026-03-25 13:51:35 +00:00
Add-to-app samples using plugin (#152)
This commit is contained in:
@@ -0,0 +1 @@
|
||||
buildscript {}
|
||||
@@ -2,13 +2,47 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:path_provider/path_provider.dart' as paths;
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:url_launcher/url_launcher.dart' as launcher;
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
void main() {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
final model = CounterModel();
|
||||
|
||||
runApp(
|
||||
ChangeNotifierProvider.value(
|
||||
value: model,
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
class CounterModel extends ChangeNotifier {
|
||||
CounterModel() {
|
||||
_channel.setMethodCallHandler(_handleMessage);
|
||||
_channel.invokeMethod('requestCounter');
|
||||
}
|
||||
|
||||
final _channel = MethodChannel('dev.flutter.example/counter');
|
||||
|
||||
int _count = 0;
|
||||
|
||||
int get count => _count;
|
||||
|
||||
void increment() {
|
||||
_channel.invokeMethod('incrementCounter');
|
||||
}
|
||||
|
||||
Future<dynamic> _handleMessage(MethodCall call) async {
|
||||
if (call.method == 'reportCounter') {
|
||||
_count = call.arguments as int;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
@@ -35,30 +69,11 @@ class FullScreenView extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class Contents extends StatefulWidget {
|
||||
class Contents extends StatelessWidget {
|
||||
final bool showExit;
|
||||
|
||||
const Contents({this.showExit = false});
|
||||
|
||||
@override
|
||||
_ContentsState createState() => _ContentsState();
|
||||
}
|
||||
|
||||
class _ContentsState extends State<Contents> {
|
||||
int count = 0;
|
||||
|
||||
// Start off with a completed future so the FutureBuilder code below doesn't
|
||||
// need to worry about null.
|
||||
Future<void> saveFuture = Future.value(null);
|
||||
|
||||
Future<void> _saveCount() async {
|
||||
final dir = await paths.getApplicationDocumentsDirectory();
|
||||
final file = File('${dir.path}/count.txt');
|
||||
setState(() {
|
||||
saveFuture = file.writeAsString('$count\n');
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final mediaInfo = MediaQuery.of(context);
|
||||
@@ -92,32 +107,33 @@ class _ContentsState extends State<Contents> {
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'Taps: $count',
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
RaisedButton(
|
||||
onPressed: () => setState(() => count++),
|
||||
child: Text('Tap me!'),
|
||||
),
|
||||
FutureBuilder(
|
||||
future: saveFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
return RaisedButton(
|
||||
onPressed: _saveCount,
|
||||
child: Text('Save count'),
|
||||
);
|
||||
} else {
|
||||
return RaisedButton(
|
||||
onPressed: null,
|
||||
child: Text('Save count'),
|
||||
);
|
||||
}
|
||||
Consumer<CounterModel>(
|
||||
builder: (context, model, child) {
|
||||
return Text(
|
||||
'Taps: ${model.count}',
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
);
|
||||
},
|
||||
),
|
||||
if (widget.showExit) ...[
|
||||
SizedBox(height: 16),
|
||||
Consumer<CounterModel>(
|
||||
builder: (context, model, child) {
|
||||
return RaisedButton(
|
||||
onPressed: () => model.increment(),
|
||||
child: Text('Tap me!'),
|
||||
);
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
onPressed: () async {
|
||||
final url = 'https://flutter.dev/docs';
|
||||
if (await launcher.canLaunch(url)) {
|
||||
launcher.launch(url);
|
||||
}
|
||||
},
|
||||
child: Text('Open Flutter Docs'),
|
||||
),
|
||||
if (showExit) ...[
|
||||
SizedBox(height: 16),
|
||||
RaisedButton(
|
||||
onPressed: () => SystemNavigator.pop(),
|
||||
|
||||
@@ -95,13 +95,6 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.6.4"
|
||||
path_provider:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: path_provider
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.0"
|
||||
pedantic:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -116,13 +109,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.4.0"
|
||||
platform:
|
||||
dependency: transitive
|
||||
provider:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: platform
|
||||
name: provider
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.2.1"
|
||||
version: "3.1.0"
|
||||
quiver:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -184,6 +177,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.6"
|
||||
url_launcher:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: url_launcher
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "5.2.0"
|
||||
vector_math:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -200,4 +200,4 @@ packages:
|
||||
version: "3.5.0"
|
||||
sdks:
|
||||
dart: ">=2.5.0 <3.0.0"
|
||||
flutter: ">=0.1.4 <2.0.0"
|
||||
flutter: ">=1.5.0 <2.0.0"
|
||||
|
||||
@@ -9,7 +9,8 @@ environment:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
path_provider: ^1.3.0
|
||||
provider: ^3.1.0
|
||||
url_launcher: ^5.1.6
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Reference in New Issue
Block a user