Flutter TV应用的开发尝试

Flutter TV应用的开发尝试


Flutter主要是进行移动应用开发外,最近尝试了下Flutter开发TV应用。虽然写出来了,效果也还可以,体验流畅,自动适配。不过开发成本还是挺高的,按键监听、焦点处理和焦点框处理比较麻烦,由于Google官方并没有推出Flutter TV应用的SDK,所以暂时还是不要用Flutter编写TV应用了,使用原生leanback或者B/S结构开发吧,等官方推出后可以继续尝试对比使用。接下来,就分享下其中的技术点。本文将主要介绍:

  • Flutter TV应用开发主要难点
  • Flutter TV应用开发按键监听
  • Flutter TV应用开发焦点处理
  • Flutter TV应用开发焦点框效果处理

开发的运行效果图

由于Google官方并没有退出TV版Flutter SDK,所以用Flutter尝试编写TV应用,主要是焦点框和焦点的处理,其他的和手机应用差别不大。按键监听、焦点框和焦点处理比较麻烦,所以Flutter的TV应用开发还不成熟,体验还不错,很流畅,开发成本比较高,还是用原生leanback开发可能要快一些。

Fast to Study Flutter And Dart. QQ群:979966470

下面为效果图:

下面为效果图:

运行效果

运行效果

运行效果

手机上也可以自动适配:

手机端运行效果

运行视频:https://github.com/flutteranddart/flutterTV/blob/master/device.webm

APK下载体验地址:https://github.com/flutteranddart/flutterTV/blob/master/app-release.apk?raw=true

开发的主要难点

其实其他地方和Flutter开发移动应用基本没区别,主要就是按键监听、焦点框和焦点处理。原生Android的控件就默认有焦点的处理属性,直接配置使用即可。

1
2
3
4
//焦点处理
android:focusable="true"
//触摸模式下是否可以点击,可选可不选
android:focusableInTouchMode="true"

Flutter开发TV应用就要自己处理按键监听、焦点和焦点框了,比较的麻烦,处理好了这几个问题,开发起来也就没太大难度了。

Flutter TV应用开发按键监听

Flutter Widget能够监听到我们的遥控器或者手机端的按键事件的前提是这个Widget已经获取了焦点才可以。获取焦点后面会讲到,这里暂时不提了。
按键监听需要使用RawKeyboardListener这个Widget,构造方法如下:

1
2
3
4
5
6
const RawKeyboardListener({
Key key,
@required this.focusNode,//焦点结点
@required this.onKey,//按键接收处理事件
@required this.child,//接收焦点的子控件
})

很简单给个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
FocusNode focusNode0 = FocusNode();

... ...

RawKeyboardListener(
focusNode: focusNode0,
child: Container(
decoration: getCircleDecoration(color0),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_tv.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
if (event is RawKeyDownEvent && event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid = rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //KEY_DOWN
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode4);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode1);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
)

这样就实现了我们的Card Widget监听我们的按键事件,遥控器、手机的按键都能监听到。

Flutter TV应用开发焦点处理

Flutter TV的Widget获取焦点的处理通过FocusScope这个Widget处理。
主动获取焦点代码如下:

1
2
3
4
5
6
FocusNode focusNode0 = FocusNode();
... ...
//主动获取焦点
FocusScope.of(context).requestFocus(focusNode0);
//自动获取焦点
FocusScope.of(context).autofocus(focusNode0);

这样就可以了进行焦点获取处理了。FocusNode这个类也很重要,负责监听焦点的工作。

Flutter TV应用开发焦点框效果处理

有了焦点、按键事件监听,剩下的就是选中的焦点框效果的实现了,主要原理这里使用的是用边框,然后动态设置边框颜色就实现了焦点框选中显示,移走不显示的效果。例如选中后焦点框颜色设置为黄色、未选中时就设置为透明色,通过setState({…})进行刷新页面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  FocusNode focusNode0 = FocusNode();
...

//改变颜色状态,刷新页面
_changeColor0() {
setState(() {
if (focusNode0.hasFocus) {
color0 = Colors.orange;
} else {
color0 = Colors.transparent;
}
});
}

...
//为FocusNode添加监听事件方法
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
focusNode0.addListener(_changeColor0);
}
...
//销毁时要注销监听
@override
void dispose() {
super.dispose();
focusNode0.removeListener(_changeColor0);
focusNode0.dispose();
}

