-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
TIVMOF
committed
Apr 24, 2024
1 parent
ba00ea9
commit c2f47db
Showing
5 changed files
with
245 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
// ignore_for_file: prefer_const_constructors | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'pages/home_page.dart'; | ||
import 'pages/login_page.dart'; | ||
|
||
void main() { | ||
WidgetsFlutterBinding.ensureInitialized(); | ||
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]) | ||
.then((_) { | ||
runApp(new MyApp()); | ||
runApp(MyApp()); | ||
}); | ||
} | ||
|
||
class MyApp extends StatelessWidget { | ||
const MyApp({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
debugShowCheckedModeBanner: false, | ||
home: HomePage(), | ||
home: LoginPage(), // Always start with the login page | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'dart:convert'; | ||
import 'home_page.dart'; // Adjust if needed | ||
|
||
class LoginPage extends StatefulWidget { | ||
@override | ||
_LoginPageState createState() => _LoginPageState(); | ||
} | ||
|
||
class _LoginPageState extends State<LoginPage> { | ||
final TextEditingController _usernameController = TextEditingController(); | ||
final TextEditingController _passwordController = TextEditingController(); | ||
|
||
Future<void> _login() async { | ||
final username = _usernameController.text.trim(); | ||
final password = _passwordController.text.trim(); | ||
|
||
if (username.isEmpty || password.isEmpty) { | ||
// Show an error message | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text("Please enter both username and password")), | ||
); | ||
return; | ||
} | ||
|
||
// Example backend authentication (adjust endpoint and request parameters as needed) | ||
final response = await http.post( | ||
Uri.parse('https://example.com/api/login'), // Update with your endpoint | ||
headers: {'Content-Type': 'application/json'}, | ||
body: json.encode({ | ||
'username': username, | ||
'password': password, | ||
}), | ||
); | ||
|
||
if (response.statusCode == 200) { | ||
// Successful login, navigate to the Home Page | ||
Navigator.pushReplacement( | ||
context, | ||
MaterialPageRoute(builder: (context) => HomePage()), | ||
); | ||
} else { | ||
// Login failed | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text("Login failed. Please try again.")), | ||
); | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: Text("Login")), | ||
body: Padding( | ||
padding: const EdgeInsets.all(16.0), | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
TextField( | ||
controller: _usernameController, | ||
decoration: InputDecoration(labelText: "Username"), | ||
), | ||
TextField( | ||
controller: _passwordController, // Corrected variable name | ||
obscureText: true, | ||
decoration: InputDecoration(labelText: "Password"), | ||
), | ||
SizedBox(height: 16), | ||
ElevatedButton( | ||
onPressed: _login, | ||
child: Text("Login"), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters