Sobes.tech
Middle

Kā Flutter pielāgojas iOS operētājsistēmai?

sobes.tech AI

Atbilde no AI

Flutter izmanto Skia dzinēju UI attēlošanai, kas tieši sadarbojas ar GPU. Nav izmantotas iOS nativās sastāvdaļas (pogas, ritināšanas utt.). To izskatu un uzvedību imitē Flutter widgeti, kas stilizēti atbilstoši Human Interface Guidelines.

Piemēram, iOS stila pogas izveidei tiek izmantots CupertinoButton:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class MyCupertinoButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CupertinoButton(
      child: Text('Nospied mani'),
      onPressed: () {
        // Darbība, kad nospiež
      },
    );
  }
}

Flutter sadarbojas ar nativajām API, izmantojot platformas kanālus (Platform Channels). Tas ļauj izsaukt iOS nativās funkcijas no Dart un otrādi.

import 'package:flutter/services.dart';

class NativeApi {
  static const platform = MethodChannel('com.example.myapp/battery'); // Kanāla nosaukums

  Future<String> getBatteryLevel() async {
    try {
      final String result = await platform.invokeMethod('getBatteryLevel'); // Nativās metodes izsaukšana
      return 'Akumulatora līmenis: $result%.';
    } on PlatformException catch (e) {
      return "Neizdevās iegūt akumulatora līmeni: '${e.message}'.";
    }
  }
}

iOS pusē nativais kods apstrādā izsaukumus caur MethodChannel.

// Swift kods iOS projektā
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let batteryChannel = FlutterMethodChannel(name: "com.example.myapp/battery",
                                              binaryMessenger: controller.binaryMessenger)
    batteryChannel.setMethodCallHandler({
      (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
      guard call.method == "getBatteryLevel" else {
        result(FlutterMethodNotImplemented)
        return
      }
      self.receiveBatteryLevel(result: result)
    })

    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

  private func receiveBatteryLevel(result: FlutterResult) {
    let device = UIDevice.current
    device.isBatteryMonitoringEnabled = true
    if device.batteryState == .unknown {
      result(FlutterError(code: "UNAVAILABLE",
                          message: "Akumulatora līmenis nav pieejams.",
                          details: nil))
    } else {
      result(Int(device.batteryLevel * 100))
    }
  }
}

Lai pielāgotu dizainu, tiek izmantoti cupertino bibliotēkas widgeti, kas imitē iOS stilu. Tāpat piekļuve specifiskām iOS funkcijām tiek nodrošināta ar spraudņiem. Lietojumprogrammas būvniecība iOS notiek caur Xcode. Flutter ģenerē iOS projektu, kas pēc tam tiek kompilēts par nativu izpildāmo failu (.ipa).