Skip to content

Commit de3c52b

Browse files
Vidhijain20AsCress
andauthored
feat: added Settings screen (#2696)
Co-authored-by: Anashuman Singh <[email protected]>
1 parent ac37438 commit de3c52b

File tree

6 files changed

+165
-15
lines changed

6 files changed

+165
-15
lines changed

lib/colors.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'dart:ui';
2+
3+
Color primaryRed = const Color(0xFFD32F2F);

lib/constants.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ String wifi = 'WIFI';
101101
String whatisPslab = 'What is PSLab Device?';
102102
String pslabUrl = 'https://pslab.io';
103103

104+
String settings = 'Settings';
105+
String start = 'Auto Start';
106+
String autoStartText = 'Auto start app when PSLab device is connected';
107+
String export = 'Export Data Format';
108+
String txtFormat = 'TXT Format';
109+
String csvFormat = 'CSV Format';
110+
String cancel = 'CANCEL';
111+
String currentFormat = 'Current format is ';
104112
String aboutUs = 'About Us';
105113
String pslabDescription =
106114
'The goal of PSLab is to create an Open Source hardware device (open on all layers) that can be used for experiments by teachers, students and citizen scientists. Our tiny pocket lab provides an array of sensors for doing science and engineering experiments. It provides functions of numerous measurement devices including an oscilloscope, a waveform generator, a frequency generator, a frequency counter, a programmable voltage, current source and as a data logger.';

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:pslab/view/connect_device_screen.dart';
66
import 'package:pslab/view/faq_screen.dart';
77
import 'package:pslab/view/instruments_screen.dart';
88
import 'package:pslab/view/oscilloscope_screen.dart';
9+
import 'package:pslab/view/settings_screen.dart';
910
import 'package:pslab/view/about_us_screen.dart';
1011

1112
import 'constants.dart';
@@ -44,6 +45,7 @@ class MyApp extends StatelessWidget {
4445
'/oscilloscope': (context) => const OscilloscopeScreen(),
4546
'/connectDevice': (context) => const ConnectDeviceScreen(),
4647
'/faq': (context) => const FAQScreen(),
48+
'/settings': (context) => const SettingsScreen(),
4749
'/aboutUs': (context) => const AboutUsScreen(),
4850
},
4951
);

