ModernMemoires/lib/views/start_page/start_page.dart

188 lines
5.1 KiB
Dart
Raw Normal View History

2024-01-09 12:34:44 +01:00
import 'package:flutter/foundation.dart';
2024-01-01 18:31:10 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
2024-01-08 19:34:48 +01:00
import '../../utils/definitions/style_guide.dart';
2024-01-09 12:34:44 +01:00
import '../../utils/logic/preferences_service.dart'; // For Haptic Feedback
2024-01-01 18:31:10 +01:00
class StartPage extends StatelessWidget {
2024-01-09 12:34:44 +01:00
const StartPage({super.key});
2024-01-01 18:31:10 +01:00
@override
Widget build(BuildContext context) {
2024-01-09 12:34:44 +01:00
return const MaterialApp(
2024-01-01 18:31:10 +01:00
title: 'PIN Input',
home: PinInputScreen(),
);
}
}
class PinInputScreen extends StatefulWidget {
2024-01-09 12:34:44 +01:00
const PinInputScreen({super.key});
2024-01-01 18:31:10 +01:00
@override
2024-01-09 12:34:44 +01:00
State<PinInputScreen> createState() => _PinInputScreen();
2024-01-01 18:31:10 +01:00
}
2024-01-09 12:34:44 +01:00
class _PinInputScreen extends State<PinInputScreen> {
2024-01-01 18:31:10 +01:00
String _pin = '';
static const int pinLength = 4;
@override
void initState() {
super.initState();
_checkPinStatus();
}
Future<void> _checkPinStatus() async {
2024-01-09 12:34:44 +01:00
bool pinEnabled = await PreferencesService().isPinEnabled();
2024-01-01 18:31:10 +01:00
if (!pinEnabled) {
2024-01-09 12:34:44 +01:00
Future.microtask(() {
if (mounted) {
context.go("/first");
}
});
2024-01-01 18:31:10 +01:00
}
}
// This method would be your actual method for checking the pin
// Replace with your actual method from PreferencesService
Future<bool> checkPin(int pin) async {
bool pinRight = await PreferencesService().checkPin(pin);
return pinRight;
}
void _onNumberTap(int number) {
if (_pin.length < pinLength) {
setState(() {
_pin += number.toString();
});
if (_pin.length == pinLength) {
_validatePin();
}
}
}
Future<void> _validatePin() async {
bool correct = await checkPin(int.parse(_pin));
if (correct) {
2024-01-09 12:34:44 +01:00
if (mounted) {
context.go("/first");
}
2024-01-01 18:31:10 +01:00
// Perform any actions you need on successful pin entry
} else {
// Handle wrong pin entry, e.g., reset pin, show error, etc.
2024-01-09 12:34:44 +01:00
if (kDebugMode) {
print("Wrong PIN");
}
2024-01-01 18:31:10 +01:00
}
// Resetting the PIN for this example, you might want to do this differently
setState(() {
_pin = '';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
2024-01-08 19:34:48 +01:00
backgroundColor: AppStyle.backgroundColor,
2024-01-01 18:31:10 +01:00
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.grey, Colors.red.shade100],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Center(
child: Container(
height: 600,
width: 400,
2024-01-09 12:34:44 +01:00
decoration: const BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(50))),
2024-01-01 18:31:10 +01:00
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2024-01-09 12:34:44 +01:00
const Text('Welcome Back!', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
const SizedBox(height: 20),
2024-01-01 18:31:10 +01:00
Row(
mainAxisSize: MainAxisSize.min,
children: List.generate(pinLength, (index) {
return Container(
2024-01-09 12:34:44 +01:00
margin: const EdgeInsets.all(4),
2024-01-01 18:31:10 +01:00
width: 15,
height: 15,
decoration: BoxDecoration(
2024-01-08 19:34:48 +01:00
color: _pin.length > index ? Colors.black : Colors.transparent,
2024-01-01 18:31:10 +01:00
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(15),
),
);
}),
),
2024-01-09 12:34:44 +01:00
const SizedBox(height: 20),
2024-01-01 18:31:10 +01:00
..._buildNumberPad(),
],
),
),
),
),
);
}
List<Widget> _buildNumberPad() {
List<int> numbers = List.generate(9, (index) => index + 1);
List<Widget> rows = [];
// First three rows (1-9)
for (var i = 0; i < 3; i++) {
rows.add(
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: numbers.skip(i * 3).take(3).map((number) {
return _buildNumberButton(number);
}).toList(),
),
);
}
// Last row with 0
rows.add(
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(width: 70), // Empty container to align 0 in center
_buildNumberButton(0),
Container(width: 70), // Empty container to align 0 in center
],
),
);
return rows;
}
Widget _buildNumberButton(int number) {
return GestureDetector(
onTap: () => _onNumberTap(number),
2024-01-08 19:34:48 +01:00
onTapDown: (_) => HapticFeedback.lightImpact(), // Cool haptic feedback on tap
2024-01-01 18:31:10 +01:00
child: Container(
width: 70,
height: 70,
2024-01-09 12:34:44 +01:00
margin: const EdgeInsets.all(10),
2024-01-01 18:31:10 +01:00
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(35),
),
child: Center(
child: Text(
number.toString(),
2024-01-09 12:34:44 +01:00
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
2024-01-01 18:31:10 +01:00
),
),
),
);
}
}