summaryrefslogtreecommitdiffstats
path: root/lib/vehicle_signal/initial_socket_connection.dart
blob: 6b9305ad358a81fcea881eadac34f5da2cba6872 (plain)
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
// SPDX-License-Identifier: Apache-2.0

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_cluster_dashboard/vehicle_signal/listen_stream.dart';
import 'package:flutter_cluster_dashboard/vehicle_signal/vehicle_signal_config.dart';

class InitialScreen extends ConsumerWidget {
  InitialScreen({Key? key, required this.client}) : super(key: key);
  final HttpClient client;
  late WebSocket socket;

  @override
  Widget build(BuildContext context, ref) {
    final sockConnect = ref.watch(sockConnectprovider(client));

    return sockConnect.when(
      data: (socket) {
        this.socket = socket;
        this.socket.pingInterval = const Duration(seconds: 2);
        return OnBoardingPage(client: client, socket: this.socket);
      },
      error: (e, stk) {
        print(e);
        Future.delayed(const Duration(milliseconds: 700), (() {
          ref.refresh(sockConnectprovider(client));
        }));
        return const Scaffold(
          backgroundColor: Colors.black,
          body: NoticeWidget(
            assetImageName: "images/server_error.png",
            text1: "Server Unavailable",
            text2: "Retrying to conncect!",
          ),
        );
      },
      loading: () => const Scaffold(
        backgroundColor: Colors.black,
        body: NoticeWidget(
          assetImageName: "images/server.png",
          text1: "Hi!",
          text2: "Connecting...!",
        ),
      ),
    );
  }
}

class NoticeWidget extends StatelessWidget {
  const NoticeWidget({
    Key? key,
    required this.assetImageName,
    required this.text1,
    required this.text2,
    this.loadingColor,
  }) : super(key: key);

  final String assetImageName;
  final String text1;
  final String text2;
  final Color? loadingColor;

  @override
  Widget build(BuildContext context) {
    return LoadingContainer(
        child: Flex(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      crossAxisAlignment: CrossAxisAlignment.center,
      direction: Axis.vertical,
      children: [
        Flexible(
          child: SizedBox(
            height: 100,
            child:
                Image(image: AssetImage(assetImageName), fit: BoxFit.fitWidth),
          ),
        ),
        Flexible(
            child: Text(text1,
                style: const TextStyle(fontWeight: FontWeight.bold))),
        Flexible(
            child: Text(text2,
                style: const TextStyle(fontWeight: FontWeight.bold))),
        Flexible(
          child: Padding(
            padding: const EdgeInsets.fromLTRB(25, 0, 25, 20),
            child: LinearProgressIndicator(color: loadingColor ?? Colors.red),
          ),
        )
      ],
    ));
  }
}

class LoadingContainer extends StatelessWidget {
  const LoadingContainer({Key? key, required this.child}) : super(key: key);
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Center(
        child: Container(
          width: MediaQuery.of(context).size.width / 2,
          height: MediaQuery.of(context).size.height * 3 / 4,
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(40)),
          child: child,
        ),
      ),
    );
  }
}