RawKeyboardListener(
//焦点监听
focusNode: focusNode0,
child: Container(
//这里就是设置焦点框效果的地方
decoration: getCircleDecoration(color0),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_tv.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
print('监听');
if (event is RawKeyDownEvent && event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid = rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //KEY_DOWN
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode4);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode1);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
);

...
//焦点框颜色变换
Decoration getCircleDecoration(Color colors) {
return BoxDecoration(
border: Border.all(width: borderWidth, color: colors),
shape: BoxShape.circle);
}

扩充一点,如果你想获取某个Widget或者布局的宽高信息,可以通过如下代码获取:

1
2
3
4
5
6
7
8
9
10
11
//这里的context为你要获取宽高信息的Widget的context
RenderObject renderObject = context.findRenderObject();
...
//这个context可以通过设置key来进行获取
GlobalKey _bodyKey = new GlobalKey();
...
Widget body1() {
return Expanded(
key: _bodyKey,
child: Container(
...

我们需要在页面渲染完毕后获取Widget的宽高信息,所以还要监听页面渲染刷新完毕的事件,用with WidgetsBindingObserver来实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
...
@override
void initState() {
//注册监听
WidgetsBinding.instance.addObserver(this);
super.initState();
}

@override
void dispose() {
super.dispose();
//取消监听
WidgetsBinding.instance.removeObserver(this);
}

@override
void didChangeDependencies() {
WidgetsBinding.instance.addPostFrameCallback(_onAfterRendering);
super.didChangeDependencies();
}

@override
void didUpdateWidget(Widget oldWidget) {
WidgetsBinding.instance.addPostFrameCallback(_onAfterRendering);
super.didUpdateWidget(oldWidget);
}

void _onAfterRendering(Duration timeStamp) {
//这里编写获取元素大小和位置的方法
RenderObject renderObject = context.findRenderObject();
Size size = renderObject.paintBounds.size;
print('onAfterRendering:');
print(size.height);
//获取对应key的context所在的Widget的宽高信息
RenderObject _renderObject = _bodyKey.currentContext.findRenderObject();
Size _size = _renderObject.paintBounds.size;
print('_onAfterRendering:');
print(_size.height);
bodyHeight = _size.height;
setState(() {});
}

最后给一个完整的Flutter TV的应用开发示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
/*
* @Author: Tan Dong
* @Date: 2019-03-16 12:39:05
* @Last Modified by: Tan Dong
* @Last Modified time: 2019-03-16 12:39:05
*/
import 'dart:async';

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TV Page',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blueGrey,
backgroundColor: Colors.teal,
),
routes: {
// '/home': (context) =>` VideoPlay(),
},
home: MyHomePage(title: 'TV Page'),
);
}
}

const double radius = 30;
const double padding = 2;
const double borderWidth = 2;
double itemWidth = 0;
FocusNode focusNode = null;

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
FocusNode focusNodeB0 = FocusNode();
FocusNode focusNodeB1 = FocusNode();
FocusNode focusNodeB2 = FocusNode();
FocusNode focusNodeB3 = FocusNode();
FocusNode focusNodeB4 = FocusNode();
Color colorB0 = Colors.transparent;
Color colorB1 = Colors.transparent;
Color colorB2 = Colors.transparent;
Color colorB3 = Colors.transparent;
Color colorB4 = Colors.transparent;
bool init = false;

_changeColorB0() {
setState(() {
if (focusNodeB0.hasFocus) {
colorB0 = Colors.orange;
} else {
colorB0 = Colors.transparent;
}
});
}

_changeColorB1() {
setState(() {
if (focusNodeB1.hasFocus) {
colorB1 = Colors.orange;
} else {
colorB1 = Colors.transparent;
}
});
}

_changeColorB2() {
setState(() {
if (focusNodeB2.hasFocus) {
colorB2 = Colors.orange;
} else {
colorB2 = Colors.transparent;
}
});
}

_changeColorB3() {
setState(() {
if (focusNodeB3.hasFocus) {
colorB3 = Colors.orange;
} else {
colorB3 = Colors.transparent;
}
});
}

_changeColorB4() {
setState(() {
if (focusNodeB4.hasFocus) {
colorB4 = Colors.orange;
} else {
colorB4 = Colors.transparent;
}
});
}

