123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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<ConnectButtonWithAnimations>
- 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<BluetoothState>(
- stream: bBloc.bluetoothState$,
- initialData: BluetoothState.on,
- builder: (_, AsyncSnapshot<BluetoothState> snapshot) {
- if (snapshot.hasData) {
- if (snapshot.data == BluetoothState.on) {
- // Bluetooth is on, button enabled.
- return StreamBuilder<bool>(
- stream: bBloc.isScanning$,
- initialData: false,
- builder: (_, AsyncSnapshot<bool> snapshotIsScanning) {
- if (snapshotIsScanning.hasData) {
- return AnimatedSwitcher(
- duration: durationOpacity,
- transitionBuilder:
- (Widget child, Animation<double> 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<bool>(
- stream: bBloc.isConnected$,
- initialData: false,
- builder: (_, AsyncSnapshot<bool> 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();
- });
- }
- }
|