Anuj-Panthri commited on
Commit
512a583
·
1 Parent(s): f49e1ff

managed aspect ratio problem

Browse files
Files changed (1) hide show
  1. client/lib/HomeScreen.dart +79 -47
client/lib/HomeScreen.dart CHANGED
@@ -55,6 +55,8 @@ class HomeScreenState extends State<HomeScreen>{
55
  final int touchRefreshRate = (1000/30).toInt(); // 30 fps
56
  // final int touchRefreshRate = (1000/1).toInt(); // 30 fps
57
 
 
 
58
  @override
59
  void initState(){
60
  super.initState();
@@ -71,12 +73,12 @@ class HomeScreenState extends State<HomeScreen>{
71
 
72
  Map data={
73
  "user": {
74
- "username": "anne",
75
  "id": "string",
76
  },
77
  "position": {
78
- "x": 200.0,
79
- "y": 200.0,
80
  },
81
  "color": "ff2244bb", // Hex value.
82
  "intensity": 1, // Vibration intensity.
@@ -84,6 +86,15 @@ class HomeScreenState extends State<HomeScreen>{
84
 
85
  othersTouchPoints[data['user']['id']]=data;
86
 
 
 
 
 
 
 
 
 
 
87
  }
88
 
89
  @override
@@ -93,6 +104,12 @@ class HomeScreenState extends State<HomeScreen>{
93
  super.dispose();
94
  }
95
 
 
 
 
 
 
 
96
  void getPreferences() async{
97
  prefs = await SharedPreferences.getInstance();
98
 
@@ -192,9 +209,15 @@ class HomeScreenState extends State<HomeScreen>{
192
  // print(map);
193
 
194
  if(map['type']=='enabled'){ // add touch point
 
 
 
 
 
195
  // rescale point according to the screen
196
- map['position']['x']*=width;
197
- map['position']['y']*=height;
 
198
  // print(map['position']['x'].toString());
199
  // print(map['position']['y'].toString());
200
  setState(() {
@@ -314,6 +337,7 @@ class HomeScreenState extends State<HomeScreen>{
314
  Widget header(){
315
  final Color buttoncolor=Color.fromARGB(255, 70, 70, 70);
316
  return Padding(
 
317
  padding:EdgeInsets.symmetric(vertical:20),
318
  child:Row(
319
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
@@ -405,6 +429,8 @@ class HomeScreenState extends State<HomeScreen>{
405
  Duration(milliseconds: touchRefreshRate), (timer) async {
406
  // send touch to server
407
  Map data;
 
 
408
 
409
  try{
410
  await channel.ready;
@@ -420,13 +446,14 @@ class HomeScreenState extends State<HomeScreen>{
420
  "position": {
421
  // "x":20,
422
  // "y":20,
423
- "x": touchX/width, // send ratio x e.g. : x:700, width:800, then send 0.9
424
- "y": touchY/height, // send ratio y e.g. : y:400, height:800 then send 0.5
425
  },
426
  "color": profileColor, // Hex value. Default: random
427
  // "intensity"?: 1 // Vibration intensity. Default: 1
428
  };
429
-
 
430
  channel.sink.add(jsonEncode(data));
431
  }
432
  );
@@ -450,8 +477,8 @@ class HomeScreenState extends State<HomeScreen>{
450
  "position": {
451
  // "x":20,
452
  // "y":20,
453
- "x": touchX/width, // send ratio x e.g. : x:700, width:800, then send 0.9
454
- "y": touchY/height, // send ratio y e.g. : y:400, height:800 then send 0.5
455
  },
456
  "color": profileColor, // Hex value. Default: random
457
  // "intensity"?: 1 // Vibration intensity. Default: 1
@@ -562,13 +589,13 @@ class HomeScreenState extends State<HomeScreen>{
562
 
563
  // detect Collision
564
  detectTouchCollision(
565
- x:details.globalPosition.dx,
566
- y:details.globalPosition.dy,
567
  );
568
 
569
  setState(() {
570
- touchX = details.globalPosition.dx;
571
- touchY = details.globalPosition.dy;
572
  isTouching=true; // show it
573
  });
574
 
@@ -590,17 +617,17 @@ class HomeScreenState extends State<HomeScreen>{
590
 
591
  // detect Collision
592
  detectTouchCollision(
593
- x:details.globalPosition.dx,
594
- y:details.globalPosition.dy,
595
  );
596
 
597
  // print(details.localPosition);
598
  setState(() {
599
- touchX = details.globalPosition.dx;
600
- touchY = details.globalPosition.dy;
601
  });
602
  // print(profileColor);
603
- // print(details.globalPosition);
604
 
605
  }
606
 
@@ -610,8 +637,8 @@ class HomeScreenState extends State<HomeScreen>{
610
 
611
  // detect Collision end
612
  detectTouchCollision(
613
- x:0,
614
- y:0,
615
  );
616
 
617
  setState(() {
@@ -649,8 +676,35 @@ class HomeScreenState extends State<HomeScreen>{
649
  color:Color.fromARGB(255, 30, 30, 30),
650
  borderRadius: BorderRadius.circular(50),
651
  ),
652
- // width:width*0.9,
653
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
  ),
655
  ),
656
  );
@@ -659,8 +713,8 @@ class HomeScreenState extends State<HomeScreen>{
659
 
660
 
661
  Widget build(BuildContext){
662
- width=MediaQuery.of(context).size.width-(outsidepadding*2);
663
- height=MediaQuery.of(context).size.height-(outsidepadding*2);
664
 
665
  return Scaffold(
666
  // backgroundColor: Color(0xff0e0e0e),
@@ -737,28 +791,6 @@ class HomeScreenState extends State<HomeScreen>{
737
  ),
738
 
739
 
740
- // render received touch points
741
- ...othersTouchPoints.values.map<Widget>((map){
742
- return buildTouchPoint(
743
- visible:true,
744
- x:map['position']['x'],
745
- y:map['position']['y'],
746
- size:touchsize,
747
- color:colorFromHex(map['color'])!,
748
- );
749
- }).toList(),
750
-
751
-
752
- // our touch point
753
- buildTouchPoint(
754
- visible:isTouching,
755
- x:touchX,
756
- y:touchY,
757
- size:touchsize,
758
- color:colorFromHex(profileColor)!,
759
- // color:Colors.red,
760
- ),
761
-
762
  ]),
763
 
764
  );
 
55
  final int touchRefreshRate = (1000/30).toInt(); // 30 fps
56
  // final int touchRefreshRate = (1000/1).toInt(); // 30 fps
57
 
58
+ GlobalKey headerKey = GlobalKey(); // to calculate the header's height
59
+
60
  @override
61
  void initState(){
62
  super.initState();
 
73
 
74
  Map data={
75
  "user": {
76
+ "username": "enemy",
77
  "id": "string",
78
  },
79
  "position": {
80
+ "x": 0.0+touchsize/2, // top left corner
81
+ "y": 0.0+touchsize/2, // top left corner
82
  },
83
  "color": "ff2244bb", // Hex value.
84
  "intensity": 1, // Vibration intensity.
 
86
 
87
  othersTouchPoints[data['user']['id']]=data;
88
 
89
+ // just for debugging
90
+ // WidgetsBinding.instance.addPostFrameCallback((_){
91
+ // ScaffoldMessenger.of(context).hideCurrentSnackBar();
92
+ // ScaffoldMessenger.of(context).showSnackBar(
93
+ // SnackBar(
94
+ // content:Text(getHeaderHeight().toString())
95
+ // )
96
+ // );
97
+ // });
98
  }
99
 
100
  @override
 
104
  super.dispose();
105
  }
106
 
107
+ double? getHeaderHeight(){
108
+ var size=headerKey.currentContext?.size;
109
+ // print(size?.height);
110
+ return size?.height;
111
+ }
112
+
113
  void getPreferences() async{
114
  prefs = await SharedPreferences.getInstance();
115
 
 
209
  // print(map);
210
 
211
  if(map['type']=='enabled'){ // add touch point
212
+
213
+ // static width and height
214
+ double staticHeight=getHeaderHeight()!+outsidepadding*2; // this is the extra static height which change with resize
215
+ double staticWidth=outsidepadding*2; // this is the extra static height which change with resize
216
+
217
  // rescale point according to the screen
218
+ map['position']['x']*=width-staticWidth;
219
+ map['position']['y']*=height-staticHeight;
220
+
221
  // print(map['position']['x'].toString());
222
  // print(map['position']['y'].toString());
223
  setState(() {
 
337
  Widget header(){
338
  final Color buttoncolor=Color.fromARGB(255, 70, 70, 70);
339
  return Padding(
340
+ key:headerKey,
341
  padding:EdgeInsets.symmetric(vertical:20),
342
  child:Row(
343
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
 
429
  Duration(milliseconds: touchRefreshRate), (timer) async {
430
  // send touch to server
431
  Map data;
432
+ double staticHeight=getHeaderHeight()!+outsidepadding*2; // this is the extra static height which change with resize
433
+ double staticWidth=outsidepadding*2; // this is the extra static height which change with resize
434
 
435
  try{
436
  await channel.ready;
 
446
  "position": {
447
  // "x":20,
448
  // "y":20,
449
+ "x": touchX/(width-staticWidth), // send ratio x e.g. : x:700, width:800, then send 0.9
450
+ "y": touchY/(height-staticHeight), // send ratio y e.g. : y:400, height:800 then send 0.5
451
  },
452
  "color": profileColor, // Hex value. Default: random
453
  // "intensity"?: 1 // Vibration intensity. Default: 1
454
  };
455
+ // print(data['position']['x']);
456
+ // print(data['position']['y']);
457
  channel.sink.add(jsonEncode(data));
458
  }
459
  );
 
477
  "position": {
478
  // "x":20,
479
  // "y":20,
480
+ "x": 0, // send ratio x e.g. : x:700, width:800, then send 0.9
481
+ "y": 0, // send ratio y e.g. : y:400, height:800 then send 0.5
482
  },
483
  "color": profileColor, // Hex value. Default: random
484
  // "intensity"?: 1 // Vibration intensity. Default: 1
 
589
 
590
  // detect Collision
591
  detectTouchCollision(
592
+ x:details.localPosition.dx,
593
+ y:details.localPosition.dy,
594
  );
595
 
596
  setState(() {
597
+ touchX = details.localPosition.dx;
598
+ touchY = details.localPosition.dy;
599
  isTouching=true; // show it
600
  });
601
 
 
617
 
618
  // detect Collision
619
  detectTouchCollision(
620
+ x:details.localPosition.dx,
621
+ y:details.localPosition.dy,
622
  );
623
 
624
  // print(details.localPosition);
625
  setState(() {
626
+ touchX = details.localPosition.dx;
627
+ touchY = details.localPosition.dy;
628
  });
629
  // print(profileColor);
630
+ // print(details.localPosition);
631
 
632
  }
633
 
 
637
 
638
  // detect Collision end
639
  detectTouchCollision(
640
+ x:-1000,
641
+ y:-1000,
642
  );
643
 
644
  setState(() {
 
676
  color:Color.fromARGB(255, 30, 30, 30),
677
  borderRadius: BorderRadius.circular(50),
678
  ),
679
+ clipBehavior: Clip.hardEdge,
680
+ child:Stack(
681
+ clipBehavior: Clip.hardEdge,
682
+ children: [
683
+
684
+ // render received touch points
685
+ ...othersTouchPoints.values.map<Widget>((map){
686
+ return buildTouchPoint(
687
+ visible:true,
688
+ x:map['position']['x'],
689
+ y:map['position']['y'],
690
+ size:touchsize,
691
+ color:colorFromHex(map['color'])!,
692
+ );
693
+ }).toList(),
694
+
695
+
696
+ // our touch point
697
+ buildTouchPoint(
698
+ visible:isTouching,
699
+ x:touchX,
700
+ y:touchY,
701
+ size:touchsize,
702
+ color:colorFromHex(profileColor)!,
703
+ // color:Colors.red,
704
+ ),
705
+
706
+ ],
707
+ ),
708
  ),
709
  ),
710
  );
 
713
 
714
 
715
  Widget build(BuildContext){
716
+ width=MediaQuery.of(context).size.width;
717
+ height=MediaQuery.of(context).size.height;
718
 
719
  return Scaffold(
720
  // backgroundColor: Color(0xff0e0e0e),
 
791
  ),
792
 
793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
794
  ]),
795
 
796
  );