mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
Adds data transfer to Add2App samples, using caching on Android (#146)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
package="dev.flutter.example.androidfullscreen">
|
||||
|
||||
<application
|
||||
android:name="io.flutter.app.FlutterApplication"
|
||||
android:name=".MyApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
|
||||
@@ -6,18 +6,32 @@ package dev.flutter.example.androidfullscreen
|
||||
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private var counterLabel: TextView? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
counterLabel = findViewById(R.id.counter_label)
|
||||
|
||||
val button = findViewById<Button>(R.id.launch_button)
|
||||
|
||||
button.setOnClickListener {
|
||||
startActivity(FlutterActivity.createDefaultIntent(this))
|
||||
val intent = FlutterActivity
|
||||
.withCachedEngine(ENGINE_ID)
|
||||
.build(this)
|
||||
startActivity(intent)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
val app = application as MyApplication
|
||||
counterLabel?.text = "Current count: ${app.count}"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright 2019 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package dev.flutter.example.androidfullscreen
|
||||
|
||||
import android.app.Application
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.embedding.engine.FlutterEngineCache
|
||||
import io.flutter.embedding.engine.dart.DartExecutor
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.view.FlutterMain
|
||||
|
||||
const val ENGINE_ID = "1"
|
||||
|
||||
class MyApplication : Application() {
|
||||
var count = 0
|
||||
|
||||
private var channel: MethodChannel? = null
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
FlutterMain.startInitialization(this)
|
||||
FlutterMain.ensureInitializationComplete(this, null)
|
||||
|
||||
val flutterEngine = FlutterEngine(this)
|
||||
flutterEngine
|
||||
.dartExecutor
|
||||
.executeDartEntrypoint(
|
||||
DartExecutor.DartEntrypoint.createDefault()
|
||||
)
|
||||
|
||||
FlutterEngineCache.getInstance().put(ENGINE_ID, flutterEngine)
|
||||
|
||||
channel = MethodChannel(flutterEngine.dartExecutor, "dev.flutter.example/counter")
|
||||
|
||||
channel?.setMethodCallHandler { call, _ ->
|
||||
when (call.method) {
|
||||
"incrementCounter" -> {
|
||||
count++
|
||||
reportCounter()
|
||||
}
|
||||
"requestCounter" -> {
|
||||
print("requestCounter")
|
||||
reportCounter()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun reportCounter() {
|
||||
channel?.invokeMethod("reportCounter", count)
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,18 @@
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/counter_label"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="32dp"
|
||||
android:text="Current counter: 0"
|
||||
app:layout_constraintBottom_toTopOf="@+id/launch_button"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
tools:ignore="HardcodedText" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@@ -4,8 +4,44 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
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
|
||||
@@ -32,18 +68,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;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final mediaInfo = MediaQuery.of(context);
|
||||
@@ -73,20 +102,28 @@ class _ContentsState extends State<Contents> {
|
||||
children: [
|
||||
Text(
|
||||
'Window is ${mediaInfo.size.width.toStringAsFixed(1)} x '
|
||||
'${mediaInfo.size.height.toStringAsFixed(1)}',
|
||||
'${mediaInfo.size.height.toStringAsFixed(1)}',
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'Taps: $count',
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
Consumer<CounterModel>(
|
||||
builder: (context, model, child) {
|
||||
return Text(
|
||||
'Taps: ${model.count}',
|
||||
style: Theme.of(context).textTheme.headline,
|
||||
);
|
||||
},
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
RaisedButton(
|
||||
onPressed: () => setState(() => count++),
|
||||
child: Text('Tap me!'),
|
||||
Consumer<CounterModel>(
|
||||
builder: (context, model, child) {
|
||||
return RaisedButton(
|
||||
onPressed: () => model.increment(),
|
||||
child: Text('Tap me!'),
|
||||
);
|
||||
},
|
||||
),
|
||||
if (widget.showExit) ...[
|
||||
if (showExit) ...[
|
||||
SizedBox(height: 16),
|
||||
RaisedButton(
|
||||
onPressed: () => SystemNavigator.pop(),
|
||||
|
||||
@@ -109,6 +109,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.4.0"
|
||||
provider:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: provider
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "3.1.0"
|
||||
quiver:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
@@ -9,6 +9,7 @@ environment:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
provider: ^3.1.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
@@ -18,21 +18,32 @@
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<subviews>
|
||||
<button opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Cnf-d3-wlx">
|
||||
<rect key="frame" x="105" y="432" width="204" height="30"/>
|
||||
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Cnf-d3-wlx">
|
||||
<rect key="frame" x="105" y="433" width="204" height="30"/>
|
||||
<state key="normal" title="Launch Flutter ViewController"/>
|
||||
<connections>
|
||||
<action selector="buttonWasTapped:" destination="BYZ-38-t0r" eventType="primaryActionTriggered" id="yLP-26-wPf"/>
|
||||
</connections>
|
||||
</button>
|
||||
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Current counter: 0" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="KJB-T1-vx3">
|
||||
<rect key="frame" x="136.5" y="380" width="141" height="21"/>
|
||||
<fontDescription key="fontDescription" type="system" pointSize="17"/>
|
||||
<nil key="textColor"/>
|
||||
<nil key="highlightedColor"/>
|
||||
</label>
|
||||
</subviews>
|
||||
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
|
||||
<constraints>
|
||||
<constraint firstItem="Cnf-d3-wlx" firstAttribute="top" secondItem="KJB-T1-vx3" secondAttribute="bottom" constant="32" id="HZo-C0-7aM"/>
|
||||
<constraint firstItem="Cnf-d3-wlx" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="IZY-8s-o5L"/>
|
||||
<constraint firstItem="Cnf-d3-wlx" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="ofh-fr-CHR"/>
|
||||
<constraint firstItem="KJB-T1-vx3" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="vxr-sw-WVe"/>
|
||||
</constraints>
|
||||
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
|
||||
</view>
|
||||
<connections>
|
||||
<outlet property="counterLabel" destination="KJB-T1-vx3" id="Uph-NZ-re8"/>
|
||||
</connections>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
|
||||
@@ -6,10 +6,43 @@ import UIKit
|
||||
import Flutter
|
||||
|
||||
class ViewController: UIViewController {
|
||||
|
||||
@IBOutlet weak var counterLabel: UILabel!
|
||||
|
||||
var methodChannel : FlutterMethodChannel?
|
||||
var count = 0
|
||||
|
||||
override func viewDidLoad() {
|
||||
if let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine {
|
||||
methodChannel = FlutterMethodChannel(name: "dev.flutter.example/counter",
|
||||
binaryMessenger: flutterEngine.binaryMessenger)
|
||||
methodChannel?.setMethodCallHandler({ [weak self]
|
||||
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
|
||||
if let strongSelf = self {
|
||||
switch(call.method) {
|
||||
case "incrementCounter":
|
||||
strongSelf.count += 1
|
||||
strongSelf.counterLabel.text = "Current counter: \(strongSelf.count)"
|
||||
strongSelf.reportCounter()
|
||||
case "requestCounter":
|
||||
strongSelf.reportCounter()
|
||||
default:
|
||||
// Unrecognized method name
|
||||
print("Unrecognized method name: \(call.method)")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func reportCounter() {
|
||||
methodChannel?.invokeMethod("reportCounter", arguments: count)
|
||||
}
|
||||
|
||||
@IBAction func buttonWasTapped(_ sender: Any) {
|
||||
let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine
|
||||
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!
|
||||
self.present(flutterViewController, animated: false, completion: nil)
|
||||
if let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine {
|
||||
let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
|
||||
self.present(flutterViewController, animated: false, completion: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,6 @@ SPEC CHECKSUMS:
|
||||
flutter_module: 37f078337caf8acad3074374a49bf81442fd3e07
|
||||
FlutterPluginRegistrant: d59dd07dd90e9c6430996073575a5bd1ea19677e
|
||||
|
||||
PODFILE CHECKSUM: 3a2cacb59163f4a25654a84fcd92b179f682c008
|
||||
PODFILE CHECKSUM: 1f9809794b37c933b7cb40791d64724dc2376c54
|
||||
|
||||
COCOAPODS: 1.7.5
|
||||
|
||||
Reference in New Issue
Block a user