diff --git a/obs_flutter/obs/obs_demo/lib/CreateUserPage.dart b/obs_flutter/obs/obs_demo/lib/CreateUserPage.dart index f442913..c4b681f 100644 --- a/obs_flutter/obs/obs_demo/lib/CreateUserPage.dart +++ b/obs_flutter/obs/obs_demo/lib/CreateUserPage.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:obs_demo/screen/bottomNavi.dart'; -import 'user_profile.dart'; // Import the UserProfile class +import 'package:shared_preferences/shared_preferences.dart'; +import 'screen/bottomNavi.dart'; +import 'user_profile.dart'; class CreateUserPage extends StatefulWidget { @override @@ -10,6 +11,7 @@ class CreateUserPage extends StatefulWidget { class _CreateUserPageState extends State { String? _selectedLanguage; String? _userName; + final GlobalKey _formKey = GlobalKey(); @override Widget build(BuildContext context) { @@ -18,66 +20,91 @@ class _CreateUserPageState extends State { backgroundColor: Colors.white, title: Text('OBS Translator'), centerTitle: true, - automaticallyImplyLeading: false, // Remove back button + automaticallyImplyLeading: false, ), body: Center( child: Padding( padding: const EdgeInsets.all(20.0), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - TextField( - onChanged: (value) { - setState(() { - _userName = value; - }); - }, - decoration: InputDecoration( - labelText: 'Enter your name', - border: OutlineInputBorder(), + child: Form( + key: _formKey, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + TextFormField( + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter your name'; + } else if (value.length < 4) { + return 'Username must be at least 4 characters long'; + } else if (!RegExp(r'^[a-zA-Z]+$').hasMatch(value)) { + return 'Username can only contain letters'; + } + return null; + }, + onChanged: (value) { + setState(() { + _userName = value; + }); + }, + decoration: InputDecoration( + labelText: 'Enter your name', + border: OutlineInputBorder(), + ), ), - ), - SizedBox(height: 20), - DropdownButton( - value: _selectedLanguage, - items: ['English', 'Spanish', 'French'] - .map((String value) { - return DropdownMenuItem( - value: value, - child: Text(value), - ); - }).toList(), - onChanged: (String? newValue) { - setState(() { - _selectedLanguage = newValue; - }); - }, - hint: Text('Select a language'), - ), - SizedBox(height: 20), - ElevatedButton( - onPressed: _userName != null - ? () { - // Create a UserProfile instance - UserProfile userProfile = UserProfile( - userName: _userName!, - language: _selectedLanguage, - stories: [], - ); + SizedBox(height: 20), + DropdownButtonFormField( + validator: (value) { + if (value == null) { + return 'Please select a language'; + } + return null; + }, + value: _selectedLanguage, + items: ['English', 'Spanish', 'French'] + .map((String value) { + return DropdownMenuItem( + value: value, + child: Text(value), + ); + }).toList(), + onChanged: (String? newValue) { + setState(() { + _selectedLanguage = newValue; + }); + }, + decoration: InputDecoration( + labelText: 'Select a language', + border: OutlineInputBorder(), + ), + ), + SizedBox(height: 20), + ElevatedButton( + onPressed: () async { + if (_formKey.currentState!.validate()) { + SharedPreferences prefs = + await SharedPreferences.getInstance(); + await prefs.setString('userName', _userName!); + await prefs.setString('language', _selectedLanguage!); + + UserProfile userProfile = UserProfile( + userName: _userName!, + language: _selectedLanguage!, + stories: [], + ); - // Navigate to the bottom navigation bar example page with the user profile data - Navigator.pushReplacement( - context, - MaterialPageRoute( - builder: (context) => - BottomNavigationBarExample(userProfile), - ), - ); - } - : null, - child: Text('Create User'), - ), - ], + Navigator.pushReplacement( + context, + MaterialPageRoute( + builder: (context) => + BottomNavigationBarExample(userProfile), + ), + ); + } + }, + child: Text('Create User'), + ), + ], + ), ), ), ), diff --git a/obs_flutter/obs/obs_demo/lib/main.dart b/obs_flutter/obs/obs_demo/lib/main.dart index 8c5ac97..f1bd36f 100644 --- a/obs_flutter/obs/obs_demo/lib/main.dart +++ b/obs_flutter/obs/obs_demo/lib/main.dart @@ -1,26 +1,42 @@ import 'package:flutter/material.dart'; -import 'package:obs_demo/CreateUserPage.dart'; -import 'package:obs_demo/screen/bottomNavi.dart'; -import 'package:obs_demo/screen/dashboard.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'CreateUserPage.dart'; +import 'screen/bottomNavi.dart'; +import 'user_profile.dart'; -void main() { - runApp(const MyApp()); +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + SharedPreferences prefs = await SharedPreferences.getInstance(); + String? userName = prefs.getString('userName'); + String? language = prefs.getString('language'); + + runApp(MyApp( + userName: userName, + language: language, + )); } class MyApp extends StatelessWidget { - const MyApp({super.key}); + final String? userName; + final String? language; + + MyApp({this.userName, this.language}); @override Widget build(BuildContext context) { return MaterialApp( - debugShowCheckedModeBanner: false, - title: 'Flutter Demo', - theme: ThemeData( - colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), - useMaterial3: true, - ), - home: CreateUserPage() // Navigate to UserProfilePage directly - // home: Dashboard(), - ); + debugShowCheckedModeBanner: false, + title: 'Flutter Demo', + theme: ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), + useMaterial3: true, + ), + home: userName != null && language != null + ? BottomNavigationBarExample( + UserProfile( + userName: userName!, language: language!, stories: []), + ) + : CreateUserPage(), + ); } } diff --git a/obs_flutter/obs/obs_demo/lib/screen/bottomNavi.dart b/obs_flutter/obs/obs_demo/lib/screen/bottomNavi.dart index 68d569f..9dcf9e2 100644 --- a/obs_flutter/obs/obs_demo/lib/screen/bottomNavi.dart +++ b/obs_flutter/obs/obs_demo/lib/screen/bottomNavi.dart @@ -60,10 +60,6 @@ class _BottomNavigationBarExampleState rowIndex: 0, onUpdateTextAvailability: onUpdateTextAvailability, ), - // Text( - // 'Audio', - // style: optionStyle, - // ), Column( mainAxisAlignment: MainAxisAlignment.center, children: [ @@ -108,10 +104,6 @@ class _BottomNavigationBarExampleState @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - title: const Text('OBS Translator'), - centerTitle: true, - ), body: Center( child: _widgetOptions(context, widget.userProfile) .elementAt(_selectedIndex), @@ -130,10 +122,6 @@ class _BottomNavigationBarExampleState icon: Icon(Icons.create), label: '', ), - // BottomNavigationBarItem( - // icon: Icon(Icons.audiotrack_outlined), - // label: 'Audio', - // ), BottomNavigationBarItem( icon: Icon(Icons.menu), label: '', diff --git a/obs_flutter/obs/obs_demo/lib/screen/dashboard.dart b/obs_flutter/obs/obs_demo/lib/screen/dashboard.dart index c9f97e2..d1ce65e 100644 --- a/obs_flutter/obs/obs_demo/lib/screen/dashboard.dart +++ b/obs_flutter/obs/obs_demo/lib/screen/dashboard.dart @@ -92,6 +92,10 @@ class _DashboardState extends State { @override Widget build(BuildContext context) { return Scaffold( + appBar: AppBar( + title: const Text('OBS Translator'), + centerTitle: true, + ), body: FutureBuilder>>( future: loadData(), // Reference to the async function builder: (context, snapshot) { diff --git a/obs_flutter/obs/obs_demo/macos/Flutter/GeneratedPluginRegistrant.swift b/obs_flutter/obs/obs_demo/macos/Flutter/GeneratedPluginRegistrant.swift index e777c67..b8e2b22 100644 --- a/obs_flutter/obs/obs_demo/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/obs_flutter/obs/obs_demo/macos/Flutter/GeneratedPluginRegistrant.swift @@ -6,7 +6,9 @@ import FlutterMacOS import Foundation import path_provider_foundation +import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/obs_flutter/obs/obs_demo/pubspec.lock b/obs_flutter/obs/obs_demo/pubspec.lock index ca691a4..2614020 100644 --- a/obs_flutter/obs/obs_demo/pubspec.lock +++ b/obs_flutter/obs/obs_demo/pubspec.lock @@ -65,6 +65,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.2" + file: + dependency: transitive + description: + name: file + sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c" + url: "https://pub.dev" + source: hosted + version: "7.0.0" flutter: dependency: "direct main" description: flutter @@ -83,6 +91,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" http: dependency: "direct main" description: @@ -227,6 +240,62 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180 + url: "https://pub.dev" + source: hosted + version: "2.2.3" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "93d0ec9dd902d85f326068e6a899487d1f65ffcd5798721a95330b26c8131577" + url: "https://pub.dev" + source: hosted + version: "2.2.3" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "0a8a893bf4fd1152f93fec03a415d11c27c74454d96e2318a7ac38dd18683ab7" + url: "https://pub.dev" + source: hosted + version: "2.4.0" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "9f2cbcf46d4270ea8be39fa156d86379077c8a5228d9dfdb1164ae0bb93f1faa" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "22e2ecac9419b4246d7c22bfbbda589e3acf5c0351137d87dd2939d984d37c3b" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: "9aee1089b36bd2aafe06582b7d7817fd317ef05fc30e6ba14bff247d0933042a" + url: "https://pub.dev" + source: hosted + version: "2.3.0" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "841ad54f3c8381c480d0c9b508b89a34036f512482c407e6df7a9c4aa2ef8f59" + url: "https://pub.dev" + source: hosted + version: "2.3.2" sky_engine: dependency: transitive description: flutter @@ -304,6 +373,14 @@ packages: url: "https://pub.dev" source: hosted version: "14.2.1" + web: + dependency: transitive + description: + name: web + sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27" + url: "https://pub.dev" + source: hosted + version: "0.5.1" win32: dependency: transitive description: diff --git a/obs_flutter/obs/obs_demo/pubspec.yaml b/obs_flutter/obs/obs_demo/pubspec.yaml index 58a9112..b459e8b 100644 --- a/obs_flutter/obs/obs_demo/pubspec.yaml +++ b/obs_flutter/obs/obs_demo/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: http: ^0.13.3 path_provider: ^2.0.2 + shared_preferences: ^2.2.3 dev_dependencies: flutter_test: