import 'package:flutter/material.dart'; import 'package:touch_demonstrator/ui/pageTouchPoints/AppBarPageTouchPoints.dart'; import 'package:touch_demonstrator/ui/pageTouchPoints/SideBar.dart'; import 'dart:core'; import 'package:touch_demonstrator/src/blocs/BlocProvider.dart'; //bloc: import 'package:touch_demonstrator/ui/pageTouchPoints/ScanResultBottomSheet.dart'; import 'package:touch_demonstrator/ui/pageTouchPoints/ButtonOrAnimation.dart'; import 'package:touch_demonstrator/ui/pageTouchPoints/Logo.dart'; import 'package:touch_demonstrator/ui/pageTouchPoints/Sensor.dart'; import 'package:flutter_blue/flutter_blue.dart'; import 'package:vibrate/vibrate.dart'; import 'package:touch_demonstrator/model/vibrate.dart'; class PageTouchPoints extends StatefulWidget { PageTouchPoints({Key key, this.title}) : super(key: key); final String title; @override _PageTouchPointsState createState() => new _PageTouchPointsState(); } class _PageTouchPointsState extends State { final GlobalKey mScaffoldState = new GlobalKey(); static Modal modal; static bool firstTime = true; @override void dispose() { final bBloc = BlocProvider.of(context).bluetoothBlocGetter; bBloc.disconnect.add(null); //disconnect everything super.dispose(); } @override void didChangeDependencies() { super.didChangeDependencies(); print('didChangeDependencies'); /// Manages Snackbar "no device found" and "Bluetooth off" and toggles the debugmode. final bBloc = BlocProvider.of(context).bluetoothBlocGetter; final debugBloc = BlocProvider.of(context).debugBlocGetter; _showSnackbar(String textInSnackBar) { /// Shows Snackbar for 0.5 s with String textInSnackBar in it print('snackbar $textInSnackBar'); mScaffoldState.currentState.showSnackBar(SnackBar( content: Text( textInSnackBar, style: TextStyle(fontSize: 15), ), duration: Duration(milliseconds: 1000), )); } _toggleDebug(bool debugStatus) { vibrate(FeedbackType.success); _showSnackbar('Debug View: ${debugStatus ? 'enabled' : 'disabled'}!'); } if (firstTime) { firstTime = false; modal = new Modal(); bBloc.devicesFound$.listen((device) { if (device.length == 0) { print('Snackbar: No device found'); _showSnackbar('No device found'); vibrateDelayed(FeedbackType.warning, Duration(milliseconds: 150)); } else { modal.connectBottomSheet(context, device); } }); debugBloc.debugEnabledStatus$.listen((data) => _toggleDebug(data)); bBloc.bluetoothState$ .where((bluetoothState) => bluetoothState == BluetoothState.off) .listen( (bluetoothState) => _showBluetoothOffSnackbar(bluetoothState)); bBloc.bluetoothState$ .where((bluetoothState) => bluetoothState == BluetoothState.on) .listen((_) => mScaffoldState.currentState.removeCurrentSnackBar()); } else { print('REMOVE current snack bar'); mScaffoldState.currentState.removeCurrentSnackBar(); } } _showBluetoothOffSnackbar(BluetoothState bluetoothState) { /// Shows Snackbar if Bluetooth is off to inform user. var textInSnackbar = 'Bluetooth is off!'; print('snackbar $textInSnackbar'); mScaffoldState.currentState.showSnackBar(SnackBar( backgroundColor: Colors.red, content: Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Icon(Icons.warning), Padding( padding: EdgeInsets.only(left: 10.0), child: Text( textInSnackbar, textAlign: TextAlign.center, style: TextStyle(fontSize: 25), ), ), ], ), duration: Duration(days: 1), )); } @override Widget build(BuildContext context) { /// build UI for the main screen in the app return Scaffold( key: mScaffoldState, // necessary for toast message appBar: buildAppBarPageTouchPoints(context), drawer: LeftDrawer(), body: SafeArea(child: OrientationBuilder(builder: (context, orientation) { return _buildBody(orientation); // return _buildVerticalLayout(); })), ); } _buildBody(Orientation orientation) { if (orientation == Orientation.portrait) return _buildVerticalLayout(); else return _buildHorizontalLayout(); } static const _heightLogo = 100.0; _buildVerticalLayout() { /// Layout for vertical orientation EdgeInsets devicePadding = MediaQuery.of(context).padding; return Padding( padding: const EdgeInsets.all(5.0), child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Container(height: devicePadding.top), LogoWithText(_heightLogo), Expanded(child: Sensor()), ConnectButtonWithAnimations(), ], ), ); } _buildHorizontalLayout() { /// Layout for horizontal orientation return Padding( padding: const EdgeInsets.all(8.0), child: Column(children: [ Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Column( mainAxisAlignment: MainAxisAlignment.start, children: [ LogoWithText(_heightLogo), ConnectButtonWithAnimations(), ], ), Column( children: [ Sensor(), ], ), ], ), ]), ); } }