mirror of
https://github.com/flutter/samples.git
synced 2025-11-09 22:38:42 +00:00
Add a FlutterView level integration example on Android for add-to-app (#632)
This commit is contained in:
136
add_to_app/flutter_module_using_plugin/lib/cell.dart
Normal file
136
add_to_app/flutter_module_using_plugin/lib/cell.dart
Normal file
@@ -0,0 +1,136 @@
|
||||
// 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.
|
||||
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
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());
|
||||
}
|
||||
|
||||
class Cell extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() => new _CellState();
|
||||
}
|
||||
|
||||
class _CellState extends State<Cell> with WidgetsBindingObserver {
|
||||
static const double gravity = 9.81;
|
||||
static final AccelerometerEvent defaultPosition = AccelerometerEvent(0, 0, 0);
|
||||
|
||||
int cellNumber = 0;
|
||||
Random _random;
|
||||
AppLifecycleState appLifecycleState;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
final channel = MethodChannel('dev.flutter.example/cell');
|
||||
channel.setMethodCallHandler((MethodCall call) async {
|
||||
if (call.method == 'setCellNumber') {
|
||||
setState(() {
|
||||
cellNumber = call.arguments as int;
|
||||
_random = Random(cellNumber);
|
||||
});
|
||||
}
|
||||
});
|
||||
// Keep track of what the current platform lifecycle state is.
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
setState(() {
|
||||
appLifecycleState = state;
|
||||
});
|
||||
}
|
||||
|
||||
// Show a random bright color.
|
||||
Color randomLightColor() {
|
||||
_random ??= Random(cellNumber);
|
||||
|
||||
return Color.fromARGB(255, _random.nextInt(50) + 205,
|
||||
_random.nextInt(50) + 205, _random.nextInt(50) + 205);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
// The Flutter cells will be noticeably different (due to background color
|
||||
// and the Flutter logo). The banner breaks immersion.
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: Container(
|
||||
color: Colors.white,
|
||||
child: Builder(
|
||||
builder: (BuildContext context) {
|
||||
return Card(
|
||||
// Mimic the platform Material look.
|
||||
margin: EdgeInsets.symmetric(horizontal: 36, vertical: 24),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
elevation: 16,
|
||||
color: randomLightColor(),
|
||||
child: Stack(
|
||||
children: [
|
||||
Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
// Show a number provided by the platform based on
|
||||
// the cell's index.
|
||||
cellNumber.toString(),
|
||||
style: Theme.of(context).textTheme.headline3,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
left: 42,
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
child: Opacity(
|
||||
opacity: 0.2,
|
||||
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<AccelerometerEvent> snapshot) {
|
||||
return Transform(
|
||||
// Figure out the phone's orientation relative
|
||||
// to gravity's direction. Ignore the z vector.
|
||||
transform: Matrix4.rotationX(
|
||||
snapshot.data.y / gravity * pi / 2)
|
||||
..multiply(Matrix4.rotationY(
|
||||
snapshot.data.x / gravity * pi / 2)),
|
||||
alignment: Alignment.center,
|
||||
child: FlutterLogo(size: 72));
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import 'package:flutter/services.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:url_launcher/url_launcher.dart' as launcher;
|
||||
|
||||
import 'cell.dart';
|
||||
|
||||
/// The entrypoint for the flutter module.
|
||||
void main() {
|
||||
// This call ensures the Flutter binding has been set up before creating the
|
||||
@@ -23,6 +25,14 @@ void main() {
|
||||
);
|
||||
}
|
||||
|
||||
/// This is on alternate entrypoint for this module to display Flutter UI in
|
||||
/// a (multi-)view integration scenario.
|
||||
// This is unfortunately in this file due to
|
||||
// https://github.com/flutter/flutter/issues/72630.
|
||||
void showCell() {
|
||||
runApp(Cell());
|
||||
}
|
||||
|
||||
/// A simple model that uses a [MethodChannel] as the source of truth for the
|
||||
/// state of a counter.
|
||||
///
|
||||
|
||||
@@ -7,49 +7,49 @@ packages:
|
||||
name: async
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.5.0-nullsafety.1"
|
||||
version: "2.5.0-nullsafety.3"
|
||||
boolean_selector:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: boolean_selector
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0-nullsafety.1"
|
||||
version: "2.1.0-nullsafety.3"
|
||||
characters:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: characters
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0-nullsafety.3"
|
||||
version: "1.1.0-nullsafety.5"
|
||||
charcode:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: charcode
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0-nullsafety.1"
|
||||
version: "1.2.0-nullsafety.3"
|
||||
clock:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: clock
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0-nullsafety.1"
|
||||
version: "1.1.0-nullsafety.3"
|
||||
collection:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: collection
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.15.0-nullsafety.3"
|
||||
version: "1.15.0-nullsafety.5"
|
||||
fake_async:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: fake_async
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0-nullsafety.1"
|
||||
version: "1.2.0-nullsafety.3"
|
||||
flutter:
|
||||
dependency: "direct main"
|
||||
description: flutter
|
||||
@@ -65,20 +65,27 @@ packages:
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
js:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: js
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.6.3-nullsafety.3"
|
||||
matcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: matcher
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.12.10-nullsafety.1"
|
||||
version: "0.12.10-nullsafety.3"
|
||||
meta:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: meta
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.0-nullsafety.3"
|
||||
version: "1.3.0-nullsafety.6"
|
||||
nested:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -92,7 +99,7 @@ packages:
|
||||
name: path
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.8.0-nullsafety.1"
|
||||
version: "1.8.0-nullsafety.3"
|
||||
plugin_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -107,6 +114,13 @@ packages:
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "4.3.2+3"
|
||||
sensors:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: sensors
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.4.2+6"
|
||||
sky_engine:
|
||||
dependency: transitive
|
||||
description: flutter
|
||||
@@ -118,49 +132,49 @@ packages:
|
||||
name: source_span
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.8.0-nullsafety.2"
|
||||
version: "1.8.0-nullsafety.4"
|
||||
stack_trace:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: stack_trace
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.10.0-nullsafety.1"
|
||||
version: "1.10.0-nullsafety.6"
|
||||
stream_channel:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: stream_channel
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0-nullsafety.1"
|
||||
version: "2.1.0-nullsafety.3"
|
||||
string_scanner:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: string_scanner
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0-nullsafety.1"
|
||||
version: "1.1.0-nullsafety.3"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: term_glyph
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0-nullsafety.1"
|
||||
version: "1.2.0-nullsafety.3"
|
||||
test_api:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: test_api
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.2.19-nullsafety.2"
|
||||
version: "0.2.19-nullsafety.6"
|
||||
typed_data:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: typed_data
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.0-nullsafety.3"
|
||||
version: "1.3.0-nullsafety.5"
|
||||
url_launcher:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
@@ -209,7 +223,7 @@ packages:
|
||||
name: vector_math
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0-nullsafety.3"
|
||||
version: "2.1.0-nullsafety.5"
|
||||
sdks:
|
||||
dart: ">=2.10.0-110 <2.11.0"
|
||||
flutter: ">=1.22.0 <2.0.0"
|
||||
dart: ">=2.12.0-0.0 <3.0.0"
|
||||
flutter: ">=1.22.0"
|
||||
|
||||
@@ -11,6 +11,7 @@ dependencies:
|
||||
sdk: flutter
|
||||
provider: ^4.1.0
|
||||
url_launcher: ^5.2.5
|
||||
sensors: ^0.4.2
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
|
||||
Reference in New Issue
Block a user