User input/Graphical: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 394: | Line 394: | ||
{{libheader|Flutter}} |
{{libheader|Flutter}} |
||
Displays |
Displays two text fields, a button and an output label |
||
<lang javascript>import 'package:flutter/material.dart'; |
<lang javascript>import 'package:flutter/material.dart'; |
||
Line 408: | Line 408: | ||
String output = "output"; // This will be displayed in an output text field |
String output = "output"; // This will be displayed in an output text field |
||
TextEditingController |
TextEditingController _stringInputController = TextEditingController(); // Allows us to get the text from a text field |
||
TextEditingController _numberInputController = TextEditingController(); |
|||
@override |
@override |
||
Widget build( BuildContext context ) { |
Widget build( BuildContext context ) { |
||
Line 420: | Line 421: | ||
SizedBox( height: 25 ), // Space between top and text field |
SizedBox( height: 25 ), // Space between top and text field |
||
TextField ( // |
TextField ( // String input Text Field |
||
controller: |
controller: _stringInputController, // Add input controller so we can grab text |
||
textAlign: TextAlign.center, // Center text |
textAlign: TextAlign.center, // Center text |
||
decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Enter a string...'), // Border and default text |
decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Enter a string...'), // Border and default text |
||
), |
|||
SizedBox( height: 10 ), // Space between text fields |
|||
TextField ( // Number input Text Field |
|||
controller: _numberInputController, // Add input controller so we can grab text |
|||
textAlign: TextAlign.center, // Center text |
|||
decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Enter 75000'), // Border and default text |
|||
), |
), |
||
FlatButton ( // Submit Button |
FlatButton ( // Submit Button |
||
⚫ | |||
⚫ | |||
onPressed: () { // On pressed Callback for button |
onPressed: () { // On pressed Callback for button |
||
setState( () { |
setState( () { |
||
output = ' |
output = ''; // Reset output |
||
int number; // Int to store number in |
|||
var stringInput = _stringInputController.text ?? ''; // Get the input from the first field, if it is null set it to an empty string |
|||
var numberString = _numberInputController.text ?? ''; // Get the input from the second field, if it is null set it to an empty string |
|||
if ( stringInput == '') { // If first field is empty |
|||
output = 'Please enter something in field 1\n'; |
|||
return; |
|||
} |
|||
if (_numberInputController.text == '') { // If second field is empty |
|||
output += 'Please enter something in field 2'; |
|||
return; |
|||
} else { // If we got an input in the second field |
|||
try { |
|||
number = int.parse( numberString ); // Parse numberString into an int |
|||
if ( number == 75000 ) |
|||
output = 'text output: $stringInput\nnumber: $number'; // Grabs the text from the input controllers and changes the string |
|||
else |
|||
output = '$number is not 75000!'; |
|||
} on FormatException { // If a number is not entered in second field |
|||
output = '$numberString is not a number!'; |
|||
} |
|||
} |
|||
}); |
}); |
||
} |
} |
||
⚫ | |||
⚫ | |||
), |
), |
||