Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:project_api/ui/login_screen.dart';
- import 'package:project_api/ui/main_screen.dart';
- import 'ui/register_screen.dart';
- void main() {
- runApp(const MyApp());
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
- useMaterial3: true,
- ),
- home: RegisterScreen(),
- debugShowCheckedModeBanner: false,
- routes:
- {
- '/register' : (context) => RegisterScreen(),
- '/login' : (context) => LoginScreen(),
- }
- );
- }
- }
- =================================Login Screen =======================================
- import 'package:flutter/material.dart';
- import 'main_screen.dart';
- class LoginScreen extends StatefulWidget {
- const LoginScreen({super.key});
- @override
- State<LoginScreen> createState() => _LoginScreenState();
- }
- class _LoginScreenState extends State<LoginScreen> {
- final usernameController = TextEditingController();
- final passwordController = TextEditingController();
- var isSignedIn = false;
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Image.asset(
- 'assets/images/logo.png',
- fit: BoxFit.cover,
- width: 100.0,
- ),
- SizedBox(
- height: 10.0,
- ),
- Text(
- 'Login to your account',
- style: TextStyle(
- fontFamily: "Oswald",
- fontSize: 24.0,
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 16.0,
- ),
- child: TextFormField(
- keyboardType: TextInputType.emailAddress,
- controller: usernameController,
- decoration: InputDecoration(
- labelText: 'Username',
- labelStyle: TextStyle(
- fontFamily: "Oswald",
- ),
- border: OutlineInputBorder(),
- ),
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 16.0,
- ),
- child: TextFormField(
- keyboardType: TextInputType.visiblePassword,
- controller: passwordController,
- enableSuggestions: false,
- autocorrect: false,
- obscureText: true,
- decoration: InputDecoration(
- labelText: 'Password',
- labelStyle: TextStyle(
- fontFamily: "Oswald",
- ),
- border: OutlineInputBorder(),
- ),
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 16.0,
- ),
- child: SizedBox(
- width: double.infinity,
- child: ElevatedButton(
- onPressed: () {
- setState(
- () {
- //kondisi bila username null
- if (usernameController.text.isEmpty) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text('Username cannot be empty'),
- ),
- );
- } else {
- isSignedIn = true;
- }
- if (passwordController.text.isEmpty) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(
- content: Text('Password cannot be empty'),
- ),
- );
- } else {
- isSignedIn = true;
- }
- if (usernameController.text.isNotEmpty &&
- passwordController.text.isNotEmpty) {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => MainScreen(
- username: usernameController.text,
- password: passwordController.text,
- ),
- ),
- );
- }
- },
- );
- },
- style: ButtonStyle(
- elevation: WidgetStateProperty.all(5.0),
- backgroundColor: WidgetStateProperty.all(
- Colors.blue,
- ),
- ),
- child: Text(
- 'Login',
- style: TextStyle(fontFamily: "Oswald", color: Colors.white),
- ),
- ),
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- ElevatedButton(
- onPressed: () {
- Navigator.pushNamed(context, '/register');
- },
- style: ButtonStyle(
- elevation: WidgetStatePropertyAll(
- 5.0,
- ),
- backgroundColor: WidgetStatePropertyAll(
- Colors.red,
- ),
- ),
- child: Text(
- 'Register',
- style: TextStyle(
- color: Colors.white,
- fontFamily: "Oswald",
- ),
- ),
- ),
- ],
- )),
- );
- }
- }
- =============================================================== MAIN SCREEN ===============================================================
- import 'package:flutter/material.dart';
- class MainScreen extends StatelessWidget {
- const MainScreen({super.key, this.username, this.password});
- final String? username;
- final String? password;
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Icon(
- Icons.person,
- size: 100.0,
- ),
- Text(
- 'Welcome $username!',
- style: TextStyle(
- fontFamily: 'Oswald',
- fontSize: 18.0,
- letterSpacing: 2.0,
- ),
- ),
- Text(
- 'Your password is $password',
- style: TextStyle(
- fontFamily: 'Oswald',
- fontSize: 18.0,
- letterSpacing: 2.0,
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
- =============================================================== REGISTER SCREEN ===============================================================
- import 'package:flutter/material.dart';
- class RegisterScreen extends StatefulWidget {
- const RegisterScreen({super.key});
- @override
- State<RegisterScreen> createState() => _RegisterScreenState();
- }
- class _RegisterScreenState extends State<RegisterScreen> {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Image.asset(
- 'assets/images/logo.png',
- fit: BoxFit.cover,
- width: 100.0,
- ),
- Text(
- 'Register to create an account',
- style: TextStyle(
- fontFamily: "Oswald",
- fontSize: 18.0,
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 16.0,
- ),
- child: TextFormField(
- keyboardType: TextInputType.emailAddress,
- decoration: InputDecoration(
- labelText: 'Email',
- labelStyle: TextStyle(
- fontFamily: "Oswald",
- ),
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(25.0),
- ),
- ),
- style: TextStyle(
- fontFamily: "Oswald",
- ),
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 16.0,
- ),
- child: Row(
- children: <Widget>[
- Expanded(
- flex: 1,
- child: TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: 'First Name',
- labelStyle: TextStyle(
- fontFamily: "Oswald",
- ),
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(25.0),
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- flex: 1,
- child: TextFormField(
- keyboardType: TextInputType.text,
- decoration: InputDecoration(
- labelText: 'Last Name',
- labelStyle: TextStyle(
- fontFamily: "Oswald",
- ),
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(25.0),
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: EdgeInsets.symmetric(
- horizontal: 16.0,
- ),
- child: TextFormField(
- keyboardType: TextInputType.visiblePassword,
- obscureText: true,
- autocorrect: false,
- enableSuggestions: false,
- decoration: InputDecoration(
- labelText: 'Password',
- labelStyle: TextStyle(
- fontFamily: "Oswald",
- ),
- border: OutlineInputBorder(
- borderRadius: BorderRadius.circular(25.0),
- ),
- ),
- ),
- ),
- SizedBox(
- height: 10.0,
- ),
- Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 16.0,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- Expanded(
- flex: 1,
- child: ElevatedButton(
- onPressed: () {
- Navigator.pushNamed(context, '/login');
- },
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.blue,
- ),
- child: Text(
- 'Login',
- style: TextStyle(
- color: Colors.white,
- fontFamily: 'Oswald',
- ),
- ),
- ),
- ),
- SizedBox(
- width: 10.0,
- ),
- Expanded(
- flex: 2,
- child: ElevatedButton(
- onPressed: () {},
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.red,
- ),
- child: Text(
- 'Register',
- style: TextStyle(
- color: Colors.white,
- fontFamily: 'Oswald',
- ),
- ),
- ),
- ),
- ],
- ),
- )
- ],
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment