flutterdartfreeze

Why does my Flutter app freeze when rooting to a specific page?


So I loaded up my flutter project after a few hours of not working on it, and when I try to click on a button that is supposed to root me to a specific page, the whole app freezes. The last time I ran it, it was working fine, and I haven't changed anything. I have restarted the program, and my pc and it still freezes. All other button that root to pages work so I assume it is a problem with the code of that page. This is the entire code for that page:

import 'package:flutter/material.dart';

import 'package:workwise2/classes/taskClass.dart';
import 'package:workwise2/config.dart';

class TodoTasks extends StatefulWidget {
  @override
  _TodoTasksState createState() => _TodoTasksState();
}

class _TodoTasksState extends State<TodoTasks> {

  Settings settings = Settings();
  BottomNavBarConfig bottomNavBarConfig = BottomNavBarConfig();

  List <Task> tasks = [
    Task(title: "Maths HW", description: "Complete Q2, Q6 and Q7 with GPCs on Q8", subject: "maths", day: 12, month: 4, year: 2021),
    Task(title: "English test", description: "Revise quotes for anger", subject: "english", day: 13, month: 4, year: 2021),
  ];

  @override
  Widget build(BuildContext context) {

    int _currentIndex = 0;
    String _requestedPage;

    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Tasks",
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 30.0
          ),
        ), 

        backgroundColor: Colors.indigoAccent[700],

        leading: Icon(Icons.check_box_rounded, size: 40.0,),
      ),

      body: AnimatedContainer(
        duration: Duration(milliseconds: 1700),
        child: ListView.builder(
          itemBuilder: (context, index) {

            Color _checkButtonColour;
            double _checkButtonElevation;

            IconData _bookmark = Icons.bookmark_border;

            if (tasks[index].state == "todo") {
              _checkButtonColour = Colors.red[600];
              _checkButtonElevation = 0.0;
            } 
            else if (tasks[index].state == "doing") {
              _checkButtonColour = Colors.amber;
              _checkButtonElevation = 5.0;
            } 
            else if (tasks[index].state == "done") {
              _checkButtonColour = Colors.green;
              _checkButtonElevation = 10.0;
            }

            while (tasks[index].favourited == true) {
              _bookmark = Icons.bookmark;
            }
            while (tasks[index].favourited == false) {
              _bookmark = Icons.bookmark_border;
            }

            return Padding(
              padding: EdgeInsets.symmetric(horizontal: 8.0, vertical: 3.0),
              child: Card(
                elevation: 10.0,
                child: ListTile(

                  contentPadding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 5.0),

                  onTap: () {},

                  leading: IconButton(
                    onPressed: () {

                      tasks[index].favourited = !tasks[index].favourited;
                      print(tasks[index].favourited);

                    },
                    icon: Icon(_bookmark),
                    iconSize: 35.0,
                    color: settings.subjectColours[tasks[index].subject]
                  ),

                  title: Text(tasks[index].title, maxLines: 1, overflow: TextOverflow.fade,),

                  subtitle: Text(tasks[index].description, maxLines: 1, overflow: TextOverflow.ellipsis,),

                  trailing: AnimatedContainer(
                    height: 80.0,
                    width: 100.0,
                    duration: Duration(milliseconds: 1700),
                    
                    child: Row(
                      children: <Widget> [

                        SizedBox(
                          width: 40.0,
                          height: 40.0,
                          child: MaterialButton( 
                            elevation: _checkButtonElevation, 
                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6.0)),
                            onPressed: () {
                              if (tasks[index].state == "todo") {
                                setState(() {
                                  tasks[index].state = "doing";
                                });
                              } else if (tasks[index].state == "doing") {
                                setState(() {
                                  tasks[index].state = "done"; 
                                });
                              } else if (tasks[index].state == "done") {
                                setState(() {
                                  tasks[index].state = "todo";
                                });
                              }

                            },
                            padding: EdgeInsets.all(2.0),
                            color: _checkButtonColour,
                          ),
                        ),

                        IconButton(
                          onPressed: () {},
                          icon: Icon(Icons.more_vert),
                        )

                      ],
                    ),
                  ),

                ),
              ),
            );
          },
          itemCount: tasks.length,
        )    
      ),

      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,

        backgroundColor: Colors.indigoAccent[700],
        fixedColor: Colors.white,

        unselectedFontSize: bottomNavBarConfig.unactiveTextSize,
        unselectedIconTheme: IconThemeData(
          size: bottomNavBarConfig.unactiveIconSize
        ),

        selectedFontSize: bottomNavBarConfig.activeTextSize,
        selectedIconTheme: IconThemeData(
            size: bottomNavBarConfig.activeIconSize
        ),
        
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.check_box_rounded),
            label: "Tasks"
          ),

          BottomNavigationBarItem(
            icon: Icon(Icons.lightbulb),
            label: "Learn"
          ),
            
          BottomNavigationBarItem(
            icon: Icon(Icons.home_rounded),
            label: "Home"
          ),

          BottomNavigationBarItem(
            icon: Icon(Icons.library_books_rounded),
            label: "Notes"
          ),
        ],

        onTap: (index) {

          if (index == 0) {
            _requestedPage = "/todotasks";
          }
          else if (index == 2) {
            _requestedPage = "/home";
          }
          else if (index == 3) {
            _requestedPage = "/notes";
          }
          else if (index == 1) {
            _requestedPage = "/sets";
          }

          setState(() {
            _currentIndex = index;
          });
          
          Navigator.popAndPushNamed(context, _requestedPage);

        },
      )
    );
  }
}

I don't know what is wrong. Please help me I have no clue what to do. Comment for more details.


Solution

  • This was caused due to the two while statements in my code. They basically called each other and froze the app.