Created
January 8, 2020 09:38
-
-
Save igabice/ab036017ebb7f155e5601d4d98d48728 to your computer and use it in GitHub Desktop.
Flutter TextField with number input type
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
bool _numberInputIsValid = true; | |
Widget _buildNumberTextField() { | |
return TextField( | |
keyboardType: TextInputType.number, | |
style: Theme.of(context).textTheme.display1, | |
decoration: InputDecoration( | |
icon: Icon(Icons.attach_money), | |
labelText: 'Enter an integer:', | |
errorText: _numberInputIsValid ? null : 'Please enter an integer!', | |
border: OutlineInputBorder( | |
borderRadius: BorderRadius.all(Radius.circular(10.0)), | |
), | |
), | |
onSubmitted: (val) => | |
Fluttertoast.showToast(msg: 'You entered: ${int.parse(val)}'), | |
onChanged: (String val) { | |
final v = int.tryParse(val); | |
debugPrint('parsed value = $v'); | |
if (v == null) { | |
setState(() => _numberInputIsValid = false); | |
} else { | |
setState(() => _numberInputIsValid = true); | |
} | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment