diff --git a/dialer/lib/features/settings/blocked/settings_blocked.dart b/dialer/lib/features/settings/blocked/settings_blocked.dart new file mode 100644 index 0000000..77c3a85 --- /dev/null +++ b/dialer/lib/features/settings/blocked/settings_blocked.dart @@ -0,0 +1,167 @@ +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class BlockedNumbersPage extends StatefulWidget { + const BlockedNumbersPage({super.key}); + + @override + _BlockedNumbersPageState createState() => _BlockedNumbersPageState(); +} + +class _BlockedNumbersPageState extends State { + bool _blockUnknownNumbers = false; // Toggle for blocking unknown numbers + List _blockedNumbers = []; // List of blocked numbers + final TextEditingController _numberController = TextEditingController(); + + @override + void initState() { + super.initState(); + _loadPreferences(); // Load data on initialization + } + + // Load preferences from local storage + Future _loadPreferences() async { + final prefs = await SharedPreferences.getInstance(); + setState(() { + _blockUnknownNumbers = prefs.getBool('blockUnknownNumbers') ?? false; + _blockedNumbers = prefs.getStringList('blockedNumbers') ?? []; + }); + } + + // Save preferences to local storage + Future _savePreferences() async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setBool('blockUnknownNumbers', _blockUnknownNumbers); + await prefs.setStringList('blockedNumbers', _blockedNumbers); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + backgroundColor: Colors.black, + appBar: AppBar( + title: const Text('Blocked Numbers'), + ), + body: ListView( + padding: const EdgeInsets.all(16), + children: [ + SwitchListTile( + title: const Text( + 'Block Unknown Numbers', + style: TextStyle(color: Colors.white), + ), + value: _blockUnknownNumbers, + onChanged: (bool value) { + setState(() { + _blockUnknownNumbers = value; + _savePreferences(); // Save the state to local storage + }); + }, + ), + const SizedBox(height: 16), + ListTile( + title: const Text( + 'Blocked Numbers', + style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold), + ), + subtitle: _blockedNumbers.isEmpty + ? const Text( + 'No blocked numbers', + style: TextStyle(color: Colors.grey), + ) + : null, + ), + ..._blockedNumbers.map( + (number) => ListTile( + title: Text( + number, + style: const TextStyle(color: Colors.white), + ), + trailing: IconButton( + icon: const Icon(Icons.delete, color: Colors.red), + onPressed: () => _unblockNumber(number), + ), + ), + ), + const Divider(color: Colors.grey), + ListTile( + title: const Text( + 'Block a Number', + style: TextStyle(color: Colors.white), + ), + trailing: const Icon(Icons.add, color: Colors.white), + onTap: () => _showBlockNumberDialog(), + ), + ], + ), + ); + } + + // Function to block a number + void _blockNumber(String number) { + if (number.isNotEmpty && !_blockedNumbers.contains(number)) { + setState(() { + _blockedNumbers.add(number); + _savePreferences(); // Save the updated list + }); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('$number has been blocked')), + ); + } + } + + // Function to unblock a number + void _unblockNumber(String number) { + setState(() { + _blockedNumbers.remove(number); + _savePreferences(); // Save the updated list + }); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text('$number has been unblocked')), + ); + } + + // Dialog for blocking a new number + void _showBlockNumberDialog() { + showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + backgroundColor: Colors.grey[900], + title: const Text('Block a Number', style: TextStyle(color: Colors.white)), + content: TextField( + controller: _numberController, + keyboardType: TextInputType.phone, + decoration: const InputDecoration( + hintText: 'Enter number', + hintStyle: TextStyle(color: Colors.grey), + ), + style: const TextStyle(color: Colors.white), + ), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + }, + child: const Text('Cancel', style: TextStyle(color: Colors.white)), + ), + TextButton( + onPressed: () { + _blockNumber(_numberController.text); + _numberController.clear(); + Navigator.pop(context); + }, + child: const Text('Block', style: TextStyle(color: Colors.red)), + ), + ], + ); + }, + ); + } + + @override + void dispose() { + _numberController.dispose(); + super.dispose(); + } +} diff --git a/dialer/lib/features/settings/settings.dart b/dialer/lib/features/settings/settings.dart index be94b8c..0081ca3 100644 --- a/dialer/lib/features/settings/settings.dart +++ b/dialer/lib/features/settings/settings.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:dialer/features/settings/call/settingsCall.dart'; import 'package:dialer/features/settings/sim/settings_accounts.dart'; import 'package:dialer/features/settings/key/manage_keys_page.dart'; +import 'package:dialer/features/settings/blocked/settings_blocked.dart'; class SettingsPage extends StatelessWidget { const SettingsPage({super.key}); @@ -28,9 +29,15 @@ class SettingsPage extends StatelessWidget { MaterialPageRoute(builder: (context) => const KeyManagementPage()), ); break; - // Add more cases for other settings pages + case 'Blocked numbers': + Navigator.push( + context, + MaterialPageRoute(builder: (context) => const BlockedNumbersPage()), + ); + break; + // Add more cases for other settings pages default: - // Handle default or unknown settings + // Handle default or unknown settings break; } } @@ -41,6 +48,7 @@ class SettingsPage extends StatelessWidget { 'Calling settings', 'Page of telephone accounts', 'Key management', + 'Blocked numbers' ]; return Scaffold( diff --git a/dialer/pubspec.yaml b/dialer/pubspec.yaml index 501f488..2610104 100644 --- a/dialer/pubspec.yaml +++ b/dialer/pubspec.yaml @@ -49,6 +49,7 @@ dependencies: intl_utils: ^2.0.7 mobile_number: path: packages/mobile_number + shared_preferences: ^2.3.3 dev_dependencies: flutter_test: