ModernMemoires/lib/views/start_page/start_page.dart

185 lines
5.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import '../../utils/logic/PreferencesService.dart'; // For Haptic Feedback
class StartPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'PIN Input',
home: PinInputScreen(),
);
}
}
class PinInputScreen extends StatefulWidget {
@override
_PinInputScreenState createState() => _PinInputScreenState();
}
class _PinInputScreenState extends State<PinInputScreen> {
String _pin = '';
static const int pinLength = 4;
@override
void initState() {
super.initState();
_checkPinStatus();
}
Future<void> _checkPinStatus() async {
bool pinEnabled = await PreferencesService()
.isPinEnabled(); // Adjust with actual implementation
if (!pinEnabled) {
// If no PIN is enabled, navigate to the "/first" route.
// Navigator context will be available after the widget build.
Future.microtask(() => context.go("/first"));
}
}
// 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) {
print("Right");
context.go("/first");
// Perform any actions you need on successful pin entry
} else {
// Handle wrong pin entry, e.g., reset pin, show error, etc.
print("Wrong PIN");
}
// Resetting the PIN for this example, you might want to do this differently
setState(() {
_pin = '';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey.shade200, // Light gray background
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,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(50))),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Welcome Back!',
style:
TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
SizedBox(height: 20),
Row(
mainAxisSize: MainAxisSize.min,
children: List.generate(pinLength, (index) {
return Container(
margin: EdgeInsets.all(4),
width: 15,
height: 15,
decoration: BoxDecoration(
color: _pin.length > index
? Colors.black
: Colors.transparent,
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(15),
),
);
}),
),
SizedBox(height: 20),
..._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),
onTapDown: (_) =>
HapticFeedback.lightImpact(), // Cool haptic feedback on tap
child: Container(
width: 70,
height: 70,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
borderRadius: BorderRadius.circular(35),
),
child: Center(
child: Text(
number.toString(),
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
),
);
}
}