Skip to content

Commit

Permalink
Update readme and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxiang committed Oct 8, 2021
1 parent 5812a3b commit 7705445
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 122 deletions.
98 changes: 70 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,84 @@

A flutter plugin allows you to create native android floating window.

[](https://user-images.githubusercontent.com/1709072/136388895-4b576f60-f00e-4188-ae74-dd4a3da9beca.mp4)
## Install

- [Full example](https://github.com/qiuxiang/flutter-android-window/tree/main/example)
- [example.apk](https://github.com/qiuxiang/flutter-android-window/releases/download/latest/example.apk)
```
flutter pub add android_window
```

MainActivity.kt:

## Usage
```kotlin
class MainActivity : qiuxiang.android_window.AndroidWindowActivity()
```

Create MainApplication.kt:

```kotlin
package your_package // same as MainActivity.kt

class MainApplication : qiuxiang.android_window.AndroidWindowApplication()
```

Add `android:name=".MainApplication"` to AndroidManifest.xml `<application>`:

```xml
<application
android:name=".MainApplication"
...
>
```

## Example

main.dart:

```dart
import 'package:android_window/main.dart' as window;
import 'package:android_window/main.dart' as android_window;
import 'package:flutter/material.dart';
import 'android_window.dart';
@pragma('vm:entry-point')
void androidWindow() {
runApp(const AndroidWindowApp());
}
// run in main app
window.open(
entryPoint: 'androidWindow',
size: const Size(600, 800),
position: const Offset(200, 200),
);
void main() {
runApp(const App());
}
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(title: 'Flutter Demo', home: HomePage());
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => android_window.open(size: const Size(300, 200)),
child: const Icon(Icons.add),
),
);
}
}
```

android_window.dart:

```dart
import 'package:android_window/android_window.dart';
import 'package:flutter/material.dart';
class AndroidWindowApp extends StatelessWidget {
const AndroidWindowApp({Key? key}) : super(key: key);
Expand All @@ -51,33 +99,27 @@ class HomePage extends StatelessWidget {
Widget build(BuildContext context) {
return AndroidWindow(
child: Scaffold(
backgroundColor: Colors.grey.withOpacity(0.8),
body: const Center(child: Text('Hello android window')),
backgroundColor: Colors.lightGreen.withOpacity(0.9),
body: const Padding(
padding: EdgeInsets.all(8),
child: Text('Hello android window'),
),
),
);
}
}
```

MainActivity.kt:

```kotlin
import qiuxiang.android_window.AndroidWindowActivity
class MainActivity : AndroidWindowActivity()
```
Screenshot:

Create MainApplication.kt:
<img width=320 src=https://user-images.githubusercontent.com/1709072/136494923-fd9f10bb-aa1e-4365-bece-f595bc913ebf.png>

```kotlin
import qiuxiang.android_window.AndroidWindowApplication
class MainApplication : AndroidWindowApplication()
```
### More examples

Add `android:name=".MainApplication"` to AndroidManifest.xml:
- [Full example](https://github.com/qiuxiang/flutter-android-window/tree/main/example)
- [example.apk](https://github.com/qiuxiang/flutter-android-window/releases/download/latest/example.apk)

```xml
<application android:name=".MainApplication">
```
[](https://user-images.githubusercontent.com/1709072/136388895-4b576f60-f00e-4188-ae74-dd4a3da9beca.mp4)

[pub]: https://pub.dartlang.org/packages/android_window
[pub-badge]: https://img.shields.io/pub/v/android_window.svg
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package qiuxiang.android_window_example

import qiuxiang.android_window.AndroidWindowActivity

class MainActivity : AndroidWindowActivity()
class MainActivity : qiuxiang.android_window.AndroidWindowActivity()
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package qiuxiang.android_window_example

import qiuxiang.android_window.AndroidWindowApplication

class MainApplication : AndroidWindowApplication()
class MainApplication : qiuxiang.android_window.AndroidWindowApplication()
83 changes: 0 additions & 83 deletions example/lib/app.dart

This file was deleted.

83 changes: 82 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:android_window/main.dart' as window;
import 'package:flutter/material.dart';

import 'android_window.dart';
import 'app.dart';

@pragma('vm:entry-point')
void androidWindow() {
Expand All @@ -11,3 +11,84 @@ void androidWindow() {
void main() {
runApp(const App());
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return const MaterialApp(title: 'Flutter Demo', home: HomePage());
}
}

class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
window.setHandler((name, data) async {
switch (name) {
case 'hello':
showSnackBar(context, 'message from android window: $data');
return 'hello android window';
}
});
return Scaffold(
body: SafeArea(
child: ListView(padding: const EdgeInsets.all(16), children: [
ElevatedButton(
onPressed: () async {
showSnackBar(context, '${await window.canDrawOverlays()}');
},
child: const Text('Check can draw overlays'),
),
const ElevatedButton(
onPressed: window.requestPermission,
child: Text('Request overlay display permission'),
),
ElevatedButton(
onPressed: () => window.open(
size: const Size(600, 800),
position: const Offset(200, 200),
),
child: const Text('Open android window'),
),
const ElevatedButton(
onPressed: window.close,
child: Text('Close android window'),
),
ElevatedButton(
onPressed: () => window.resize(600, 400),
child: const Text('resize(600, 400)'),
),
ElevatedButton(
onPressed: () => window.resize(400, 600),
child: const Text('resize(400, 600)'),
),
ElevatedButton(
onPressed: () => window.setPosition(0, 0),
child: const Text('setPosition(0, 0)'),
),
ElevatedButton(
onPressed: () => window.setPosition(300, 300),
child: const Text('setPosition(300, 300)'),
),
ElevatedButton(
onPressed: () async {
final response = await window.post(
'hello',
'hello android window',
);
showSnackBar(context, 'response from android window: $response');
},
child: const Text('Send message to android window'),
),
]),
),
);
}

showSnackBar(BuildContext context, String title) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(title)));
}
}
8 changes: 4 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ final _api = MainApi();
/// For example:
///
/// ```dart
// @pragma('vm:entry-point')
// void androidWindow() {
// runApp(const AndroidWindowApp());
// }
/// @pragma('vm:entry-point')
/// void androidWindow() {
/// runApp(const AndroidWindowApp());
/// }
/// ```
void open({
String entryPoint = 'androidWindow',
Expand Down

0 comments on commit 7705445

Please sign in to comment.