import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:pyramid/features/auth/login_notifier.dart'; class ServerPage extends ConsumerStatefulWidget { const ServerPage({super.key}); @override ConsumerState createState() => _ServerPageState(); } class _ServerPageState extends ConsumerState { final _formKey = GlobalKey(); final _ctrl = TextEditingController(text: 'matrix.org'); bool _loading = false; String? _error; @override void initState() { super.initState(); ref.read(homeserverProvider.future).then((saved) { if (saved != null && mounted) { _ctrl.text = saved.replaceAll(RegExp(r'^https?://'), ''); } }); } @override void dispose() { _ctrl.dispose(); super.dispose(); } Future _continue() async { if (!_formKey.currentState!.validate()) return; setState(() { _loading = true; _error = null; }); final input = _ctrl.text.trim().replaceAll(RegExp(r'^https?://'), ''); try { await saveHomeserver(input); if (mounted) context.go('/login'); } catch (e) { setState(() => _error = e.toString()); } finally { if (mounted) setState(() => _loading = false); } } @override Widget build(BuildContext context) { final theme = Theme.of(context); return Scaffold( body: Center( child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 400), child: Padding( padding: const EdgeInsets.all(24), child: Form( key: _formKey, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Icon( Icons.change_history_rounded, size: 72, color: theme.colorScheme.primary, ), const SizedBox(height: 8), Text( 'Pyramid', textAlign: TextAlign.center, style: theme.textTheme.headlineMedium ?.copyWith(fontWeight: FontWeight.bold), ), const SizedBox(height: 32), Text( 'Server', style: theme.textTheme.titleMedium, ), const SizedBox(height: 8), TextFormField( controller: _ctrl, decoration: const InputDecoration( hintText: 'matrix.org', prefixText: 'https://', prefixIcon: Icon(Icons.dns_outlined), border: OutlineInputBorder(), ), keyboardType: TextInputType.url, validator: (v) { if (v == null || v.trim().isEmpty) { return 'Server-Adresse eingeben'; } return null; }, onFieldSubmitted: (_) => _continue(), ), if (_error != null) ...[ const SizedBox(height: 12), Text( _error!, style: TextStyle(color: theme.colorScheme.error), textAlign: TextAlign.center, ), ], const SizedBox(height: 24), FilledButton( onPressed: _loading ? null : _continue, child: _loading ? const SizedBox( height: 20, width: 20, child: CircularProgressIndicator(strokeWidth: 2), ) : const Text('Weiter'), ), ], ), ), ), ), ), ); } }