bluetooth_button.dart 4.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:logair_application/ui/animation/forward_animation.dart';
import 'package:logair_application/logic/handlers/bluetooth_le_handler.dart';
import 'package:logair_application/ui/bluetooth_dialog/dialog.dart';
import 'package:logair_application/utils/enums/bluetooth_connection_status.dart';

/// Button enabling the app to connect to a PM detector device.
class BluetoothButton extends StatefulWidget {
  BluetoothButton({Key key}) : super(key: key);

  @override
  _BluetoothButtonState createState() => _BluetoothButtonState();
}

class _BluetoothButtonState extends State<BluetoothButton> {
  _BluetoothButtonState({Key key});

  Color _color;
  BluetoothConnectionStatus _status;

  // Can ignore dart(cancel_subscriptions) because linter error, subscription canceled in dispose.
  // ignore: cancel_subscriptions
  StreamSubscription<BluetoothConnectionStatus> _connectionStatusListener;

  /// This [Function] enables non-dirty transitions, only on value change, reducing overhead.
  void connectionStatusStateChanger(BluetoothConnectionStatus newStatus) {
    if (mounted && newStatus != _status) {
      Color newColor;
      switch (newStatus) {
        case BluetoothConnectionStatus.BTSTATUS_NOT_STREAMING:
          newColor = Colors.grey[350];
          break;
        case BluetoothConnectionStatus.BTSTATUS_DISCOVERING_SERVICES:
          newColor = Colors.blue[100];
          break;
        case BluetoothConnectionStatus.BTSTATUS_GETTING_CHARACTERISTICS:
          newColor = Colors.blue[200];
          break;
        case BluetoothConnectionStatus.BTSTATUS_SETTING_MTU:
          newColor = Colors.blue[300];
          break;
        case BluetoothConnectionStatus.BTSTATUS_STREAMING:
          newColor = Colors.blueAccent;
          break;
        default:
      }
      setState(() {
        _color = newColor;
        _status = newStatus;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    this._connectionStatusListener = BTLEHandler().getConnectionStatus().listen(connectionStatusStateChanger);
  }

  @override
  Future<void> dispose() async {
    this._connectionStatusListener.cancel();
    this._connectionStatusListener = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: StreamBuilder<BluetoothConnectionStatus>(
        stream: BTLEHandler().getConnectionStatus(),
        initialData: BluetoothConnectionStatus.BTSTATUS_NOT_STREAMING,
        builder: (context, snapshot) {
          BluetoothConnectionStatus _bluetoothStatus = snapshot.data;
          return Ink(
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.white,
                width: 2.0,
              ),
              shape: BoxShape.circle,
            ),
            child: Ink(
              decoration: BoxDecoration(
                border: Border.all(
                  color: Colors.indigoAccent,
                  width: 2.0,
                ),
                color: _color,
                shape: BoxShape.circle,
              ),
              child: InkWell(
                borderRadius: BorderRadius.circular(100.0),
                onTap: () {
                  print("onTap");
                  if (_bluetoothStatus != BluetoothConnectionStatus.BTSTATUS_STREAMING) {
                    print("Tap On");
                    Navigator.push(
                      context,
                      PopForwardRoute(
                        page: BluetoothDialog(),
                      ),
                    );
                    return true;
                  } else {
                    BTLEHandler().disconnect();
                    print('Tap off');
                    return true;
                  }
                },
                child: Padding(
                  padding: EdgeInsets.all(10),
                  child: Icon(
                    Icons.bluetooth, 
                    color: Colors.white,
                    size: 30.0,
                  ),
                ),
              )
            )
          );
        }
      ),
    );
  }
}