@override
void initState() {
WidgetsBinding.instance.addObserver(this);
// SystemChrome.setEnabledSystemUIOverlays([]);
// SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top, SystemUiOverlay.bottom]);
super.initState();
focusNodeB0.addListener(_changeColorB0);
focusNodeB1.addListener(_changeColorB1);
focusNodeB2.addListener(_changeColorB2);
focusNodeB3.addListener(_changeColorB3);
focusNodeB4.addListener(_changeColorB4);
}

@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
focusNodeB0.removeListener(_changeColorB0);
focusNodeB0.dispose();
focusNodeB1.removeListener(_changeColorB1);
focusNodeB1.dispose();
focusNodeB2.removeListener(_changeColorB2);
focusNodeB2.dispose();
focusNodeB3.removeListener(_changeColorB3);
focusNodeB3.dispose();
focusNodeB4.removeListener(_changeColorB4);
focusNodeB4.dispose();
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
itemWidth = MediaQuery.of(context).size.width / 3;
return Scaffold(
backgroundColor: Color(0xff277188),
body:
view1(), // This trailing comma makes auto-formatting nicer for build methods.
);
}

Widget view1() {
return Column(
children: <Widget>[
SizedBox(
height: 20,
),
TopWidget(),
body1(),
SizedBox(
height: 20,
),
BottomWidget(focusNodeB0),
SizedBox(
height: 20,
),
],
);
}

@override
void didChangeDependencies() {
WidgetsBinding.instance.addPostFrameCallback(_onAfterRendering);
super.didChangeDependencies();
}

@override
void didUpdateWidget(Widget oldWidget) {
WidgetsBinding.instance.addPostFrameCallback(_onAfterRendering);
super.didUpdateWidget(oldWidget);
}

void _onAfterRendering(Duration timeStamp) {
//这里编写获取元素大小和位置的方法
RenderObject renderObject = context.findRenderObject();
Size size = renderObject.paintBounds.size;
print('onAfterRendering:');
print(size.height);
RenderObject _renderObject = _bodyKey.currentContext.findRenderObject();
Size _size = _renderObject.paintBounds.size;
print('_onAfterRendering:');
print(_size.height);
bodyHeight = _size.height;
setState(() {});
// Navigator.of(context).push(MaterialPageRoute(builder: (_) {
// return PermissionSamples();
// }));
}

GlobalKey _bodyKey = new GlobalKey();
double bodyHeight = 600;
Widget image1() {
return RawKeyboardListener(
focusNode: focusNodeB0,
onKey: (event) {
if (event is RawKeyDownEvent && event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid = rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
break;
case 20: //KEY_DOWN
FocusScope.of(context).requestFocus(focusNode);
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNodeB4);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNodeB1);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
child: Expanded(
child: Container(
height: bodyHeight,
decoration: getRectangleDecoration(colorB0),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
elevation: 10,
child: ClipRRect(
child: Image.asset(
'assets/hmjz.jpg',
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
)),
);
}

Widget body1() {
return Expanded(
key: _bodyKey,
child: Container(
margin: EdgeInsets.all(5),
child: Row(
children: <Widget>[
image1(),
Expanded(
child: Column(
children: <Widget>[
RawKeyboardListener(
focusNode: focusNodeB1,
onKey: (event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
break;
case 20: //KEY_DOWN
FocusScope.of(context).requestFocus(focusNodeB2);
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNodeB0);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNodeB3);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
child: Expanded(
child: Container(
width: itemWidth,
decoration: getRectangleDecoration(colorB1),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
elevation: 10,
child: ClipRRect(
child: Image.asset(
'assets/zqc.jpg',
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
)),
),
RawKeyboardListener(
focusNode: focusNodeB2,
onKey: (event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(focusNodeB1);
break;
case 20: //KEY_DOWN
FocusScope.of(context).requestFocus(focusNode);
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNodeB0);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNodeB4);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
child: Expanded(
child: Container(
decoration: getRectangleDecoration(colorB2),
width: itemWidth,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
elevation: 10,
child: ClipRRect(
child: Image.asset(
'assets/lifeandpi.jpg',
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
)),
),
],
),
),
Expanded(
child: Column(
children: <Widget>[
RawKeyboardListener(
focusNode: focusNodeB3,
onKey: (event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
break;
case 20: //KEY_DOWN
FocusScope.of(context).requestFocus(focusNodeB4);
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNodeB1);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNodeB0);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
child: Expanded(
child: Container(
width: itemWidth,
decoration: getRectangleDecoration(colorB3),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
elevation: 10,
child: ClipRRect(
child: Image.asset(
'assets/zzx.jpg',
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
)),
),
RawKeyboardListener(
focusNode: focusNodeB4,
onKey: (event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(focusNodeB3);
break;
case 20: //KEY_DOWN
FocusScope.of(context).requestFocus(focusNode);
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNodeB2);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNodeB0);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
child: Expanded(
child: Container(
width: itemWidth,
decoration: getRectangleDecoration(colorB4),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10)),
),
elevation: 10,
child: ClipRRect(
child: Image.asset(
'assets/mgdz.jpg',
fit: BoxFit.cover,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
),
)),
),
],
),
),
],
),
),
);
}
}

class TopWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return TopWidgetState();
}
}

