flutter 电视开发

1 focus的使用 和keyEvent的捕获

 

在flutter 中 有一个Focus 类。要在widget 创建的时候,数组长度为需要焦点的widget数量。

@overridevoid initState() {

  // TODO: implement initState  super.initState();  

  for (int i = 0; i < 3; i++) {

    FocusNode focus = FocusNode();  

    focusNodes.add(focus);  }

  }

在widget dispose的时候释放

 

@overridevoid dispose() {

  for (int i = 0; i < 3; i++) {

    focusNodes[i].dispose();  }

  super.dispose();

}

 


RawKeyboardListener

 

focusNode: focusNodes[0],

onKey: (RawKeyEvent event) {

// 判断是否属于android 

  if (event is RawKeyDownEvent &&

      event.data is RawKeyEventDataAndroid) {

    RawKeyDownEvent rawKeyDownEvent = event;    

    RawKeyEventDataAndroid rawKeyEventDataAndroid =  rawKeyDownEvent.data;  //

   // 通过对keyCode值的判断
   switch (rawKeyEventDataAndroid.keyCode) {

}

// 更新ui

setState(() {});

}

 


child: new RaisedButton(

  child: new Container(

      alignment: Alignment.center,      width: 300,      child: new Text('专网普通影片')),  

// 判断当前widget 是否有焦点

     color: focusNodes[0].hasFocus ? Colors.red : Colors.grey,  onPressed: () {  

  },),

 

2 和原生android 通信

 

android 原生部分注册这个两个类

FlutterEventChannel.registerWith(this.registrarFor(FlutterEventChannel.CHANNEL));//注册FlutterMethodCallHandler.registerWith(this.registrarFor(FlutterMethodCallHandler.CHANNEL));//注册

EventChannel.StreamHandler

public class FlutterEventChannel implements EventChannel.StreamHandler {

    public static String CHANNEL = "com.youku.player.demo/event";    

   static EventChannel channel;    

   private Context context;  

   private FlutterEventChannel(Context context){

        this.context = context;    }

    public static void registerWith(PluginRegistry.Registrar registrar) {

        channel = new EventChannel(registrar.messenger(), CHANNEL);        

       FlutterEventChannel instance = new FlutterEventChannel(registrar.context());                channel.setStreamHandler(instance);  

 }

    @Override    public void onListen(Object o, EventChannel.EventSink eventSink) {

        // 需要保存这个eventSink 对象。      

       // eventSink.success(123456);    }

    @Override    public void onCancel(Object o) {

        Log.e("FlutterPluginCounter", "FlutterPluginCounter:onCancel");    }

}

MethodChannel

public class FlutterMethodCallHandler implements MethodChannel.MethodCallHandler {

    private final String TAG = FlutterMethodCallHandler.class.getCanonicalName();    

    private YoukuVideoPlayer mYoukuVideoPlayer;    

   public static String CHANNEL = "com.youku.player.demo/method_call";  

   static MethodChannel channel;    private Application context;    private FlutterMethodCallHandler(Application context){

        this.context = context;    }

    @Override    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {

        if(methodCall.method.equals("play")){

            mYoukuVideoPlayer.play();        }

        }

    }

    public static void registerWith(PluginRegistry.Registrar registrar){

        channel = new MethodChannel(registrar.messenger(),CHANNEL);      

         FlutterMethodCallHandler instantce =      new FlutterMethodCallHandler(registrar.activity().getApplication());              channel.setMethodCallHandler(instantce);  

 }

 

flutter调用android 部分方式

static const jumpPlugin = const MethodChannel("com.youku.player.demo/method_call");

// 异步调用
login() async{

  Map<String,String> params = {"tuid":"987654321","pid":"0ee876a3655e248e",    "clientId":"xC52PWqFGtYudyXy"};  

  jumpPlugin.invokeMethod("login",params);

}

 

android 原生通知flutter部分


static const receiverPlugin = const EventChannel("com.youku.player.demo.flutter_event");

@overridevoid initState() {

   super.initState();   if( subscription == null){

     subscription = receiverPlugin.receiveBroadcastStream().listen(getMessage,onError: onError);  

   }

}

   

3 使用原生android view

CustomViewPlugin.registerWith(this);//注册

public class CustomYoukuScreenView  implements PlatformView {

    private final YoukuScreenView youkuScreenView;    TextView textView;
   public CustomYoukuScreenView(Context context, BinaryMessenger messenger, int id, Object args) {

        this.youkuScreenView = new YoukuScreenView(context);    }

    @Override    public View getView() {

        return youkuScreenView;    }

    @Override    public void dispose() {

    }

}


public class CustomViewPlugin {

    public static void registerWith(PluginRegistry registry){

        final String key = CustomViewPlugin.class.getCanonicalName();        if(registry.hasPlugin(key)) return;        PluginRegistry.Registrar registrar = registry.registrarFor(key);        

// 组件名称        registrar.platformViewRegistry().registerViewFactory("com.youku.player.demo/youkuScreenView",                new CustomViewFactory(registrar.messenger()));    }

}

 


public class CustomViewFactory extends PlatformViewFactory {

    private final BinaryMessenger messenger;
   public CustomViewFactory(BinaryMessenger messenger) {

        super(StandardMessageCodec.INSTANCE);        this.messenger = messenger;    }

    @Override    public PlatformView create(Context context, int id, Object args) {

        Map<String, Object> params = (Map<String, Object>) args;        return new CustomYoukuScreenView(context, messenger, id, params);    }

}

// dart 中使用

widget(

child: AndroidView(viewType: "com.youku.player.demo/youkuScreenView"),

  • 使用 AndroidView 时,Android API > 20
  • 使用 AndroidView 时均需要有界父类;
  • 官网明确提醒,AndroidView 方式代价较大,由于是 GPU -> CPU -> GPU 有明显的性能缺陷,尽量避免使用;
  • 测试过程中热重载无效,每次均需重新编译;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值