lib/providers/board_state_provider.dart

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/cupertino.dart';
22
import 'package:pslab/communication/science_lab.dart';
3+
import 'package:pslab/constants.dart';
34
import 'package:pslab/others/logger_service.dart';
45
import 'package:pslab/providers/locator.dart';
56
import 'package:usb_serial/usb_serial.dart';
@@ -12,6 +13,8 @@ class BoardStateProvider extends ChangeNotifier {
1213
bool hasPermission = false;
1314
late ScienceLabCommon scienceLabCommon;
1415
String pslabVersionID = 'Not Connected';
16+
String exportFormat = txtFormat;
17+
bool autoStart = true;
1518

1619
BoardStateProvider() {
1720
scienceLabCommon = getIt.get<ScienceLabCommon>();
@@ -21,21 +24,23 @@ class BoardStateProvider extends ChangeNotifier {
2124
await scienceLabCommon.initialize();
2225
pslabIsConnected = await scienceLabCommon.openDevice();
2326
setPSLabVersionIDs();
24-
UsbSerial.usbEventStream?.listen(
25-
(UsbEvent usbEvent) async {
26-
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
27-
if (await attemptToConnectPSLab()) {
28-
pslabIsConnected = await scienceLabCommon.openDevice();
29-
setPSLabVersionIDs();
27+
if (autoStart) {
28+
UsbSerial.usbEventStream?.listen(
29+
(UsbEvent usbEvent) async {
30+
if (usbEvent.event == UsbEvent.ACTION_USB_ATTACHED) {
31+
if (await attemptToConnectPSLab()) {
32+
pslabIsConnected = await scienceLabCommon.openDevice();
33+
setPSLabVersionIDs();
34+
}
35+
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED) {
36+
scienceLabCommon.setConnected(false);
37+
pslabIsConnected = false;
38+
pslabVersionID = 'Not Connected';
39+
notifyListeners();
3040
}
31-
} else if (usbEvent.event == UsbEvent.ACTION_USB_DETACHED) {
32-
scienceLabCommon.setConnected(false);
33-
pslabIsConnected = false;
34-
pslabVersionID = 'Not Connected';
35-
notifyListeners();
36-
}
37-
},
38-
);
41+
},
42+
);
43+
}
3944
}
4045

4146
Future<void> setPSLabVersionIDs() async {

lib/view/settings_screen.dart

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:get_it/get_it.dart';
3+
import 'package:pslab/colors.dart';
4+
import 'package:pslab/providers/board_state_provider.dart';
5+
import 'package:pslab/view/widgets/main_scaffold_widget.dart';
6+
import 'package:pslab/constants.dart';
7+
8+
class SettingsScreen extends StatefulWidget {
9+
const SettingsScreen({super.key});
10+
11+
@override
12+
State<StatefulWidget> createState() => _SettingsScreenState();
13+
}
14+
15+
class _SettingsScreenState extends State<SettingsScreen> {
16+
bool isTxtFormatSelected =
17+
(GetIt.instance.get<BoardStateProvider>().exportFormat == txtFormat);
18+
bool isCsvFormatSelected =
19+
(GetIt.instance.get<BoardStateProvider>().exportFormat == csvFormat);
20+
21+
void _showExportFormatDialog() {
22+
showDialog(
23+
context: context,
24+
builder: (BuildContext context) {
25+
return SimpleDialog(
26+
shape: const RoundedRectangleBorder(
27+
borderRadius: BorderRadius.zero,
28+
),
29+
backgroundColor: Colors.white,
30+
title: Text(export,
31+
style:
32+
const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
33+
children: [
34+
RadioListTile<bool>(
35+
title: Text(txtFormat),
36+
value: true,
37+
groupValue: isTxtFormatSelected,
38+
activeColor: primaryRed,
39+
onChanged: (bool? value) {
40+
setState(
41+
() {
42+
isTxtFormatSelected = true;
43+
isCsvFormatSelected = false;
44+
GetIt.instance.get<BoardStateProvider>().exportFormat =
45+
txtFormat;
46+
Navigator.of(context).pop();
47+
},
48+
);
49+
},
50+
),
51+
RadioListTile<bool>(
52+
title: Text(csvFormat),
53+
value: true,
54+
groupValue: isCsvFormatSelected,
55+
activeColor: primaryRed,
56+
onChanged: (bool? value) {
57+
setState(
58+
() {
59+
isTxtFormatSelected = false;
60+
isCsvFormatSelected = true;
61+
GetIt.instance.get<BoardStateProvider>().exportFormat =
62+
csvFormat;
63+
Navigator.of(context).pop();
64+
},
65+
);
66+
},
67+
),
68+
Align(
69+
alignment: Alignment.bottomRight,
70+
child: GestureDetector(
71+
onTap: () {
72+
Navigator.of(context).pop();
73+
},
74+
child: Padding(
75+
padding: const EdgeInsets.only(right: 20, bottom: 5),
76+
child: Text(
77+
cancel,
78+
style: const TextStyle(fontWeight: FontWeight.w500),
79+
),
80+
),
81+
),
82+
)
83+
],
84+
);
85+
},
86+
);
87+
}
88+
89+
@override
90+
Widget build(BuildContext context) {
91+
return MainScaffold(
92+
title: settings,
93+
index: 4,
94+
body: SafeArea(
95+
child: ListView(
96+
children: [
97+
const SizedBox(height: 10),
98+
CheckboxListTile(
99+
title: Text(start),
100+
subtitle: Text(autoStartText),
101+
value: GetIt.instance.get<BoardStateProvider>().autoStart,
102+
onChanged: (bool? value) {
103+
setState(() {
104+
GetIt.instance.get<BoardStateProvider>().autoStart = value!;
105+
});
106+
},
107+
activeColor: primaryRed,
108+
),
109+
const SizedBox(height: 10),
110+
ListTile(
111+
title: Text(export),
112+
subtitle: Text(currentFormat +
113+
GetIt.instance.get<BoardStateProvider>().exportFormat),
114+
onTap: () {
115+
_showExportFormatDialog();
116+
},
117+
),
118+
],
119+
),
120+
),
121+
);
122+
}
123+
}

lib/view/widgets/navigation_drawer.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,16 @@ class _NavDrawerState extends State<NavDrawer> {
171171
),
172172
),
173173
onTap: () {
174-
/**/
174+
if (Navigator.canPop(context) &&
175+
ModalRoute.of(context)?.settings.name == '/settings') {
176+
Navigator.popUntil(context, ModalRoute.withName('/settings'));
177+
} else {
178+
Navigator.pushNamedAndRemoveUntil(
179+
context,
180+
'/settings',
181+
(route) => route.isFirst,
182+
);
183+
}
175184
},
176185
),
177186
const Divider(),

0 commit comments

Comments
 (0)