How To Random Select A String From List After Tabbing Button
I would like to press a button and pick a random string from my list to display somewhere on the screen.
Currently, the convoTopic variable within the builder is running an error.
Any help is appreciated!
Below is my truncated code:
final List<String> ConvoTopics = [
'blah blah',
'black sheep',
'balh bath'
];
class ConvoPage extends StatefulWidget {
@override
_ConvoPageState createState() => _ConvoPageState();
}
class _ConvoPageState extends State<ConvoPage>
@override
Widget build(BuildContext context) {
void generateConvoTopic() {
final _random = Random();
var convoTopic = ConvoTopics[_random.nextInt(ConvoTopics.length)];
print(convoTopic);
}
return Scaffold(
backgroundColor: Color(0xff04072E),
appBar: AppBar(
title: Text('Convo', style: TextStyle(color: Colors.white)),
backgroundColor: Color(0xff04072E),
),
body: SafeArea(
child: SingleChildScrollView(
child: Column(children: <Widget>[
Container(
child: Align(
alignment: Alignment.center,
child: Text(convoTopic,
),
// where randomized string appears
),
),
ElevatedButton(
onPressed: () async {
generateConvoTopic();
},
// button's function is to randomize convo topic
child: Container(
child: Text('Get Convo'),
),
),
:
:
:
Answer
Your approach is mostly correct.
Three small things you were missing were
You should define your
generateConvoTopic
in yourState
class rather than thebuild
method.Your
convoTopic
should be aState
class variable. You are currently defining it inside afunction
inside thebuild
method. So it will only be accessible inside yourfunction
but you need to update it and read it, so it will go inState
class.You should call
setState
whenever you want to update any variable of yourState
class. In your case, theconvoTopic
after you follow step 2.List item
;
final List<String> ConvoTopics = ['blah blah', 'black sheep', 'balh bath']
class ConvoPage extends StatefulWidget {
@override
_ConvoPageState createState() => _ConvoPageState();
}
class _ConvoPageState extends State<ConvoPage> {
String convoTopic;
void generateConvoTopic() {
setState(() {
convoTopic = ConvoTopics[Random().nextInt(ConvoTopics.length)];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xff04072E),
appBar: AppBar(
title: Text('Convo', style: TextStyle(color: Colors.white)),
backgroundColor: Color(0xff04072E),
),
body: SafeArea(
child: SingleChildScrollView(
child: Column(children: <Widget>[
Container(
child: Align(
alignment: Alignment.center,
child: Text(convoTopic),
),
),
ElevatedButton(
onPressed: () async {
generateConvoTopic();
},
child: Container(
child: Text('Get Convo'),
),
),
]))));
}
}
Related Questions
- → I can't convert Json to string [OctoberCms]
- → Passing a JS var from AJAX response to Twig
- → how to convert stdclass into string
- → Dynamic url segment name in laravel 5.1
- → Knockout JS - How to return empty strings for observable fields
- → How to replace *( in a string
- → Removing a JavaScript property inside a specific Object within an Array
- → Is this considered to be a custom facade approach in Laravel?
- → JavaScript: How would I reverse ONLY the words in a string
- → How to access a complex object comming from a datatable cell in JQuery/Javascript
- → Regex extract string after the second "." dot character at the end of a string
- → Htaccess negation
- → Convert generic text string in json object into valid url using Angular