tcard icon indicating copy to clipboard operation
tcard copied to clipboard

tcard is not resetting for the first time, getting null in tcardController.state

Open subhangiP opened this issue 3 years ago • 0 comments

i want reset the card in every 5 seconds automatically the length of the list is changing but the UI is not resetting. Refer to the below code - List<Color> colors = [ Colors.blue, Colors.yellow, Colors.red, Colors.orange, Colors.pink, Colors.amber, Colors.cyan, Colors.purple, Colors.brown, Colors.teal, ];

  class TCardPage extends StatefulWidget {
    const TCardPage({Key? key}) : super(key: key);
  
    @override
    _TCardPageState createState() => _TCardPageState();
  }
  
  class _TCardPageState extends State<TCardPage> {
    final TCardController _controller = TCardController();
    int _index = 0;
    Timer? timer;
    bool changeColor = false;
  
    @override
    void initState() {
      super.initState();
      timer = Timer.periodic(
          const Duration(seconds: 5), (Timer t) => sequenceApiCall());
      print(_controller.state.toString());
      // _controller.reset;
    }
  
    void sequenceApiCall() {
      changeColor = !changeColor;
      Future.delayed(const Duration(seconds: 10), () {
        print('sequenceApiCall');
        if (changeColor) {
          print('changeColor');
          colors.removeAt(0);
          print('color count = ${colors.length}'); //9
        } else {
          colors.insert(0, Colors.black);
          print('black');
          print('color count = ${colors.length}'); //10
        }
        print(_controller.state.toString());
        _controller.reset();
        setState(() {});
      });
    }
  
    @override
    void dispose() {
      super.dispose();
      timer?.cancel();
      _controller.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            children: <Widget>[
              const SizedBox(height: 200),
              TCard(
                cards: cardsColor,
                controller: _controller,
                onForward: (index, info) {
                  _index = index;
                  print(info.direction);
                  setState(() {});
                },
                onBack: (index, info) {
                  _index = index;
                  setState(() {});
                },
                onEnd: () {
                  print('end');
                },
              ),
              SizedBox(height: 20),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  OutlineButton(
                    onPressed: () {
                      _controller.back();
                    },
                    child: const Text('Back'),
                  ),
                  OutlineButton(
                    onPressed: () {
                      _controller.forward();
                    },
                    child: const Text('Forward'),
                  ),
                  OutlineButton(
                    onPressed: () {
                      _controller.reset();
                    },
                    child: const Text('Reset'),
                  ),
                ],
              ),
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          child: Text(_index.toString()),
        ),
      );
    }
  
    List<Widget> cardsColor = List.generate(
      colors.length,
      (int index) {
        return Container(
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: colors[index],
          ),
          child: Text(
            '${index + 1}',
            style: const TextStyle(fontSize: 100.0, color: Colors.white),
          ),
        );
      },
    );
  }

subhangiP avatar Jan 03 '22 09:01 subhangiP