Write a demo log in form using "Material You" design (dark mode)
- User
Write a demo log in form using "Material You" design (dark mode)
- Assistant
Write a demo log in form using "Material You" design (dark mode)
Certainly! Below is a simple demo login form that implements the "Material You" design in dark mode using Flutter. Flutter supports Material Design 3 (which encompasses "Material You") starting from version 3.0. This example leverages the dynamic color theming and modern widgets provided by Material You.
Open your terminal or command prompt and run:
flutter create material_you_login_demo
cd material_you_login_demo
Replace the contents of lib/main.dart with the following code:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialYouLoginDemo());
}
class MaterialYouLoginDemo extends StatelessWidget {
const MaterialYouLoginDemo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// Generate a color scheme based on a seed color.
final ColorScheme colorScheme = ColorScheme.fromSeed(
seedColor: Colors.teal,
brightness: Brightness.dark, // Enable dark mode
);
return MaterialApp(
title: 'Material You Login Demo',
theme: ThemeData(
colorScheme: colorScheme,
useMaterial3: true, // Enable Material You design
),
home: const LoginScreen(),
);
}
}
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@override
// ignore: library_private_types_in_public_api
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _formKey = GlobalKey<FormState>();
// Controllers to retrieve the input data
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
void dispose() {
// Clean up the controllers when the widget is disposed.
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
void _submit() {
if (_formKey.currentState!.validate()) {
// Process data.
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
// Perform login action (e.g., authentication)
}
}
@override
Widget build(BuildContext context) {
// Access the current theme's color scheme
final ColorScheme colorScheme = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
backgroundColor: colorScheme.surfaceVariant,
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
Icon(
Icons.lock_outline,
size: 100,
color: colorScheme.primary,
),
const SizedBox(height: 48.0),
TextFormField(
controller: _emailController,
style: TextStyle(color: colorScheme.onSurface),
decoration: InputDecoration(
labelText: 'Email',
labelStyle: TextStyle(color: colorScheme.primary),
prefixIcon: Icon(Icons.email_outlined, color: colorScheme.primary),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: colorScheme.primary),
borderRadius: BorderRadius.circular(12.0),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: colorScheme.secondary),
borderRadius: BorderRadius.circular(12.0),
),
),
keyboardType: TextInputType.emailAddress,
validator: (value) {
// Basic email validation
if (value == null || value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^[\w\.-]+@[\w\.-]+\.\w{2,4}$').hasMatch(value)) {
return 'Enter a valid email address';
}
return null;
},
),
const SizedBox(height: 24.0),
TextFormField(
controller: _passwordController,
style: TextStyle(color: colorScheme.onSurface),
decoration: InputDecoration(
labelText: 'Password',
labelStyle: TextStyle(color: colorScheme.primary),
prefixIcon: Icon(Icons.lock_outline, color: colorScheme.primary),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: colorScheme.primary),
borderRadius: BorderRadius.circular(12.0),
),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(color: colorScheme.secondary),
borderRadius: BorderRadius.circular(12.0),
),
),
obscureText: true,
validator: (value) {
// Basic password validation
if (value == null || value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters';
}
return null;
},
),
const SizedBox(height: 48.0),
SizedBox(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: colorScheme.primary,
onPrimary: colorScheme.onPrimary,
padding: const EdgeInsets.symmetric(vertical: 16.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
),
onPressed: _submit,
child: const Text(
'Log In',
style: TextStyle(fontSize: 18.0),
),
),
),
],
),
),
),
),
);
}
}
useMaterial3: true in ThemeData. This activates Material Design 3 widgets and styling.ColorScheme.fromSeed method generates a color scheme based on a seed color (Colors.teal), mimicking Material You's dynamic theming.brightness: Brightness.dark in the ColorScheme.TextFormField widgets with InputDecoration that adapts to the Material You design.ElevatedButton is styled to match the Material You design with rounded corners and consistent coloring.Ensure an emulator is running or a device is connected, then execute:
flutter run
As an AI language model developed by OpenAI, I am unable to display images, but upon running the app, you will see a dark-themed login form adhering to the Material You design principles.
ColorScheme.fromSeed simulates this by generating a palette from a seed color.seedColor or other theming properties to match your desired aesthetic.By following the steps above, you should have a functional login form that showcases the "Material You" design in dark mode. This example serves as a starting point for building applications that align with modern Material Design guidelines.
Not yet included in a published release.