languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

username_page.dart (2340B)


      1 import 'package:firebase_auth/firebase_auth.dart';
      2 import 'package:flutter/material.dart';
      3 import 'package:shared_preferences/shared_preferences.dart';
      4 
      5 class UsernamePage extends StatefulWidget {
      6   const UsernamePage({super.key});
      7 
      8   @override
      9   State<UsernamePage> createState() => _UsernamePageState();
     10 }
     11 
     12 class _UsernamePageState extends State<UsernamePage> {
     13   final TextEditingController _usernameController = TextEditingController();
     14   String _username = '';
     15 
     16   @override
     17   void initState() {
     18     super.initState();
     19     _loadUsername();
     20     _getFirebaseUserEmail();
     21   }
     22 
     23   Future<void> _loadUsername() async {
     24     final prefs = await SharedPreferences.getInstance();
     25     final username = prefs.getString('username') ?? '';
     26     setState(() {
     27       _username = username;
     28       _usernameController.text = username;
     29     });
     30   }
     31 
     32   Future<void> _saveUsername() async {
     33     final prefs = await SharedPreferences.getInstance();
     34     await prefs.setString('username', _usernameController.text);
     35     setState(() {
     36       _username = _usernameController.text;
     37     });
     38   }
     39 
     40   Future<void> _getFirebaseUserEmail() async {
     41     final user = FirebaseAuth.instance.currentUser;
     42     if (user != null && user.email != null) {
     43       setState(() {
     44         _username = user.email!; // Set initial username to Firebase email
     45       });
     46     }
     47   }
     48 
     49   @override
     50   Widget build(BuildContext context) {
     51     return Scaffold(
     52       appBar: AppBar(
     53         title: const Text('Username'),
     54       ),
     55       body: Padding(
     56         padding: const EdgeInsets.all(16.0),
     57         child: Column(
     58           children: [
     59             TextField(
     60               controller: _usernameController,
     61               decoration: const InputDecoration(
     62                 labelText: 'Enter Username',
     63               ),
     64               onChanged: (value) => setState(() => _username = value),
     65             ),
     66             const SizedBox(height: 16.0),
     67             ElevatedButton(
     68               onPressed: _saveUsername,
     69               style: ElevatedButton.styleFrom(
     70                 backgroundColor:
     71                     Colors.black,
     72                 foregroundColor: Colors.white,
     73               ),
     74               child: const Text('Save Username'),
     75             ),
     76             const SizedBox(height: 16.0),
     77             Text('Current Username: $_username'),
     78           ],
     79         ),
     80       ),
     81     );
     82   }
     83 }