Created
May 9, 2024 01:35
-
-
Save oluyoung/e0a80a3f170e248d16059856895e4805 to your computer and use it in GitHub Desktop.
Generated code from pixels2flutter.dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: BlocProvider( | |
create: (context) => UserBloc(), | |
child: SignUpScreen(), | |
), | |
); | |
} | |
} | |
class SignUpScreen extends StatefulWidget { | |
@override | |
_SignUpScreenState createState() => _SignUpScreenState(); | |
} | |
class _SignUpScreenState extends State<SignUpScreen> { | |
final TextEditingController _firstNameController = TextEditingController(); | |
final TextEditingController _lastNameController = TextEditingController(); | |
final TextEditingController _dobController = TextEditingController(); | |
bool _termsAccepted = false; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Padding( | |
padding: EdgeInsets.all(16.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
Text( | |
'Sign Up', | |
style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold), | |
), | |
SizedBox(height: 16), | |
Text( | |
'Hi, we just need a few details about you', | |
style: TextStyle(fontSize: 16), | |
), | |
SizedBox(height: 32), | |
TextField( | |
controller: _firstNameController, | |
decoration: InputDecoration( | |
labelText: 'First Name', | |
border: OutlineInputBorder(), | |
), | |
onChanged: (value) { | |
// Handle change | |
}, | |
), | |
SizedBox(height: 16), | |
TextField( | |
controller: _lastNameController, | |
decoration: InputDecoration( | |
labelText: 'Last Name', | |
border: OutlineInputBorder(), | |
), | |
onChanged: (value) { | |
// Handle change | |
}, | |
), | |
SizedBox(height: 16), | |
TextField( | |
controller: _dobController, | |
decoration: InputDecoration( | |
labelText: 'Date of birth', | |
border: OutlineInputBorder(), | |
), | |
onChanged: (value) { | |
// Handle change | |
}, | |
), | |
SizedBox(height: 16), | |
Row( | |
children: [ | |
Checkbox( | |
value: _termsAccepted, | |
onChanged: (bool? newValue) { | |
setState(() { | |
_termsAccepted = newValue!; | |
}); | |
}, | |
), | |
Expanded( | |
child: GestureDetector( | |
onTap: () { | |
setState(() { | |
_termsAccepted = !_termsAccepted; | |
}); | |
}, | |
child: Text( | |
'By clicking sign up you agree to our terms and conditions', | |
style: TextStyle(fontSize: 14), | |
), | |
), | |
), | |
], | |
), | |
SizedBox(height: 32), | |
ElevatedButton( | |
style: ElevatedButton.styleFrom( | |
primary: Colors.green, | |
minimumSize: Size(double.infinity, 50), | |
), | |
onPressed: _termsAccepted ? _onSignUpPressed : null, | |
child: Text('Sign Up'), | |
), | |
], | |
), | |
), | |
); | |
} | |
void _onSignUpPressed() { | |
if (_termsAccepted) { | |
BlocProvider.of<UserBloc>(context).add( | |
UserEvent.register( | |
_firstNameController.text, | |
_lastNameController.text, | |
_dobController.text, | |
), | |
); | |
} | |
} | |
@override | |
void dispose() { | |
_firstNameController.dispose(); | |
_lastNameController.dispose(); | |
_dobController.dispose(); | |
super.dispose(); | |
} | |
} | |
// User Bloc | |
class UserBloc extends Bloc<UserEvent, UserState> { | |
UserBloc() : super(UserInitial()); | |
@override | |
Stream<UserState> mapEventToState(UserEvent event) async* { | |
if (event is UserRegister) { | |
// Implement registration logic | |
yield UserRegistered(); | |
} | |
} | |
} | |
// User States | |
abstract class UserState {} | |
class UserInitial extends UserState {} | |
class UserRegistered extends UserState {} | |
// User Events | |
abstract class UserEvent { | |
const UserEvent(); | |
factory UserEvent.register(String firstName, String lastName, String dob) { | |
return UserRegister(firstName, lastName, dob); | |
} | |
} | |
class UserRegister extends UserEvent { | |
final String firstName; | |
final String lastName; | |
final String dob; | |
UserRegister(this.firstName, this.lastName, this.dob); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment