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.

ScanResultBottomSheet.dart 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import 'package:flutter_blue/flutter_blue.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:touch_demonstrator/src/blocs/BlocProvider.dart';
  4. class Modal {
  5. _buildScanResult(
  6. BuildContext context, Map<DeviceIdentifier, ScanResult> scanResults) {
  7. final bBloc = BlocProvider.of(context).bluetoothBlocGetter;
  8. return scanResults.values
  9. .where((result) => result.advertisementData.localName
  10. .contains('Touchpad Demonstrator'))
  11. .map((result) => ScanResultTile(
  12. result: result,
  13. onTapCallback: () {
  14. bBloc.connect.add(result);
  15. print('Connect to ${result.device.name} pressed!');
  16. Navigator.pop(context);
  17. }))
  18. .toList();
  19. }
  20. _buildHeaderBottomSheet(BuildContext context) {
  21. return Container(
  22. color: Theme.of(context).primaryColor,
  23. child: ListTile(
  24. title: Center(
  25. child: Text(
  26. "CHOOSE TOUCHPAD",
  27. style: TextStyle(color: Colors.white),
  28. ),
  29. ),
  30. ),
  31. );
  32. }
  33. connectBottomSheet(
  34. BuildContext context, Map<DeviceIdentifier, ScanResult> scanResults) {
  35. print('Modal Sheet - Result Count: ${scanResults.length}');
  36. var _bottomSheetElements = List<Widget>();
  37. _bottomSheetElements.add(_buildHeaderBottomSheet(context));
  38. _bottomSheetElements.addAll(_buildScanResult(context, scanResults));
  39. showModalBottomSheet(
  40. context: context,
  41. builder: (BuildContext context) {
  42. return Container(
  43. child: Padding(
  44. padding: EdgeInsets.all(0.0),
  45. child: ListView(
  46. shrinkWrap: true, children: _bottomSheetElements)));
  47. });
  48. }
  49. }
  50. class ScanResultTile extends StatelessWidget {
  51. final ScanResult result;
  52. final VoidCallback onTapCallback;
  53. const ScanResultTile({Key key, this.result, this.onTapCallback})
  54. : super(key: key);
  55. _buildTitle(BuildContext context) {
  56. var textOfTile;
  57. (result.device.name.length > 0)
  58. ? textOfTile = result.device.name
  59. : textOfTile = result.device.id.toString();
  60. return Flexible(
  61. child: Container(
  62. child: Text(
  63. textOfTile,
  64. overflow: TextOverflow.ellipsis,
  65. style: TextStyle(fontWeight: FontWeight.bold),
  66. )));
  67. }
  68. _buildConnectButton(BuildContext context) {
  69. return OutlineButton(
  70. borderSide: BorderSide(color: Theme.of(context).primaryColor),
  71. shape: BeveledRectangleBorder(),
  72. onPressed: (result.advertisementData.connectable) ? onTapCallback : null,
  73. child: Text('CONNECT'),
  74. ); // Name of devices
  75. }
  76. @override
  77. Widget build(BuildContext context) {
  78. return Container(
  79. child: Padding(
  80. padding: const EdgeInsets.all(8.0),
  81. child: Row(
  82. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  83. children: <Widget>[
  84. _buildTitle(context),
  85. _buildConnectButton(context),
  86. ],
  87. ),
  88. ),
  89. );
  90. }
  91. }