Home > Net >  how to loop infinitely inside a Text widget Flutter
how to loop infinitely inside a Text widget Flutter

Time:02-16

Well it is pretty simple, just increment the counter infinitely and show it with the widget Text() simple, right?..

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: RainbowButton()));
}

class _RainbowButtonState extends State<RainbowButton> {
  int counter = 0;
  void _increase() {
    super.initState();
    while (true) {
      setState(() {
        counter  ;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
      child:
          Column(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
        Text(counter.toString()),
      ]),
    )));
  }
}

class RainbowButton extends StatefulWidget {
  const RainbowButton({Key? key}) : super(key: key);

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

it only displays a 0 but it doesn't go up.

this is all i got so far, my respects and thanks for your time. i feel a bit overwhelmed.

CodePudding user response:

When stateful pages is shown, you can set action/s on the initState() function. And as for your specific application you can follow jamesdlin advice by using Timer.periodic.

Example Code:

void initState() {
    Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {
        counter  ;
      });
    });
    super.initState();
  }
  • Related