304 lines
10 KiB
Dart
304 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:mobile_scanner/mobile_scanner.dart';
|
|
import 'data_screen.dart';
|
|
|
|
class HomeScreen extends StatefulWidget {
|
|
const HomeScreen({super.key});
|
|
|
|
@override
|
|
State<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends State<HomeScreen> {
|
|
String? scannedData;
|
|
List<String> scannedHistory = [];
|
|
|
|
void _navigateToDataScreen() {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => DataScreen(scannedHistory: scannedHistory),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _openScanner() {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ScannerScreen(
|
|
onScan: (String code) {
|
|
setState(() {
|
|
scannedData = code;
|
|
scannedHistory.add(code);
|
|
});
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Barcode Scanner'),
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(20),
|
|
child: ElevatedButton.icon(
|
|
onPressed: _openScanner,
|
|
icon: const Icon(Icons.qr_code_scanner, size: 32),
|
|
label: const Text(
|
|
'Scan Barcode',
|
|
style: TextStyle(fontSize: 18),
|
|
),
|
|
style: ElevatedButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 40,
|
|
vertical: 15,
|
|
),
|
|
backgroundColor: Theme.of(context).colorScheme.primary,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 30),
|
|
if (scannedData != null)
|
|
Container(
|
|
margin: const EdgeInsets.symmetric(horizontal: 20),
|
|
padding: const EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.grey[100],
|
|
borderRadius: BorderRadius.circular(10),
|
|
border: Border.all(color: Colors.grey[300]!),
|
|
),
|
|
child: Column(
|
|
children: [
|
|
const Text(
|
|
'Last Scanned:',
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
Text(
|
|
scannedData!,
|
|
style: const TextStyle(fontSize: 18),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
if (scannedHistory.isNotEmpty)
|
|
Expanded(
|
|
child: Container(
|
|
margin: const EdgeInsets.symmetric(horizontal: 20),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Scan History (${scannedHistory.length} items)',
|
|
style: const TextStyle(
|
|
fontSize: 16,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 10),
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemCount: scannedHistory.length,
|
|
itemBuilder: (context, index) {
|
|
final reversedIndex =
|
|
scannedHistory.length - 1 - index;
|
|
return Card(
|
|
child: ListTile(
|
|
leading: CircleAvatar(
|
|
child: Text('${reversedIndex + 1}'),
|
|
),
|
|
title: Text(scannedHistory[reversedIndex]),
|
|
subtitle: Text('Item ${reversedIndex + 1}'),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Container(
|
|
padding: const EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.grey.withOpacity(0.3),
|
|
spreadRadius: 1,
|
|
blurRadius: 5,
|
|
offset: const Offset(0, -3),
|
|
),
|
|
],
|
|
),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: [
|
|
ElevatedButton.icon(
|
|
onPressed: scannedHistory.isEmpty ? null : () {
|
|
setState(() {
|
|
scannedHistory.clear();
|
|
scannedData = null;
|
|
});
|
|
},
|
|
icon: const Icon(Icons.clear_all),
|
|
label: const Text('Clear'),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.red,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
),
|
|
ElevatedButton.icon(
|
|
onPressed: scannedHistory.isEmpty ? null : _navigateToDataScreen,
|
|
icon: const Icon(Icons.list),
|
|
label: const Text('View All Data'),
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: Colors.green,
|
|
foregroundColor: Colors.white,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class ScannerScreen extends StatefulWidget {
|
|
final Function(String) onScan;
|
|
|
|
const ScannerScreen({super.key, required this.onScan});
|
|
|
|
@override
|
|
State<ScannerScreen> createState() => _ScannerScreenState();
|
|
}
|
|
|
|
class _ScannerScreenState extends State<ScannerScreen> {
|
|
MobileScannerController cameraController = MobileScannerController(
|
|
formats: [BarcodeFormat.code128]
|
|
);
|
|
bool hasScanned = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Scan Barcode'),
|
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
|
actions: [
|
|
// IconButton(
|
|
// icon: ValueListenableBuilder(
|
|
// valueListenable: cameraController.torchStateNotifier,
|
|
// builder: (context, state, child) {
|
|
// switch (state) {
|
|
// case TorchState.off:
|
|
// return const Icon(Icons.flash_off, color: Colors.grey);
|
|
// case TorchState.on:
|
|
// return const Icon(Icons.flash_on, color: Colors.yellow);
|
|
// }
|
|
// },
|
|
// ),
|
|
// onPressed: () => cameraController.toggleTorch(),
|
|
// ),
|
|
// IconButton(
|
|
// icon: ValueListenableBuilder(
|
|
// valueListenable: cameraController.cameraFacingState,
|
|
// builder: (context, state, child) {
|
|
// switch (state) {
|
|
// case CameraFacing.front:
|
|
// return const Icon(Icons.camera_front);
|
|
// case CameraFacing.back:
|
|
// return const Icon(Icons.camera_rear);
|
|
// }
|
|
// },
|
|
// ),
|
|
// onPressed: () => cameraController.switchCamera(),
|
|
// ),
|
|
],
|
|
),
|
|
body: Stack(
|
|
children: [
|
|
MobileScanner(
|
|
controller: cameraController,
|
|
onDetect: (capture) {
|
|
if (!hasScanned) {
|
|
final List<Barcode> barcodes = capture.barcodes;
|
|
for (final barcode in barcodes) {
|
|
if (barcode.rawValue != null) {
|
|
hasScanned = true;
|
|
widget.onScan(barcode.rawValue!);
|
|
Navigator.pop(context);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
),
|
|
Center(
|
|
child: Container(
|
|
width: 300,
|
|
height: 300,
|
|
decoration: BoxDecoration(
|
|
border: Border.all(
|
|
color: Colors.green,
|
|
width: 2,
|
|
),
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
),
|
|
),
|
|
Positioned(
|
|
bottom: 100,
|
|
left: 0,
|
|
right: 0,
|
|
child: Center(
|
|
child: Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
|
|
decoration: BoxDecoration(
|
|
color: Colors.black54,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: const Text(
|
|
'Align barcode within the frame',
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
cameraController.dispose();
|
|
super.dispose();
|
|
}
|
|
} |