text stringlengths 1 372 |
|---|
add the following as a new function at the top of |
flutter_window.cpp just after the #include section: |
<code_start> |
static int GetBatteryLevel() { |
SYSTEM_POWER_STATUS status; |
if (getsystempowerstatus(&status) == 0 || status.BatteryLifePercent == 255) { |
return -1; |
} |
return status.BatteryLifePercent; |
} |
<code_end> |
finally, complete the setMethodCallHandler() method added earlier. |
you need to handle a single platform method, getBatteryLevel(), |
so test for that in the call argument. |
the implementation of this platform method calls |
the windows code written in the previous step. if an unknown method |
is called, report that instead. |
remove the following code: |
<code_start> |
channel.SetMethodCallHandler( |
[](const flutter::MethodCall<>& call, |
std::unique_ptr<flutter::MethodResult<>> result) { |
// TODO |
}); |
<code_end> |
and replace with the following: |
<code_start> |
channel.SetMethodCallHandler( |
[](const flutter::MethodCall<>& call, |
std::unique_ptr<flutter::MethodResult<>> result) { |
if (call.method_name() == "getbatterylevel") { |
int battery_level = GetBatteryLevel(); |
if (battery_level != -1) { |
result->Success(battery_level); |
} else { |
result->Error("UNAVAILABLE", "battery level not available."); |
} |
} else { |
result->NotImplemented(); |
} |
}); |
<code_end> |
you should now be able to run the application on windows. |
if your device doesn’t have a battery, |
it displays ‘battery level not available’. |
<topic_end> |
<topic_start> |
step 6: add a macOS platform-specific implementation |
start by opening the macOS host portion of your flutter app in xcode: |
start xcode. |
select the menu item file > open…. |
navigate to the directory holding your flutter app, and select the macos |
folder inside it. click OK. |
add the swift implementation of the platform channel method: |
expand runner > runner in the project navigator. |
open the file MainFlutterWindow.swift located under runner > runner |
in the project navigator. |
first, add the necessary import to the top of the file, just after |
import FlutterMacOS: |
<code_start> |
import IOKit.ps |
<code_end> |
create a FlutterMethodChannel tied to the channel name |
samples.flutter.dev/battery in the awakeFromNib method: |
<code_start> |
override func awakeFromNib() { |
// ... |
self.setFrame(windowFrame, display: true) |
let batteryChannel = FlutterMethodChannel( |
name: "samples.flutter.dev/battery", |
binaryMessenger: flutterViewController.engine.binaryMessenger) |
batteryChannel.setMethodCallHandler { (call, result) in |
// this method is invoked on the UI thread. |
// handle battery messages. |
} |
RegisterGeneratedPlugins(registry: flutterViewController) |
super.awakeFromNib() |
} |
} |
<code_end> |
next, add the macOS swift code that uses the IOKit battery APIs to retrieve |
the battery level. this code is exactly the same as you |
would write in a native macOS app. |
add the following as a new method at the bottom of MainFlutterWindow.swift: |
<code_start> |
private func getBatteryLevel() -> int? { |
let info = IOPSCopyPowerSourcesInfo().takeRetainedValue() |
let sources: Array<CFTypeRef> = IOPSCopyPowerSourcesList(info).takeRetainedValue() as array |
if let source = sources.first { |
let description = |
IOPSGetPowerSourceDescription(info, source).takeUnretainedValue() as! [string: AnyObject] |
if let level = description[kIOPSCurrentCapacityKey] as? int { |
return level |
} |
} |
return nil |
} |
<code_end> |
finally, complete the setMethodCallHandler method added earlier. |
you need to handle a single platform method, getBatteryLevel(), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.