Skip to content

Instantly share code, notes, and snippets.

@wookiee
Created September 13, 2023 12:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wookiee/90132be81bab6a3d8b0ff8357b4f665d to your computer and use it in GitHub Desktop.
Save wookiee/90132be81bab6a3d8b0ff8357b4f665d to your computer and use it in GitHub Desktop.
dis-dart-flutter-fun
import 'package:flutter/material.dart';
void main() => runApp(const SignUpApp());
class SignUpApp extends StatelessWidget {
const SignUpApp();
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
'/': (context) => const SignUpScreen(),
},
);
}
}
class SignUpScreen extends StatelessWidget {
const SignUpScreen();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
body: const Center(
child: SizedBox(
width: 400,
child: Card(
child: SignUpForm(),
),
),
),
);
}
}
class SignUpForm extends StatefulWidget {
const SignUpForm();
@override
State<SignUpForm> createState() => _SignUpFormState();
}
class _SignUpFormState extends State<SignUpForm> {
final _firstNameTextController = TextEditingController();
final _lastNameTextController = TextEditingController();
final _usernameTextController = TextEditingController();
final _passwordTextController = TextEditingController();
double _formProgress = 0;
@override
void initState() {
super.initState();
_firstNameTextController.addListener(_updateFormProgress);
_lastNameTextController.addListener(_updateFormProgress);
_usernameTextController.addListener(_updateFormProgress);
_passwordTextController.addListener(_updateFormProgress);
}
void _updateFormProgress() {
var progress = 0.0;
var controllers = [
_firstNameTextController,
_lastNameTextController,
_usernameTextController,
_passwordTextController
];
for (var controller in controllers) {
if (controller.text.isNotEmpty) {
progress += 1 / controllers.length;
}
}
setState(() {
_formProgress = progress;
});
}
@override
void dispose() {
_firstNameTextController.dispose();
_lastNameTextController.dispose();
_usernameTextController.dispose();
_passwordTextController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Form(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
LinearProgressIndicator(value: _formProgress),
Text('Sign up', style: Theme.of(context).textTheme.headlineMedium),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: _firstNameTextController,
decoration: const InputDecoration(hintText: 'First name'),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: _lastNameTextController,
decoration: const InputDecoration(hintText: 'Last name'),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: _usernameTextController,
decoration: const InputDecoration(hintText: 'Username'),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: TextFormField(
controller: _passwordTextController,
decoration: const InputDecoration(hintText: 'Password'),
),
),
TextButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
return _formProgress != 1.0 ? null : Colors.white;
}),
backgroundColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
return _formProgress != 1.0 ? null : Colors.blue;
}),
),
onPressed: null,
child: const Text('Sign up'),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment