text
stringlengths
1
474
#include <memory><code_end>
Edit the FlutterWindow::OnCreate method and create
a flutter::MethodChannel tied to the channel name
samples.flutter.dev/battery:
<code_start>bool FlutterWindow::OnCreate() {
// ...
RegisterPlugins(flutter_controller_->engine());
flutter::MethodChannel<> channel(
flutter_controller_->engine()->messenger(), "samples.flutter.dev/battery",
&flutter::StandardMethodCodec::GetInstance());
channel.SetMethodCallHandler(
[](const flutter::MethodCall<>& call,
std::unique_ptr<flutter::MethodResult<>> result) {
// TODO
});
SetChildContent(flutter_controller_->view()->GetNativeWindow());
return true;
}<code_end>
Next, add the C++ code that uses the Windows battery APIs to
retrieve the battery level. This code is exactly the same as
you would write in a native Windows application.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(),
so test for that in the call argument.
The implementation of this platform method calls