Skip to content

Instantly share code, notes, and snippets.

@igabice
Created January 8, 2020 09:38
Show Gist options
  • Save igabice/ab036017ebb7f155e5601d4d98d48728 to your computer and use it in GitHub Desktop.
Save igabice/ab036017ebb7f155e5601d4d98d48728 to your computer and use it in GitHub Desktop.
Flutter TextField with number input type
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