Skip to content

Instantly share code, notes, and snippets.

@rubywai
Created March 8, 2025 09:27
Show Gist options
  • Save rubywai/5c6cf925322a422a62f4a6b87626daed to your computer and use it in GitHub Desktop.
Save rubywai/5c6cf925322a422a62f4a6b87626daed to your computer and use it in GitHub Desktop.
login_form_with_page_view
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MultiStepForm(),
);
}
}
class MultiStepForm extends StatefulWidget {
@override
_MultiStepFormState createState() => _MultiStepFormState();
}
class _MultiStepFormState extends State<MultiStepForm> {
final PageController _pageController = PageController();
final _formKey = GlobalKey<FormState>();
int _currentIndex = 0;
final TextEditingController _nameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
void _nextPage() {
if (_formKey.currentState!.validate()) {
if (_currentIndex < 2) {
_pageController.nextPage(
duration: Duration(milliseconds: 300), curve: Curves.easeInOut);
}
}
}
void _prevPage() {
if (_currentIndex > 0) {
_pageController.previousPage(
duration: Duration(milliseconds: 300), curve: Curves.easeInOut);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
// Background Gradient
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue.shade300, Colors.blue.shade800],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
),
Center(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
margin: EdgeInsets.symmetric(horizontal: 20),
elevation: 8,
child: Container(
width: 350,
height: 500,
padding: EdgeInsets.all(20),
child: Column(
children: [
SizedBox(height: 10),
Expanded(
child: Form(
key: _formKey,
child: PageView(
controller: _pageController,
onPageChanged: (index) {
setState(() {
_currentIndex = index;
});
},
physics: NeverScrollableScrollPhysics(),
children: [
_buildStep1(),
_buildStep2(),
_buildStep3(),
],
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
if (_currentIndex > 0)
TextButton(
onPressed: _prevPage,
child: Text("Back", style: TextStyle(fontSize: 16)),
),
ElevatedButton(
onPressed: _currentIndex == 2 ? _submitForm : _nextPage,
child: Text(
_currentIndex == 2 ? "Submit" : "Next",
style: TextStyle(fontSize: 16),
),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
],
),
],
),
),
),
),
],
),
);
}
Widget _buildStep1() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Step 1", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
SizedBox(height: 20),
Text("Enter your full name below", textAlign: TextAlign.center),
SizedBox(height: 10),
TextFormField(
controller: _nameController,
decoration: InputDecoration(labelText: "Full Name", border: OutlineInputBorder()),
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter your full name";
}
return null;
},
),
],
);
}
Widget _buildStep2() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Step 2", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
SizedBox(height: 20),
Text("Provide a valid email address", textAlign: TextAlign.center),
SizedBox(height: 10),
TextFormField(
controller: _emailController,
decoration: InputDecoration(labelText: "Email", border: OutlineInputBorder()),
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter your email";
}
if (!value.contains('@') || !value.contains('.')) {
return "Please enter a valid email";
}
return null;
},
),
],
);
}
Widget _buildStep3() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Step 3", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
SizedBox(height: 20),
Text("Set a strong password", textAlign: TextAlign.center),
SizedBox(height: 10),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(labelText: "Password", border: OutlineInputBorder()),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return "Please enter your password";
}
if (value.length < 6) {
return "Password must be at least 6 characters long";
}
return null;
},
),
],
);
}
void _submitForm() {
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Form Submitted Successfully!")),
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment