vendredi 21 mai 2021

How To Random Select A String From List After Tabbing Button [Flutter]

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'),
              ),
            ),
:
:
:




Aucun commentaire:

Enregistrer un commentaire