Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sbis04/flutter_wear

Flutter WearOS plugin
https://github.com/sbis04/flutter_wear

Last synced: 19 days ago
JSON representation

Flutter WearOS plugin

Awesome Lists containing this project

README

        


Flutter Wear

This is a Flutter plugin for WearOS devices

## Features

* Helps in getting the **shape** (square or round) of the watch face
* Handles the mode changes, **Active Mode** and **Ambient Mode**

More features will be arriving soon.

EXAMPLE APP


Flutter Wear App

> ***NOTE:*** The project is not using *Fluter Embedding v2*

## Usage

* Add the dependency `flutter_wear` to your **pubspec.yaml** file.

* Go to `/android/app/build.gradle` and set the `minSdkVersion` to **23**:

```gradle
minSdkVersion 23
```

* Also add the following dependencies to the same file:
```gradle
dependencies {
// Wear dependencies
implementation 'com.android.support:wear:27.1.1'
implementation 'com.google.android.support:wearable:2.3.0'
compileOnly 'com.google.android.wearable:wearable:2.3.0'
}
```

* Go to `/android/app/src/main/AndroidManifest.xml` and add the following inside the `manifest` tag:
```xml





```

* Inside the same file, update the contents of the `application` tag like this:
```xml











```

* Go to `/android/app/src/main/kotlin//MainActivity.kt` and update the `MainActivity` code as following:
```kotlin
// Except the package name
// Replace everything

import android.os.Bundle

// Do not import this package:
// io.flutter.embedding.android.FlutterActivity

// Make sure that you import the exact same packages here
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant

import androidx.wear.ambient.AmbientMode

// Here, replace the app id with yours or,
// just open the android folder using Android Studio &
// import the following two packages
import com.souvikbiswas.flutter_wear.FlutterAmbientCallback
import com.souvikbiswas.flutter_wear.getChannel

class MainActivity: FlutterActivity(), AmbientMode.AmbientCallbackProvider {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)

// Wire up the activity for ambient callbacks
AmbientMode.attachAmbientSupport(this)
}

override fun getAmbientCallback(): AmbientMode.AmbientCallback {
return FlutterAmbientCallback(getChannel(flutterView))
}
}
```

## Widgets

There are three primary widgets:

* **WearShape**: Determines the *shape* of the watch face, whether it is square or round.
* **WearMode**: Determines the *current mode* of the watch (*Active Mode* or *Ambient Mode*). The widgets inside it rebuilds whenever the mode changes.
* **WearInheritedShape**: It can be used for passing the shape of the watch down the widget tree.

## Example

```dart
import 'package:flutter/material.dart';
import 'package:flutter_wear/mode.dart';
import 'package:flutter_wear/shape.dart';
import 'package:flutter_wear/wear_mode.dart';
import 'package:flutter_wear/wear_shape.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
Color _containerColor;
Color _textColor;

@override
void initState() {
super.initState();

_containerColor = Colors.white;
_textColor = Colors.black;
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: WearShape(
builder: (context, shape) => WearMode(builder: (context, mode) {
if (mode == Mode.active) {
_containerColor = Colors.white;
_textColor = Colors.black;
} else {
_containerColor = Colors.black;
_textColor = Colors.white;
}
return Container(
color: _containerColor,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(),
FlutterLogo(size: 100),
Text(
'Shape: ${shape == Shape.round ? 'round' : 'square'}',
style: TextStyle(color: _textColor),
),
Text(
'Mode: ${mode == Mode.active ? 'Active' : 'Ambient'}',
style: TextStyle(color: _textColor),
),
],
),
);
}),
),
),
),
);
}
}
```
## License

Copyright (c) 2020 Souvik Biswas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.