Ghoul Kaneki
Forum Guru
create a log in page using flutter
bash
flutter create login_page
cd login_page
lib/main.dart file and replace the existing code with the following code:dart
import 'package:flutter/material.dart';
void main() {
runApp(LoginApp());
}
class LoginApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
String username = '';
String password = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login Page'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
decoration: InputDecoration(
labelText: 'Username',
),
onChanged: (value) {
setState(() {
username = value;
});
},
),
SizedBox(height: 20.0),
TextField(
decoration: InputDecoration(
labelText: 'Password',
),
onChanged: (value) {
setState(() {
password = value;
});
},
obscureText: true,
),
SizedBox(height: 20.0),
ElevatedButton(
onPressed: () {
// Add your authentication logic here
print('Username: $username');
print('Password: $password');
},
child: Text('Login'),
),
],
),
),
);
}
}
bash
flutter run
how to add background colorTo create a basic login page using Flutter, you can follow the steps below:
1. Create a new Flutter project:
Code:bash flutter create login_page cd login_page
2. Open thelib/main.dartfile and replace the existing code with the following code:
Code:dart import 'package:flutter/material.dart'; void main() { runApp(LoginApp()); } class LoginApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: LoginPage(), ); } } class LoginPage extends StatefulWidget { @override _LoginPageState createState() => _LoginPageState(); } class _LoginPageState extends State<LoginPage> { String username = ''; String password = ''; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Login Page'), ), body: Padding( padding: EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ TextField( decoration: InputDecoration( labelText: 'Username', ), onChanged: (value) { setState(() { username = value; }); }, ), SizedBox(height: 20.0), TextField( decoration: InputDecoration( labelText: 'Password', ), onChanged: (value) { setState(() { password = value; }); }, obscureText: true, ), SizedBox(height: 20.0), ElevatedButton( onPressed: () { // Add your authentication logic here print('Username: $username'); print('Password: $password'); }, child: Text('Login'), ), ], ), ), ); } }
3. Run the app on an emulator or physical device:
Code:bash flutter run
This code creates a simple login page with text fields for username and password, and a login button. When the login button is pressed, it prints the entered username and password to the console. You can customize this login page further by adding validation, authentication logic, styling, and error handling as per your requirements.
To add a background color to the login page in Flutter, you can modify thehow to add background color
Scaffold widget to include a Container as the background with the desired color. Here's how you can update the code to add a background color:lib/main.dart file in your Flutter project._LoginPageState class to include a background color for the login page. You can wrap the existing Column widget inside a Container widget with a color property set to the desired background color.class _LoginPageState extends State<LoginPage> {
String username = '';
String password = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login Page'),
),
body: Container(
color: Colors.blue, // Set the background color here
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
decoration: InputDecoration(
labelText: 'Username',
),
onChanged: (value) {
setState(() {
username = value;
});
},
),
SizedBox(height: 20.0),
TextField(
decoration: InputDecoration(
labelText: 'Password',
),
onChanged: (value) {
setState(() {
password = value;
});
},
obscureText: true,
),
SizedBox(height: 20.0),
ElevatedButton(
onPressed: () {
// Add your authentication logic here
print('Username: $username');
print('Password: $password');
},
child: Text('Login'),
),
],
),
),
),
);
}
}
flutter run.Colors class in Flutter or by specifying a custom color.