Skip to content

Commit

Permalink
Add validation to the dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
rusiruavb committed Aug 2, 2021
1 parent 9813ebf commit 80272f5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 27 deletions.
Binary file modified assets/images/home_top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
165 changes: 140 additions & 25 deletions lib/components/rounded_dropdown_feild.dart
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
import 'package:dropdown_formfield/dropdown_formfield.dart';
import 'package:flutter/material.dart';

class RoundedDropDownFeild extends StatefulWidget {
final ValueChanged<String> onChange;
final String text;
final bool isRequiredFeild;
final bool isCountry;

const RoundedDropDownFeild({
Key key,
this.isRequiredFeild,
this.onChange,
this.text,
});
this.isCountry,
}) : super(key: key);

@override
_RoundedDropDownFeildState createState() => _RoundedDropDownFeildState(
isRequiredFeild: isRequiredFeild,
onChange: onChange,
text: text,
isCountry: isCountry,
);
}

class _RoundedDropDownFeildState extends State<RoundedDropDownFeild> {
final ValueChanged<String> onChange;
final String text;
final bool isRequiredFeild;
String _country = 'Sri Lanka';
final bool isCountry;
bool _isFieldValid;

_RoundedDropDownFeildState({
Key key,
this.isRequiredFeild,
this.onChange,
this.text,
this.isCountry,
});

@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;

return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
Expand All @@ -55,29 +58,113 @@ class _RoundedDropDownFeildState extends State<RoundedDropDownFeild> {
height: size.height * 0.005,
),
DropDownContainer(
child: DropdownButtonFormField<String>(
items: data
.map(
(item) => DropdownMenuItem(
child: Text(
item,
style: TextStyle(
fontFamily: 'Larsseit',
fontSize: 20,
),
child: isCountry != null && isCountry
? DropdownButtonFormField<String>(
validator: (value) {
if (value == null) {
value = '';
}
if (value.isEmpty) {
setState(() {
_isFieldValid = false;
});
return null;
} else {
setState(() {
_isFieldValid = true;
});
return null;
}
},
items: countries
.map(
(item) => DropdownMenuItem(
child: Text(
item,
style: TextStyle(
fontFamily: 'Larsseit',
fontSize: 20,
),
),
value: item,
),
)
.toList(),
hint: Text(
'Select your country',
style: TextStyle(
fontFamily: 'Larsseit',
fontSize: 20,
color: Colors.grey[600],
),
value: item,
),
decoration: InputDecoration(
border: InputBorder.none,
),
onChanged: onChange,
)
.toList(),
hint: Text('Select your country'),
decoration: InputDecoration(
border: InputBorder.none,
),
onChanged: (value) {
print(value);
},
),
: DropdownButtonFormField<String>(
validator: (value) {
if (value == null) {
value = '';
}
if (value != null && value.isEmpty) {
setState(() {
_isFieldValid = false;
});
return null;
} else {
setState(() {
_isFieldValid = true;
});
return null;
}
},
items: titles
.map(
(item) => DropdownMenuItem(
child: Text(
item,
style: TextStyle(
fontFamily: 'Larsseit',
fontSize: 20,
),
),
value: item,
),
)
.toList(),
hint: Text(
'Select your title',
style: TextStyle(
fontFamily: 'Larsseit',
fontSize: 20,
color: Colors.grey[600],
),
),
decoration: InputDecoration(
border: InputBorder.none,
),
onChanged: onChange,
),
),
isRequiredFeild != null &&
isRequiredFeild &&
_isFieldValid != null &&
!_isFieldValid
? Padding(
padding: EdgeInsets.only(left: 15),
child: Text(
text + ' is required!',
style: TextStyle(
color: Colors.red[800],
fontFamily: 'Larsseit',
),
),
)
: Text(''),
SizedBox(
height: size.height * 0.010,
),
],
),
Expand Down Expand Up @@ -108,4 +195,32 @@ class DropDownContainer extends StatelessWidget {
}
}

List<String> data = ['Sri Lanka', 'India'];
List<String> countries = [
'๐Ÿ‡ฑ๐Ÿ‡ฐ Sri Lanka',
'๐Ÿ‡ฎ๐Ÿ‡ณ India',
'๐Ÿ‡ง๐Ÿ‡ฉ Bangaladesh',
'๐Ÿ‡ต๐Ÿ‡ฐ Pakistan',
'๐Ÿ‡ณ๐Ÿ‡ต Nepal',
'๐Ÿ‡จ๐Ÿ‡ณ China',
'๐Ÿ‡ฏ๐Ÿ‡ต Japan',
'๐Ÿ‡ท๐Ÿ‡บ Russia',
'๐Ÿ‡ณ๐Ÿ‡ฟ New Zealand',
'๐Ÿ‡ฆ๐Ÿ‡บ Australia',
'๐Ÿ‡บ๐Ÿ‡ธ United States',
'๐Ÿ‡ฌ๐Ÿ‡ง United Kingdom',
'๐Ÿ‡ฎ๐Ÿ‡น Italy'
];

List<String> titles = [
'๐Ÿง” Mr.',
'๐Ÿ‘ฉ Mrs.',
'๐Ÿง‘โ€๐Ÿฆฑ Mast.',
'๐Ÿ‘ฑโ€โ™€๏ธ Miss',
'๐Ÿ‘จโ€โš•๏ธ Dr.',
'๐Ÿ‘จโ€โš•๏ธ Dr (Ms.)',
'๐Ÿ‘ฉโ€โš•๏ธ Dr (Mrs.)',
'๐Ÿ‘จโ€๐Ÿ”ฌ Prof.',
'๐Ÿ‘จโ€๐Ÿ”ฌ Prof (Ms.)',
'๐Ÿ‘ฉโ€๐Ÿ”ฌ Prof (Mrs.)',
'๐Ÿ‘ถ Baby'
];
5 changes: 3 additions & 2 deletions lib/screens/signup/signup_form_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ class _SignUpForm1State extends State<SignUpForm1> {
),
RoundedDropDownFeild(
text: 'Your Country',
isRequiredFeild: false,
isRequiredFeild: true,
isCountry: true,
onChange: (text) {
print('this is test');
print('this is test' + text);
},
),
RoundedTextFeild(
Expand Down

0 comments on commit 80272f5

Please sign in to comment.