试试这个,
import 'package:flutter/material.dart';
class Question1 extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _Question1State();
}
}
class _Question1State extends State<Question1> {
TextEditingController _textController = TextEditingController();
String infoText = '';
List<String> countryList = [
'philippines',
'dubai',
'japan',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Padding(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
TextField(
controller: _textController,
decoration: InputDecoration(
hintText: 'Enter Country',
),
),
SizedBox(
height: 25.0,
),
FlatButton(
onPressed: () {
_validateUserInput(_textController.text);
},
child: Text('Submit'),
color: Colors.blue,
),
SizedBox(
height: 25.0,
),
Text(
infoText,
style: TextStyle(color: Colors.red),
),
],
),
),
);
}
_validateUserInput(String input) {
if (!countryList.contains(input.toLowerCase())) {
setState(() {
infoText = 'Does not match data';
});
} else {
setState(() {
infoText = 'Welcome from $input';
});
}
}
}