-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.dart
132 lines (123 loc) · 4.13 KB
/
main.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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:sim_data/sim_data.dart';
import 'package:ussd_service/ussd_service.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
enum RequestState {
ongoing,
success,
error,
}
class _MyAppState extends State<MyApp> {
RequestState? _requestState;
String _requestCode = "";
String _responseCode = "";
String _responseMessage = "";
Future<void> sendUssdRequest() async {
setState(() {
_requestState = RequestState.ongoing;
});
try {
String responseMessage;
await Permission.phone.request();
if (!await Permission.phone.isGranted) {
throw Exception("permission missing");
}
SimData simData = await SimDataPlugin.getSimData();
responseMessage = await UssdService.makeRequest(
simData.cards.first.subscriptionId, _requestCode);
setState(() {
_requestState = RequestState.success;
_responseMessage = responseMessage;
});
} on PlatformException catch (e) {
setState(() {
_requestState = RequestState.error;
_responseCode = e is PlatformException ? e.code : "";
_responseMessage = e.message ?? '';
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Ussd Plugin demo'),
),
body: Container(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
labelText: 'Enter Code',
),
onChanged: (newValue) {
setState(() {
_requestCode = newValue;
});
},
),
const SizedBox(height: 20),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
onPressed: _requestState == RequestState.ongoing
? null
: () {
sendUssdRequest();
},
child: const Text('Send Ussd request'),
),
const SizedBox(height: 20),
if (_requestState == RequestState.ongoing)
Row(
children: const <Widget>[
SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(),
),
SizedBox(width: 24),
Text('Ongoing request...'),
],
),
if (_requestState == RequestState.success) ...[
const Text('Last request was successful.'),
const SizedBox(height: 10),
const Text('Response was:'),
Text(
_responseMessage,
style: const TextStyle(fontWeight: FontWeight.bold),
),
],
if (_requestState == RequestState.error) ...[
const Text('Last request was not successful'),
const SizedBox(height: 10),
const Text('Error code was:'),
Text(
_responseCode,
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 10),
const Text('Error message was:'),
Text(
_responseMessage,
style: const TextStyle(fontWeight: FontWeight.bold),
),
]
]),
),
),
);
}
}