Masterarbeit Richard Stern. Flutter App, sich mit einem Bluetooth-Gerät verbindet und Berührungen auf einem Sensor visualisiert.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ButtonOrAnimation.dart 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_blue/flutter_blue.dart';
  3. import 'package:touch_demonstrator/src/blocs/BlocProvider.dart'; //bloc:
  4. import 'package:flare_flutter/flare_actor.dart';
  5. class ConnectButtonWithAnimations extends StatefulWidget {
  6. @override
  7. ConnectButtonWithAnimationsState createState() {
  8. return new ConnectButtonWithAnimationsState();
  9. }
  10. }
  11. class ConnectButtonWithAnimationsState
  12. extends State<ConnectButtonWithAnimations>
  13. with SingleTickerProviderStateMixin {
  14. /// Builds Button if the app is not connected to a touch pad or animates search progress.
  15. Animation animation;
  16. AnimationController animationController;
  17. static const _heightContainer = 60.0;
  18. static const _widthContainer = 170.0;
  19. static const EdgeInsets _containerEdgeInset = EdgeInsets.all(8.0);
  20. static const durationOpacity = Duration(milliseconds: 150);
  21. @override
  22. Widget build(BuildContext context) {
  23. final bBloc = BlocProvider.of(context).bluetoothBlocGetter;
  24. return Hero(
  25. tag: 'buttonHero',
  26. child: Container(
  27. margin: _containerEdgeInset,
  28. height: _heightContainer,
  29. width: _widthContainer,
  30. child: StreamBuilder<BluetoothState>(
  31. stream: bBloc.bluetoothState$,
  32. initialData: BluetoothState.on,
  33. builder: (_, AsyncSnapshot<BluetoothState> snapshot) {
  34. if (snapshot.hasData) {
  35. if (snapshot.data == BluetoothState.on) {
  36. // Bluetooth is on, button enabled.
  37. return StreamBuilder<bool>(
  38. stream: bBloc.isScanning$,
  39. initialData: false,
  40. builder: (_, AsyncSnapshot<bool> snapshotIsScanning) {
  41. if (snapshotIsScanning.hasData) {
  42. return AnimatedSwitcher(
  43. duration: durationOpacity,
  44. transitionBuilder:
  45. (Widget child, Animation<double> opacity) {
  46. return Padding(
  47. padding: EdgeInsets.all(8.0),
  48. child: FadeTransition(
  49. child: child, opacity: opacity),
  50. );
  51. },
  52. child: (snapshotIsScanning.data)
  53. ? ScanAnimation()
  54. : ConnectButton());
  55. } else
  56. return Container();
  57. });
  58. } else {
  59. // Bluetooth is off, button disabled.
  60. return Container();
  61. }
  62. }
  63. }),
  64. ),
  65. );
  66. }
  67. }
  68. class ScanAnimation extends StatelessWidget {
  69. @override
  70. Widget build(BuildContext context) {
  71. return FlareActor(
  72. 'assets/Animations/loading2.flr',
  73. animation: 'Loading',
  74. fit: BoxFit.contain,
  75. alignment: Alignment.center,
  76. isPaused: false,
  77. );
  78. }
  79. }
  80. class ConnectButton extends StatelessWidget {
  81. @override
  82. Widget build(BuildContext context) {
  83. var _primaryColor = Theme.of(context).primaryColor;
  84. var _buttonTextStyle = TextStyle(color: _primaryColor, fontSize: 20.0);
  85. final bBloc = BlocProvider.of(context).bluetoothBlocGetter;
  86. var _buttonText;
  87. var _buttonFunction;
  88. return StreamBuilder<bool>(
  89. stream: bBloc.isConnected$,
  90. initialData: false,
  91. builder: (_, AsyncSnapshot<bool> snapshotIsConnected) {
  92. if (snapshotIsConnected.hasData) {
  93. if (snapshotIsConnected.data) {
  94. _buttonText = Text(
  95. "Disconnect",
  96. style: _buttonTextStyle,
  97. key: Key('DisconnectButton'), // for integration test
  98. );
  99. _buttonFunction = () => bBloc.disconnect.add(null);
  100. } else {
  101. _buttonText = Text(
  102. "Connect",
  103. style: _buttonTextStyle,
  104. key: Key('ConnectButton'), // for integration test
  105. );
  106. _buttonFunction = () => bBloc.scan.add(null);
  107. }
  108. return Center(
  109. child: OutlineButton(
  110. key: Key('ConnectionButton'), // for integration test
  111. borderSide: BorderSide(color: _primaryColor),
  112. shape: BeveledRectangleBorder(),
  113. onPressed: _buttonFunction,
  114. child: _buttonText,
  115. ),
  116. );
  117. } else
  118. return Container();
  119. });
  120. }
  121. }