flutter_wear 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 73

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 <project root>/android/app/build.gradle and set the minSdkVersion to 23:

     minSdkVersion 23
    
  • Also add the following dependencies to the same file:

    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 <project root>/android/app/src/main/AndroidManifest.xml and add the following inside the manifest tag:

    <!-- Required for ambient mode support -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
    <!-- Flags the app as a Wear app -->
    <uses-feature android:name="android.hardware.type.watch" />
    
  • Inside the same file, update the contents of the application tag like this:

    <!-- KEEP THE application tag ATTRIBUTES AS IT IS -->
    <application
          android:name="io.flutter.app.FlutterApplication"
          android:label="wear_demo"
          android:icon="@mipmap/ic_launcher">
    
          <!-- REPLACE THE PART STARTING FROM BELOW -->
          <!-- Flags that the app doesn't require a companion phone app -->
          <meta-data
          android:name="com.google.android.wearable.standalone"
          android:value="true" />
    
          <activity
              android:name=".MainActivity"
              android:launchMode="singleTop"
              android:theme="@style/LaunchTheme"
              android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
              android:hardwareAccelerated="true"
              android:windowSoftInputMode="adjustResize">
    
              <meta-data
                android:name="io.flutter.app.android.NormalTheme"
                android:resource="@style/NormalTheme"
                />
    
              <meta-data
                android:name="io.flutter.app.android.SplashScreenDrawable"
                android:resource="@drawable/launch_background"
                />
              <intent-filter>
                  <action android:name="android.intent.action.MAIN"/>
                  <category android:name="android.intent.category.LAUNCHER"/>
              </intent-filter>
          </activity>
          <!-- TILL THE ABOVE LINE -->
      </application>
    
  • Go to <project root>/android/app/src/main/kotlin/<app_id>/MainActivity.kt and update the MainActivity code as following:
    // Except the package name
    // Replace everything
    
    import android.os.Bundle
    
    import io.flutter.app.FlutterActivity
    import io.flutter.plugins.GeneratedPluginRegistrant
    
    import androidx.wear.ambient.AmbientMode
    
    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 #

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<MyApp> {
  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: <Widget>[
                    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.

0.1.1 - beta #

  • Update usage instructions

0.1.0 - beta #

  • Initial Open Source release

example/lib/main.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<MyApp> {
  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: <Widget>[
                    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),
                    ),
                  ],
                ),
              );
            }),
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_wear: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_wear/flutter_wear.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
46
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform ios

Because:

  • package:flutter_wear/flutter_wear.dart that declares support for platforms: android

Package does not support Flutter platform linux

Because:

  • package:flutter_wear/flutter_wear.dart that declares support for platforms: android

Package does not support Flutter platform macos

Because:

  • package:flutter_wear/flutter_wear.dart that declares support for platforms: android

Package does not support Flutter platform web

Because:

  • package:flutter_wear/flutter_wear.dart that declares support for platforms: android

Package does not support Flutter platform windows

Because:

  • package:flutter_wear/flutter_wear.dart that declares support for platforms: android

Package not compatible with SDK dart

Because:

  • flutter_wear that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test