Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() {
- runApp(const MyApp());
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- // This widget is the root of your application.
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Flutter Demo',
- theme: ThemeData(
- colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
- ),
- home: MyHomePage(),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- const MyHomePage({super.key});
- @override
- State<MyHomePage> createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- String message = '';
- TextEditingController txtUsername = TextEditingController();
- TextEditingController txtPassword = TextEditingController();
- int rating = 0;
- void login() {
- // ambil username
- String user = txtUsername.text;
- // ambil password
- String pass = txtPassword.text;
- // cek jika username = 'admin' dan password = '1234' maka
- if (user == 'admin' && pass == '1234') {
- setState(() {
- message = 'Login Berhasil';
- });
- } else {
- setState(() {
- message = 'Login Gagal';
- });
- }
- // message = 'Login Berhasil'
- // selain itu message = 'Login Gagal'
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(title: Text('Belajar State')),
- body: Center(
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- children: [
- TextFormField(
- decoration: InputDecoration(hintText: 'Username'),
- controller: txtUsername,
- ),
- TextFormField(
- decoration: InputDecoration(hintText: 'Password'),
- obscureText: true,
- controller: txtPassword,
- ),
- SizedBox(height: 20),
- MyButton(title: 'Login', icon: Icons.login, myFunction: login),
- MyButton(title: 'Register', icon: Icons.app_registration),
- SizedBox(height: 20),
- Text(message),
- Image.network(
- 'https://img.freepik.com/free-psd/modern-car-isolated_23-2151504546.jpg?semt=ais_incoming&w=740&q=80',
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: List.generate(5, (index) {
- return IconButton(
- onPressed: () {
- setState(() {
- rating = index + 1;
- });
- },
- icon: Icon(
- index < rating ? Icons.star : Icons.star_border_outlined,
- color: Colors.red,
- ),
- );
- }),
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
- class MyButton extends StatelessWidget {
- String title;
- Function()? myFunction;
- IconData? icon;
- MyButton({super.key, required this.title, this.myFunction, this.icon});
- @override
- Widget build(BuildContext context) {
- return TextButton(
- onPressed: () {
- myFunction!.call();
- },
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Icon(icon, color: Colors.blue),
- Text(title, style: TextStyle(color: Colors.blue, fontSize: 20)),
- ],
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment