Advertisement
yocky12k

flutter navigasi lanjutan

Nov 28th, 2023
1,321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.50 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:fluttertoast/fluttertoast.dart';
  3. void main() {
  4.   runApp(MainApp());
  5. }
  6.  
  7. class MainApp extends StatelessWidget {
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return MaterialApp(
  11.       debugShowCheckedModeBanner: false,
  12.       home: HalamanPertama(),
  13.     );
  14.   }
  15. }
  16.  
  17. class HalamanPertama extends StatelessWidget {
  18.   TextEditingController username = TextEditingController();
  19.   TextEditingController password = TextEditingController();
  20.   @override
  21.   Widget build(BuildContext context) {
  22.     return Scaffold(
  23.       appBar: AppBar(
  24.         title: Text('Halaman Satu'),
  25.       ),
  26.       body: Center(
  27.         child: Column(
  28.           mainAxisAlignment: MainAxisAlignment.center,
  29.           children: [
  30.             Text(
  31.               'Silakan login untuk mengakses halaman ini!',
  32.               style: TextStyle(
  33.                 fontSize: 15,
  34.                 fontWeight: FontWeight.bold,
  35.                 color: Colors.blue,
  36.               ),
  37.             ),
  38.             SizedBox(height: 30),
  39.             Padding(
  40.               padding: const EdgeInsets.fromLTRB(25, 0, 25, 10),
  41.               child: TextField(
  42.                 controller: username,
  43.                 decoration: InputDecoration(
  44.                   prefixIcon: Icon(Icons.person),
  45.                   labelText: 'Username',
  46.                   hintText: 'Masukkan username Anda',
  47.                   border: OutlineInputBorder(),
  48.                 ),
  49.               ),
  50.             ),
  51.             Padding(
  52.               padding: const EdgeInsets.fromLTRB(25, 0, 25, 20),
  53.               child: TextField(
  54.                 controller: password,
  55.                 obscureText: true,
  56.                 decoration: InputDecoration(
  57.                   prefixIcon: Icon(Icons.password),
  58.                   labelText: 'Password',
  59.                   hintText: 'Masukkan password Anda',
  60.                   border: OutlineInputBorder(),
  61.                 ),
  62.               ),
  63.             ),
  64.             Row(
  65.               mainAxisAlignment: MainAxisAlignment.center,
  66.               children: [
  67.                 ElevatedButton(
  68.                   onPressed: () {
  69.                     String uname = username.text;
  70.                     String pass = password.text;
  71.  
  72.                     //logika percabangan
  73.                     if (uname == 'admin' && pass == '12345') {
  74.                       //logika login sukses
  75.                       Navigator.push(
  76.                         context,
  77.                         MaterialPageRoute(builder: (context) => HalamanKedua(data: uname)),
  78.                       );
  79.                       Fluttertoast.showToast(
  80.                         msg: 'Login Sukses',
  81.                         toastLength: Toast.LENGTH_LONG,
  82.                       );
  83.                     } else {
  84.                       //logika login gagal
  85.                       Fluttertoast.showToast(
  86.                         msg: 'Login Gagal!! Silakan periksa kembali username dan password Anda',
  87.                         toastLength: Toast.LENGTH_LONG,
  88.                       );
  89.                     }
  90.                   },
  91.                   child: Text('LOGIN'),
  92.                 ),
  93.                 SizedBox(width: 20),
  94.                 ElevatedButton(
  95.                   onPressed: () {
  96.                     //logic ketika tombol di tekan
  97.                     username.clear();
  98.                     password.clear();
  99.                   },
  100.                   child: Text('RESET'),
  101.                 ),
  102.               ],
  103.             ),
  104.           ],
  105.         ),
  106.       ),
  107.     );
  108.   }
  109. }
  110.  
  111. class HalamanKedua extends StatelessWidget {
  112.   final String data;
  113.   //menerima data dari halaman pertama
  114.   HalamanKedua({required this.data});
  115.   @override
  116.   Widget build(BuildContext context) {
  117.     return Scaffold(
  118.       appBar: AppBar(
  119.         title: Text('Halaman Dua'),
  120.       ),
  121.       body: Center(
  122.         child: Column(
  123.           mainAxisAlignment: MainAxisAlignment.center,
  124.           children: [
  125.             Text(
  126.               'Selamat Datang ' + data,
  127.               style: TextStyle(
  128.                 fontSize: 20,
  129.                 fontWeight: FontWeight.bold,
  130.                 color: Colors.orange,
  131.               ),
  132.             ),
  133.             SizedBox(height: 35),
  134.             ElevatedButton(
  135.               onPressed: () {
  136.                 //logic ketika tombol di tekan
  137.                 Navigator.pop(context);
  138.               },
  139.               child: Text('Kembali'),
  140.             ),
  141.           ],
  142.         ),
  143.       ),
  144.     );
  145.   }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement