R_Habibie

belajar state

Sep 18th, 2025 (edited)
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.53 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() {
  4.   runApp(const MyApp());
  5. }
  6.  
  7. class MyApp extends StatelessWidget {
  8.   const MyApp({super.key});
  9.  
  10.   // This widget is the root of your application.
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     return MaterialApp(
  14.       title: 'Flutter Demo',
  15.       theme: ThemeData(
  16.         colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  17.       ),
  18.       home: MyHomePage(),
  19.     );
  20.   }
  21. }
  22.  
  23. class MyHomePage extends StatefulWidget {
  24.   const MyHomePage({super.key});
  25.  
  26.   @override
  27.   State<MyHomePage> createState() => _MyHomePageState();
  28. }
  29.  
  30. class _MyHomePageState extends State<MyHomePage> {
  31.   String message = '';
  32.   TextEditingController txtUsername = TextEditingController();
  33.   TextEditingController txtPassword = TextEditingController();
  34.   int rating = 0;
  35.  
  36.   void login() {
  37.     // ambil username
  38.     String user = txtUsername.text;
  39.     // ambil password
  40.     String pass = txtPassword.text;
  41.     // cek jika username = 'admin' dan password = '1234' maka
  42.     if (user == 'admin' && pass == '1234') {
  43.       setState(() {
  44.         message = 'Login Berhasil';
  45.       });
  46.     } else {
  47.       setState(() {
  48.         message = 'Login Gagal';
  49.       });
  50.     }
  51.     // message = 'Login Berhasil'
  52.     // selain itu message = 'Login Gagal'
  53.   }
  54.  
  55.   @override
  56.   Widget build(BuildContext context) {
  57.     return Scaffold(
  58.       appBar: AppBar(title: Text('Belajar State')),
  59.       body: Center(
  60.         child: Padding(
  61.           padding: const EdgeInsets.all(16.0),
  62.           child: Column(
  63.             children: [
  64.               TextFormField(
  65.                 decoration: InputDecoration(hintText: 'Username'),
  66.                 controller: txtUsername,
  67.               ),
  68.               TextFormField(
  69.                 decoration: InputDecoration(hintText: 'Password'),
  70.                 obscureText: true,
  71.                 controller: txtPassword,
  72.               ),
  73.               SizedBox(height: 20),
  74.               MyButton(title: 'Login', icon: Icons.login, myFunction: login),
  75.               MyButton(title: 'Register', icon: Icons.app_registration),
  76.               SizedBox(height: 20),
  77.               Text(message),
  78.               Image.network(
  79.                 'https://img.freepik.com/free-psd/modern-car-isolated_23-2151504546.jpg?semt=ais_incoming&w=740&q=80',
  80.               ),
  81.               Row(
  82.                 mainAxisAlignment: MainAxisAlignment.center,
  83.                 children: List.generate(5, (index) {
  84.                   return IconButton(
  85.                     onPressed: () {
  86.                       setState(() {
  87.                         rating = index + 1;
  88.                       });
  89.                     },
  90.                     icon: Icon(
  91.                       index < rating ? Icons.star : Icons.star_border_outlined,
  92.                       color: Colors.red,
  93.                     ),
  94.                   );
  95.                 }),
  96.               ),
  97.             ],
  98.           ),
  99.         ),
  100.       ),
  101.     );
  102.   }
  103. }
  104.  
  105. class MyButton extends StatelessWidget {
  106.   String title;
  107.   Function()? myFunction;
  108.   IconData? icon;
  109.   MyButton({super.key, required this.title, this.myFunction, this.icon});
  110.  
  111.   @override
  112.   Widget build(BuildContext context) {
  113.     return TextButton(
  114.       onPressed: () {
  115.         myFunction!.call();
  116.       },
  117.       child: Row(
  118.         mainAxisAlignment: MainAxisAlignment.center,
  119.         children: [
  120.           Icon(icon, color: Colors.blue),
  121.           Text(title, style: TextStyle(color: Colors.blue, fontSize: 20)),
  122.         ],
  123.       ),
  124.     );
  125.   }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment