b.dart内のonPressedの処理内でa.dart内Deviceクラスのtestメソッドを実行したいのですが、
どうすればよいのかわかりません。
DeviceクラスはBlueModelクラス内で宣言されています。
a.dart
dart
1class Device extends BluetoothDevice { 2 3 Device({this.connection, this.id, this.name}) 4 : super(id: id, name: name); 5 6 void test(){ 7 } 8} 9 10class BlueModel extends Model { 11 FlutterBlue _blue = FlutterBlue.instance; 12 Map<DeviceIdentifier, Device> _devices = {}; 13 Device currentDevice; 14 TabController tabController; 15 16 BlueModel({ this.tabController}) : assert(tabController != null); 17 18 bool _loading = false; 19 20 Map<DeviceIdentifier, Device> get devices => _devices; 21 22 static BlueModel of(BuildContext context) => 23 ScopedModel.of<BlueModel>(context); 24 25 bool get loading => _loading; 26 27 Future<void> scan() async { 28 _loading = true; 29 notifyListeners(); 30 31 var scannedResult = Map<DeviceIdentifier, Device>(); 32 33 await for (ScanResult result in _blue.scan(timeout: Duration(seconds: 2))) { 34 var dev = result.device; 35 if (dev.name.contains('ZEAL')) { 36 scannedResult[dev.id] = Device(id: dev.id, name: dev.name); 37 } 38 } 39 40 _devices.forEach((id, dev) { 41 if (scannedResult[id] == null && !_devices[id].connected) 42 _devices.remove(id); 43 }); 44 45 scannedResult.forEach((id, dev) { 46 if (_devices[id] == null) _devices[id] = dev; 47 }); 48 49 _loading = false; 50 notifyListeners(); 51 } 52 53 void connect(Device d) async { 54 d.currentState = BluetoothDeviceState.connecting; 55 notifyListeners(); 56 var deviceState = await d.state; 57 print('Device State: $deviceState'); 58 if (_devices[d.id].connection == null && 59 deviceState != BluetoothDeviceState.connected) { 60 _devices[d.id].connection = _blue.connect(d).listen((s) { 61 print('Current State: ${d.currentState}'); 62 if (s == BluetoothDeviceState.connected) { 63 d.initiate(notifyListeners); 64 } 65 }); 66 } else { 67 d.currentState = BluetoothDeviceState.connecting; 68 notifyListeners(); 69 await Future.delayed(Duration(milliseconds: 500)); 70 d.currentState = BluetoothDeviceState.disconnected; 71 notifyListeners(); 72 } 73 } 74 75 void disconect(Device d) { 76 d.dispose(); 77 if (d.id == currentDevice.id) currentDevice = null; 78 notifyListeners(); 79 } 80 81 void select(Device d) async { 82 print('Checking...'); 83 84 if (!d.connected) return; 85 86 if (currentDevice != null && d.id != currentDevice.id) { 87 print('Stop current device'); 88 currentDevice.stopReadWeight(); 89 currentDevice.stopListen(); 90 } 91 92 print('Checked!!!'); 93 94 if (!d.listening) { 95 print('Start listening'); 96 currentDevice = d; 97 currentDevice.startListen(notifyListeners); 98 currentDevice.startReadWeight(); 99 } 100 101 tabController.animateTo(1); 102 } 103}
b.dart
dart
1class BasicFunctions extends StatelessWidget { 2 3 Widget build(BuildContext context) { 4 return Wrap( 5 alignment: WrapAlignment.spaceAround, 6 runSpacing: 20.0, 7 children: <Widget>[ 8 ControlButton( 9 text: 'aaa', 10 onPressed: () { 11 //このonPressedの中でDeviceクラスのtestメソッドを実行したい 12 }, 13 ), 14 ], 15 ); 16 } 17}
あなたの回答
tips
プレビュー