Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user validation #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 83 additions & 56 deletions obs_flutter/obs/obs_demo/lib/CreateUserPage.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +11,7 @@ class CreateUserPage extends StatefulWidget {
class _CreateUserPageState extends State<CreateUserPage> {
String? _selectedLanguage;
String? _userName;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
Expand All @@ -18,66 +20,91 @@ class _CreateUserPageState extends State<CreateUserPage> {
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<String>(
value: _selectedLanguage,
items: <String>['English', 'Spanish', 'French']
.map((String value) {
return DropdownMenuItem<String>(
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<String>(
validator: (value) {
if (value == null) {
return 'Please select a language';
}
return null;
},
value: _selectedLanguage,
items: <String>['English', 'Spanish', 'French']
.map((String value) {
return DropdownMenuItem<String>(
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'),
),
],
),
),
),
),
Expand Down
46 changes: 31 additions & 15 deletions obs_flutter/obs/obs_demo/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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(),
);
}
}
12 changes: 0 additions & 12 deletions obs_flutter/obs/obs_demo/lib/screen/bottomNavi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ class _BottomNavigationBarExampleState
rowIndex: 0,
onUpdateTextAvailability: onUpdateTextAvailability,
),
// Text(
// 'Audio',
// style: optionStyle,
// ),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expand Down Expand Up @@ -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),
Expand All @@ -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: '',
Expand Down
4 changes: 4 additions & 0 deletions obs_flutter/obs/obs_demo/lib/screen/dashboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ class _DashboardState extends State<Dashboard> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('OBS Translator'),
centerTitle: true,
),
body: FutureBuilder<List<Map<String, dynamic>>>(
future: loadData(), // Reference to the async function
builder: (context, snapshot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
77 changes: 77 additions & 0 deletions obs_flutter/obs/obs_demo/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions obs_flutter/obs/obs_demo/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies:

http: ^0.13.3
path_provider: ^2.0.2
shared_preferences: ^2.2.3

dev_dependencies:
flutter_test:
Expand Down