import 'package:flutter_blue/flutter_blue.dart'; import 'package:flutter/material.dart'; import 'package:touch_demonstrator/src/blocs/BlocProvider.dart'; class Modal { _buildScanResult( BuildContext context, Map scanResults) { final bBloc = BlocProvider.of(context).bluetoothBlocGetter; return scanResults.values .where((result) => result.advertisementData.localName .contains('Touchpad Demonstrator')) .map((result) => ScanResultTile( result: result, onTapCallback: () { bBloc.connect.add(result); print('Connect to ${result.device.name} pressed!'); Navigator.pop(context); })) .toList(); } _buildHeaderBottomSheet(BuildContext context) { return Container( color: Theme.of(context).primaryColor, child: ListTile( title: Center( child: Text( "CHOOSE TOUCHPAD", style: TextStyle(color: Colors.white), ), ), ), ); } connectBottomSheet( BuildContext context, Map scanResults) { print('Modal Sheet - Result Count: ${scanResults.length}'); var _bottomSheetElements = List(); _bottomSheetElements.add(_buildHeaderBottomSheet(context)); _bottomSheetElements.addAll(_buildScanResult(context, scanResults)); showModalBottomSheet( context: context, builder: (BuildContext context) { return Container( child: Padding( padding: EdgeInsets.all(0.0), child: ListView( shrinkWrap: true, children: _bottomSheetElements))); }); } } class ScanResultTile extends StatelessWidget { final ScanResult result; final VoidCallback onTapCallback; const ScanResultTile({Key key, this.result, this.onTapCallback}) : super(key: key); _buildTitle(BuildContext context) { var textOfTile; (result.device.name.length > 0) ? textOfTile = result.device.name : textOfTile = result.device.id.toString(); return Flexible( child: Container( child: Text( textOfTile, overflow: TextOverflow.ellipsis, style: TextStyle(fontWeight: FontWeight.bold), ))); } _buildConnectButton(BuildContext context) { return OutlineButton( borderSide: BorderSide(color: Theme.of(context).primaryColor), shape: BeveledRectangleBorder(), onPressed: (result.advertisementData.connectable) ? onTapCallback : null, child: Text('CONNECT'), ); // Name of devices } @override Widget build(BuildContext context) { return Container( child: Padding( padding: const EdgeInsets.all(8.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ _buildTitle(context), _buildConnectButton(context), ], ), ), ); } }