class TopWidgetState extends State<TopWidget> {
String time = '';
Timer timer;
@override
void initState() {
super.initState();
var now = DateTime.now();
timer = Timer.periodic(Duration(seconds: 1), (Timer timer) {
now = DateTime.now();
setState(() {
time = now.year.toString() +
"-" +
now.month.toString() +
"-" +
now.day.toString() +
" " +
now.hour.toString() +
":" +
now.minute.toString() +
":" +
now.second.toString() +
" " +
weekFormat(now.weekday);
});
});
}

@override
Widget build(BuildContext context) {
return Padding(
child: Row(
children: <Widget>[
Text(
'Page',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
Expanded(
child: Container(),
),
Text(
time,
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
],
),
padding: EdgeInsets.all(10),
);
}
}

class BottomWidget extends StatefulWidget {
FocusNode _focusNode;

BottomWidget(this._focusNode);

@override
State<StatefulWidget> createState() {
return BottomWidgetState(_focusNode);
}
}

class BottomWidgetState extends State<BottomWidget>
with WidgetsBindingObserver {
FocusNode _focusNode;
BottomWidgetState(this._focusNode);
FocusNode focusNode0 = FocusNode();
FocusNode focusNode1 = FocusNode();
FocusNode focusNode2 = FocusNode();
FocusNode focusNode3 = FocusNode();
FocusNode focusNode4 = FocusNode();

Color color0 = Colors.transparent;
Color color1 = Colors.transparent;
Color color2 = Colors.transparent;
Color color3 = Colors.transparent;
Color color4 = Colors.transparent;
bool init = false;

_changeColor0() {
setState(() {
if (focusNode0.hasFocus) {
color0 = Colors.orange;
} else {
color0 = Colors.transparent;
}
});
}

_changeColor1() {
setState(() {
if (focusNode1.hasFocus) {
color1 = Colors.orange;
} else {
color1 = Colors.transparent;
}
});
}

_changeColor2() {
setState(() {
if (focusNode2.hasFocus) {
color2 = Colors.orange;
} else {
color2 = Colors.transparent;
}
});
}

_changeColor3() {
setState(() {
if (focusNode3.hasFocus) {
color3 = Colors.orange;
} else {
color3 = Colors.transparent;
}
});
}

_changeColor4() {
setState(() {
if (focusNode4.hasFocus) {
color4 = Colors.orange;
} else {
color4 = Colors.transparent;
}
});
}

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
focusNode0.addListener(_changeColor0);
focusNode1.addListener(_changeColor1);
focusNode2.addListener(_changeColor2);
focusNode3.addListener(_changeColor3);
focusNode4.addListener(_changeColor4);
focusNode = focusNode0;
}

@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);
focusNode0.removeListener(_changeColor0);
focusNode0.dispose();
focusNode1.removeListener(_changeColor1);
focusNode1.dispose();
focusNode2.removeListener(_changeColor2);
focusNode2.dispose();
focusNode3.removeListener(_changeColor3);
focusNode3.dispose();
focusNode4.removeListener(_changeColor4);
focusNode4.dispose();
}

@override
void didChangeDependencies() {
WidgetsBinding.instance.addPostFrameCallback(_onAfterRendering);
super.didChangeDependencies();
}

@override
void didUpdateWidget(Widget oldWidget) {
WidgetsBinding.instance.addPostFrameCallback(_onAfterRendering);
super.didUpdateWidget(oldWidget);
}

void _onAfterRendering(Duration timeStamp) {
//这里编写获取元素大小和位置的方法
RenderObject renderObject = context.findRenderObject();
Size size = renderObject.paintBounds.size;
print(size.height);
}

