change container height inside scroll view

Viewed 187

scroll view not working after set state

I am trying to change an item inside scroll view that changes every one second the scroll view after set state is not working properly making the app crash how can I make the scrollview item changes every second with out making the scrollview crash

  void _startTimer() {
    if (_timerStarted == false) {
      _timerStarted = true;
      _everySecond = Timer.periodic(const Duration(seconds: 1), (Timer t) {
        setState(() {
          _changeHeight = !_changeHeight;
        });
      });
    }
  }

  void _stopTimer() {
    if (_timerStarted) {
      _timerStarted = false;
      _everySecond.cancel();
      setState(() {});
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  void _initScrollController() {
    _scrollController = ScrollController()
      ..addListener(() {
        widget.scrollUpdateTrigger!(_scrollController);
      });
  }

  late List<Widget> cashboardItems = [
    ReorderableGridItem(
      widthFlex: 1,
      key: GlobalKey(),
      child: Container(
        constraints: BoxConstraints(maxWidth: 200),
        margin: const EdgeInsets.all(20),
        decoration: const BoxDecoration(color: Colors.red),
        child: const Padding(
          padding: EdgeInsets.all(10.0),
          child: Text(
            "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed ",
             overflow: TextOverflow.visible,
            style: TextStyle(color: Colors.black),
          ),
        ),
      ),
      allowDrag: true,
    ),
    ReorderableGridItem(
      widthFlex: 1,
      key: GlobalKey(),
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 500),
        curve: Curves.easeInOut,
        height: 75,
        margin: const EdgeInsets.all(20),
        decoration: const BoxDecoration(color: Colors.blue),
      ),
      allowDrag: true,
    ),
    ReorderableGridItem(
      widthFlex: 1,
      key: GlobalKey(),
      child: Container(
        height: 75,
        margin: const EdgeInsets.all(20),
        decoration: const BoxDecoration(color: Colors.yellow),
      ),
      allowDrag: true,
    ),
    ReorderableGridItem(
      widthFlex: 1,
      key: GlobalKey(),
      child: AnimatedContainer(
        height: _changeHeight ? 100 : 200,
        duration: const Duration(milliseconds: 700),
        curve: Curves.easeInOut,
        margin: const EdgeInsets.all(20),
        decoration: const BoxDecoration(color: Color(0xff5855DC)),
        child: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              const Text(
                "Good luck",
                style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w500,
                    fontSize: 36),
              ),
              const SizedBox(width: 10),
              const Icon(
                Icons.emoji_emotions_outlined,
                color: Colors.white,
                size: 42,
              ),
            ],
          ),
        ),
      ),
    ),
    ReorderableGridItem(
      widthFlex: 1,
      key: GlobalKey(),
      child: Padding(
        padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
        child: const Center(
          child: Text(
            "Info: With a longPress on the boxes you can reorder them to "
            "different positions.",
            style: TextStyle(
                fontSize: 14, fontWeight: FontWeight.bold, height: 18 / 14),
          ),
        ),  ),
      allowDrag: false,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ReorderableGridView(
        scrollController: _scrollController,
        orderedIndexes: savedOrder,
        onOrderChange: (newOrderedList) {
          savedOrder = newOrderedList.map((e) => e!.orderNumber).toList();
        },
        children: [
          cashboardItems[0] as ReorderableGridItem,
          cashboardItems[1] as ReorderableGridItem,
          cashboardItems[2] as ReorderableGridItem,
          cashboardItems[3] as ReorderableGridItem,
          cashboardItems[4] as ReorderableGridItem,
        ],
      ),
    );
  }
}
```

I have tried to disable the set state start timer function and the app worked fine without changing the height of the item in the scrollview and scroll working perfectly

0 Answers
Related