How to run counter without click event?

Asked

Viewed 73 times

0

How to run the click counter without having to click the button? Type starts app already starts 1, 2, 3...

inserir a descrição da imagem aqui

I tried it as follows, but I can’t start the project without the same event.

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
@override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
       theme: ThemeData(

       primarySwatch: Colors.lightGreen,
    ),
  home: MyHomePage(title: 'Frases do dia'),
  );
}
}

 class MyHomePage extends StatefulWidget {
 MyHomePage({Key key, this.title}) : super(key: key);


  final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
  }

  class _MyHomePageState extends State<MyHomePage> {
     void initState() {

 setState(() {

  _numeroAleatorio = new Random().nextInt(4);
});

 }

  Future<int> _getFutureBool() {
    Future.delayed(const Duration(milliseconds: 500), () {
     setState(() {
    _numeroAleatorio = new Random().nextInt(4);
  });
});
}



   int _numeroAleatorio = 0;
    List _frases = [

    "Frase 1",
    "Frase 2",
    "Frase 3",
    "Frase 4",

    ];



   @override
    Widget build(BuildContext context) {

    return Scaffold(
     appBar: AppBar(

    title: Text(widget.title),
    ),
  body: Center(
    // Center is a layout widget. It takes a single child and positions it
    // in the middle of the parent.
    child: Column(

      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text(
          'Prescione o botão abaixo para gerar uma frase.',
        ),
        Text(
          _frases[_numeroAleatorio],
          style: Theme.of(context).textTheme.display1,
        ),
      ],
    ),
  ),
//
);
}
}

Code link: https://paste.ofcode.org/QkVzxGSAvTLtV6dqG5y2CA

  • As I could not execute this idea, I tried to do through init or Future more I did not get any results.

1 answer

2


Browser other questions tagged

You are not signed in. Login or sign up in order to post.