GlobalKey _myKey = new GlobalKey();

@override
Widget build(BuildContext context) {
if (!init) {
FocusScope.of(context).requestFocus(focusNode0);
init = true;
}
RawKeyboardListener(
focusNode: focusNode0,
child: Container(
decoration: getCircleDecoration(color0),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_tv.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
print('监听');
if (event is RawKeyDownEvent && event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid = rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //KEY_DOWN
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode4);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode1);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
);
final sizeM = MediaQuery.of(context).size;
print(sizeM.width);
print(sizeM.height);
return Row(
key: _myKey,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(
width: 10,
height: 2,
),
Column(
children: <Widget>[
RawKeyboardListener(
focusNode: focusNode0,
child: Container(
decoration: getCircleDecoration(color0),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_tv.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
print('监听');
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //KEY_DOWN
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode4);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode1);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
),
Text(
'TV',
style:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
],
),
Column(
children: <Widget>[
RawKeyboardListener(
focusNode: focusNode1,
child: Container(
decoration: getCircleDecoration(color1),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_voice.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //KEY_DOWN
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode0);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode2);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
),
Text(
'Voice',
style:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
],
),
Column(
children: <Widget>[
RawKeyboardListener(
focusNode: focusNode2,
child: Container(
decoration: getCircleDecoration(color2),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_video.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //KEY_DOWN
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode1);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode3);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
),
Text(
'Video',
style:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
],
),
Column(
children: <Widget>[
RawKeyboardListener(
focusNode: focusNode3,
child: Container(
decoration: getCircleDecoration(color3),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_phone.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode2);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode4);
break;
case 23: //KEY_CENTER
Navigator.of(context).pushNamed('/home');
break;
default:
break;
}
}
},
),
Text(
'Phone',
style:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
],
),
Column(
children: <Widget>[
RawKeyboardListener(
focusNode: focusNode4,
child: Container(
decoration: getCircleDecoration(color4),
child: Padding(
child: Card(
elevation: 5,
shape: CircleBorder(),
child: CircleAvatar(
child: Text(''),
backgroundImage: AssetImage("assets/icon_pad.png"),
radius: radius,
),
),
padding: EdgeInsets.all(padding),
),
),
onKey: (RawKeyEvent event) {
if (event is RawKeyDownEvent &&
event.data is RawKeyEventDataAndroid) {
RawKeyDownEvent rawKeyDownEvent = event;
RawKeyEventDataAndroid rawKeyEventDataAndroid =
rawKeyDownEvent.data;
print("keyCode: ${rawKeyEventDataAndroid.keyCode}");
switch (rawKeyEventDataAndroid.keyCode) {
case 19: //KEY_UP
FocusScope.of(context).requestFocus(_focusNode);
break;
case 20: //KEY_DOWN
break;
case 21: //KEY_LEFT
FocusScope.of(context).requestFocus(focusNode3);
break;
case 22: //KEY_RIGHT
FocusScope.of(context).requestFocus(focusNode0);
break;
case 23: //KEY_CENTER
break;
default:
break;
}
}
},
),
Text(
'Pad',
style:
TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
),
],
),
SizedBox(
width: 10,
height: 2,
),
],
);
}
}

Decoration getCircleDecoration(Color colors) {
return BoxDecoration(
border: Border.all(width: borderWidth, color: colors),
shape: BoxShape.circle);
}

Decoration getRectangleDecoration(Color colors) {
List<BoxShadow> boxShadows = List();
boxShadows.add(BoxShadow(color: Colors.orange[200]));
boxShadows.add(BoxShadow(color: Colors.yellow[100]));
boxShadows.add(BoxShadow(color: Colors.yellow[300]));
List<Color> colorsGradient = List();
colorsGradient.add(Colors.teal);
colorsGradient.add(Colors.teal[200]);

return BoxDecoration(
border: Border.all(width: borderWidth, color: colors),
borderRadius: BorderRadius.all(Radius.circular(10)),
shape: BoxShape.rectangle,
);
}

String weekFormat(int week) {
switch (week) {
case 1:
return '星期一';
break;
case 2:
return '星期二';
break;
case 3:
return '星期三';
break;
case 4:
return '星期四';
break;
case 5:
return '星期五';
break;
case 6:
return '星期六';
break;
case 7:
return '星期日';
break;
}
}

关于Flutter TV开发就讲解这么多。