bluetooth_button.dart 2.07 KB
Newer Older
1 2
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
3 4
import 'package:logair_application/ui/animation/forward_animation.dart';
import 'package:logair_application/logic/handlers/bluetooth_le_handler.dart';
5
import 'package:logair_application/ui/dialog/bluetooth_dialog.dart';
6 7

class BluetoothButton extends StatefulWidget {
8
  BluetoothButton({Key key}) : super(key: key);
9 10

  @override
11
  _BluetoothButtonState createState() => _BluetoothButtonState();
12 13 14
}

class _BluetoothButtonState extends State<BluetoothButton> {
15
  _BluetoothButtonState({Key key});
16 17 18 19

  @override
  Widget build(BuildContext context) {
    return Material(
20 21 22 23 24 25 26 27 28 29 30 31 32
      child: StreamBuilder<bool>(
        stream: BTLEHandler().isDeviceConnected(),
        initialData: false,
        builder: (context, snapshot) {
          bool _bluetoothConnected = snapshot.data;
          return Ink(
            decoration: BoxDecoration(
              border: Border.all(
                color: Colors.indigoAccent,
                width: 2.0,
              ),
              color: _bluetoothConnected ? Colors.blue[200] : Colors.white,
              shape: BoxShape.circle,
33
            ),
34
            child: InkWell(
35
              borderRadius: BorderRadius.circular(100.0),
36 37 38 39 40 41 42 43 44 45
              onTap: () {
                print("onTap");
                if (!_bluetoothConnected) {
                  print("Tap On");
                  Navigator.push(
                    context,
                    PopForwardRoute(
                      page: BluetoothDialog(),
                    ),
                  );
46
                  return true;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
                } else {
                  BTLEHandler().disconnect();
                  print('Tap off');
                  return true;
                }
              },
              child: Padding(
                padding: EdgeInsets.all(10),
                child: Icon(
                  Icons.bluetooth, 
                  color: _bluetoothConnected ? Colors.grey : Colors.red,
                  size: 30.0,
                ),
              ),
            )
          );
        }
64 65 66 67
      ),
    );
  }
}