-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client-server.dart
158 lines (133 loc) · 4.66 KB
/
Client-server.dart
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
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class LocationPage extends StatefulWidget {
@override
_LocationPageState createState() => _LocationPageState();
}
class _LocationPageState extends State<LocationPage> {
// Declare variables to store the location data
double latitude;
double longitude;
// Function to send the location data to the server
sendLocationData() async {
// Set up the API endpoint and the payload (i.e., the location data)
var apiEndpoint = "http://yourserver.com/api/location";
var payload = {
"latitude": latitude,
"longitude": longitude,
};
// Send the POST request to the server with the payload in the body
var response = await http.post(apiEndpoint, body: payload);
// If the request was successful, parse the JSON response
if (response.statusCode == 200) {
var data = json.decode(response.body);
// Do something with the data (e.g., update a state variable)
setState(() {
// Update the latitude and longitude with the data from the server
latitude = data['latitude'];
longitude = data['longitude'];
});
} else {
// If the request was unsuccessful, display an error message
print("Failed to send location data: ${response.statusCode}");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Display the latitude and longitude
Text("Latitude: $latitude"),
Text("Longitude: $longitude"),
// Button to send the location data to the server
RaisedButton(
onPressed: sendLocationData,
child: Text("Send Location Data"),
),
],
),
),
);
}
}
//In this example, the sendLocationData function sends a POST request to the server with the location data (latitude and longitude) in the request body. The server should compute the data and send back updated data in the response body, which is then parsed and used to update the state variables in the Flutter app.
//date and time
import 'package:flutter/material.dart';
import 'dart:core';
class ClockPage extends StatefulWidget {
@override
_ClockPageState createState() => _ClockPageState();
}
class _ClockPageState extends State<ClockPage> {
// Declare a variable to store the current time
DateTime currentTime;
@override
void initState() {
// Call the getCurrentTime function when the app starts
getCurrentTime();
super.initState();
}
// Function to get the current time
getCurrentTime() {
// Get the current time
currentTime = DateTime.now();
// Set the state to update the clock
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
// Display the current time using the Text widget
child: Text(currentTime.toString()),
),
);
}
}
//This example displays the current time as a string using the Text widget. The time is retrieved using the DateTime.now() method and stored in the currentTime variable. The clock is updated by calling the setState method whenever the getCurrentTime function is called.
//To update the clock continuously, you can use a timer to call the getCurrentTime function at regular intervals. Here's an example of how you can set up a timer in Flutter:
import 'package:flutter/material.dart';
import 'dart:async';
class ClockPage extends StatefulWidget {
@override
_ClockPageState createState() => _ClockPageState();
}
class _ClockPageState extends State<ClockPage> {
// Declare a variable to store the current time
DateTime currentTime;
// Declare a timer
Timer timer;
@override
void initState() {
// Call the getCurrentTime function when the app starts
getCurrentTime();
super.initState();
}
// Function to get the current time
getCurrentTime() {
// Get the current time
currentTime = DateTime.now();
// Set the state to update the clock
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
// Display the current time using the Text widget
child: Text(currentTime.toString()),
),
);
}
@override
void dispose() {
// Cancel the timer when the app is closed
timer.cancel();
super.dispose();
}
}
//In this example, the timer is created in the initState method and starts running immediately. The timer calls the getCurrentTime function every second (1000 milliseconds), which updates the clock.