ModernMemoires/lib/views/start_page/start_page.dart

187 lines
5.0 KiB
Dart
Raw Normal View History

2024-01-13 17:07:11 +01:00
import 'dart:async';
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;
2024-01-13 17:07:11 +01:00
String _displayedText = "welcome back!";
2024-01-01 18:31:10 +01:00
@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) {
2024-01-13 17:07:11 +01:00
Timer(const Duration(milliseconds: 200), () {
_validatePin();
});
2024-01-01 18:31:10 +01:00
}
}
}
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-13 17:07:11 +01:00
setState(() {
_displayedText = "oops! try again.";
});
2024-01-09 12:34:44 +01:00
}
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-13 17:07:11 +01:00
body: Center(
child: Container(
height: 550,
width: 380,
decoration: const BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(50))),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_displayedText, style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
const SizedBox(height: 20),
Row(
mainAxisSize: MainAxisSize.min,
children: List.generate(pinLength, (index) {
return Container(
margin: const EdgeInsets.all(4),
width: 15,
height: 15,
decoration: BoxDecoration(
color: _pin.length > index ? const Color(0xffD9D9D9) : Colors.transparent,
border: Border.all(color: const Color(0xffD9D9D9)),
borderRadius: BorderRadius.circular(15),
),
);
}),
),
const SizedBox(height: 20),
..._buildNumberPad(),
],
2024-01-01 18:31:10 +01:00
),
),
),
);
}
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
),
),
),
);
}
}