Jump to content

User input/Graphical: Difference between revisions

No edit summary
Line 394:
{{libheader|Flutter}}
 
Displays atwo text fieldfields, a button and an output label
 
<lang javascript>import 'package:flutter/material.dart';
Line 408:
String output = "output"; // This will be displayed in an output text field
 
TextEditingController _inputController_stringInputController = TextEditingController(); // Allows us to get the text from a text field
TextEditingController _numberInputController = TextEditingController();
 
@override
Widget build( BuildContext context ) {
Line 420 ⟶ 421:
SizedBox( height: 25 ), // Space between top and text field
 
TextField ( // InputString input Text Field
controller: _inputController_stringInputController, // Add input controller so we can grab text
textAlign: TextAlign.center, // Center 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
child: Text('Submit TextData'), // Button Text
color: Colors.blue[400] // button color
onPressed: () { // On pressed Callback for button
setState( () {
output = 'output: ${_inputController.text}\nnumber: 75000'; // GrabsReset the text from the input controller and changes the stringoutput
 
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!';
}
 
}
});
},
child: Text('Submit Text'), // Button Text
color: Colors.blue[400] // button color
),
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.