skip to Main Content
body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  'Class: ${className}',
                  style: TextStyle(
                    color: Colors.grey[700],
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 10),
                Text(
                  'Date: ${date}',
                  style: TextStyle(
                    color: Colors.grey[700],
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 10),
                Text(
                  'Subject: ${subjectName}',
                  style: TextStyle(
                    color: Colors.grey[700],
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 50),
                Text(
                  '${homeWorkTitle}',
                  textAlign: TextAlign.justify,
                  style: TextStyle(
                    color: Colors.grey[700],
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 15),
                GestureDetector(
                  child: SizedBox(
                      height: 50,
                      width: 150,
                      child: Link(
                        uri: Uri.parse("http://bolpur.tigps.theretreathotels.in/assets/hw_teacher_upload/${homeWorkFileName}"),
                        builder: (context,followlink) {
                          return ElevatedButton(
                              onPressed: followlink,
                              child: const Text('Download PDF')
                          );
                        },
                      )),
                ),

# HERE I want add if condition /////////****

                const SizedBox(height: 50),
                Text(
                  'Upload your task',
                  style: TextStyle(
                    color: Colors.grey[700],
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 10),
                GestureDetector(
                  child: SizedBox(
                    height: 50,
                    width: 150,
                    child: ElevatedButton(
                      onPressed: openFiles,
                      child: const Text("Upload PDF"),
                    ),
                  ),
                ),
                const SizedBox(height: 10),
                if (_filePath != null)
                  Text('File Path: $_filePath')
                else
                  const Text('No file selected'),
                const SizedBox(height: 150),
                GestureDetector(
                  child: Center(
                    child: Container(
                      height: 60,
                      width: 600,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(100)),
                      child: ElevatedButton(
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Colors.teal, // background color
                          foregroundColor: Colors.white, // text color
                        ),
                        onPressed: () async {
                          setState(() => isLoading = true);
                          await uploadImage();
                          setState(() => isLoading = false);
                        },
                        child: Center(
                          child: isLoading ? const Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text('Loading...', style: TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold),),
                              SizedBox(width: 10,),
                              CircularProgressIndicator(color: Colors.white,),
                            ],
                          ) : const Text( "Submit", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 20,),
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          )),
# HERE I want add if condition /////////****

in the above code I add

if (condition) {} 

but I got errors.

2

Answers


  1. I think you should re-format your question to be better looking and more informative, but in general you’re probably looking for the Conditional (ternary) operator

    for example:

    ...
    child: isConditionSatisfied ?
        const SuccessView() : const WaitingView()
    
    Login or Signup to reply.
  2. I’m not sure if I understood your problem fully but I’ll try to give a more general answer. if-conditions inside the list of children in a Column are a special kind of so called collection if. Read more about it here. In any case, these never use curly braces for the body of the if so if (condition){} is not possible. However, if you want to group some widgets under a single condition you can make use of the spread operation. It can be used like this:

    Column(
      children: [
        Container(),
        Container(),
        if (condition) ... [
          Container(),
          Container(),
          Container(),
        ],
        Container()
      ]
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search