import 'package:flutter/material.dart'; import 'package:flutter_blue/flutter_blue.dart'; import 'package:touch_demonstrator/src/blocs/BlocProvider.dart'; //bloc: import 'package:flare_flutter/flare_actor.dart'; class ConnectButtonWithAnimations extends StatefulWidget { @override ConnectButtonWithAnimationsState createState() { return new ConnectButtonWithAnimationsState(); } } class ConnectButtonWithAnimationsState extends State with SingleTickerProviderStateMixin { /// Builds Button if the app is not connected to a touch pad or animates search progress. Animation animation; AnimationController animationController; static const _heightContainer = 60.0; static const _widthContainer = 170.0; static const EdgeInsets _containerEdgeInset = EdgeInsets.all(8.0); static const durationOpacity = Duration(milliseconds: 150); @override Widget build(BuildContext context) { final bBloc = BlocProvider.of(context).bluetoothBlocGetter; return Hero( tag: 'buttonHero', child: Container( margin: _containerEdgeInset, height: _heightContainer, width: _widthContainer, child: StreamBuilder( stream: bBloc.bluetoothState$, initialData: BluetoothState.on, builder: (_, AsyncSnapshot snapshot) { if (snapshot.hasData) { if (snapshot.data == BluetoothState.on) { // Bluetooth is on, button enabled. return StreamBuilder( stream: bBloc.isScanning$, initialData: false, builder: (_, AsyncSnapshot snapshotIsScanning) { if (snapshotIsScanning.hasData) { return AnimatedSwitcher( duration: durationOpacity, transitionBuilder: (Widget child, Animation opacity) { return Padding( padding: EdgeInsets.all(8.0), child: FadeTransition( child: child, opacity: opacity), ); }, child: (snapshotIsScanning.data) ? ScanAnimation() : ConnectButton()); } else return Container(); }); } else { // Bluetooth is off, button disabled. return Container(); } } }), ), ); } } class ScanAnimation extends StatelessWidget { @override Widget build(BuildContext context) { return FlareActor( 'assets/Animations/loading2.flr', animation: 'Loading', fit: BoxFit.contain, alignment: Alignment.center, isPaused: false, ); } } class ConnectButton extends StatelessWidget { @override Widget build(BuildContext context) { var _primaryColor = Theme.of(context).primaryColor; var _buttonTextStyle = TextStyle(color: _primaryColor, fontSize: 20.0); final bBloc = BlocProvider.of(context).bluetoothBlocGetter; var _buttonText; var _buttonFunction; return StreamBuilder( stream: bBloc.isConnected$, initialData: false, builder: (_, AsyncSnapshot snapshotIsConnected) { if (snapshotIsConnected.hasData) { if (snapshotIsConnected.data) { _buttonText = Text( "Disconnect", style: _buttonTextStyle, key: Key('DisconnectButton'), // for integration test ); _buttonFunction = () => bBloc.disconnect.add(null); } else { _buttonText = Text( "Connect", style: _buttonTextStyle, key: Key('ConnectButton'), // for integration test ); _buttonFunction = () => bBloc.scan.add(null); } return Center( child: OutlineButton( key: Key('ConnectionButton'), // for integration test borderSide: BorderSide(color: _primaryColor), shape: BeveledRectangleBorder(), onPressed: _buttonFunction, child: _buttonText, ), ); } else return Container(); }); } }