OnurParlak
OnurParlak

Reputation: 97

Flutter Visibility with Condition (How can I hide only one widget with condition)

I have a problem with using Visibility widget.

What I want to do is hiding certain listview's by depending user's click on my Row. But when User click on row, all Listview is showing. When clicked again, all listview widget's are going away.

What I want to do is with images:
This is how my page looks
This is what happens when I click on arrow button or "Sezon 1" Text
This is what I want to do when I click on arrow button or "Sezon 1" Text

What I'm trying to do is When I click Season 2, it will show season 2 episodes. When I click season 3 it will show season 3 episodes etc.

Here is my code (I know It's a little messy for right now, apologize for that) : GestureDetector is working same for every click.

bool viewVisible = false;
void hideWidget() {
setState(() {
  viewVisible = !viewVisible;
  print(viewVisible);
});

StreamBuilder<QuerySnapshot>(
                stream: seasons.snapshots(),
                builder:
                    (BuildContext context, AsyncSnapshot asyncSnapshot) {
                  if (asyncSnapshot.hasError) {
                    return Center(
                        child:
                            Text('Error'));
                  } else {
                    if (asyncSnapshot.hasData) {
                      List<DocumentSnapshot> listOfDocumentSnap =
                          asyncSnapshot.data.docs;

                      return Padding(
                        padding: const EdgeInsets.only(top: 0, left: 0),
                        child: Align(
                          alignment: Alignment.topLeft,
                          child: Column(
                            children: [
                              ListView.builder(
                                shrinkWrap: true,
                                itemCount: listOfDocumentSnap.length,
                                itemBuilder: (context, index) {
                                  var episodes = firestore
                                      .collection('shows')
                                      .doc(data.id)
                                      .collection('Seasons')
                                      .doc(listOfDocumentSnap[index].id)
                                      .collection('Episodes');

                                  return Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    mainAxisAlignment:
                                        MainAxisAlignment.start,
                                    children: [
                                      Padding(
                                        padding: const EdgeInsets.only(
                                            top: 0, left: 18, right: 18),
                                        child: GestureDetector(
                                          onTap: () {
                                            hideWidget();
                                          },
                                          child: Container(
                                            decoration: BoxDecoration(
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        8),
                                                border: Border.all(
                                                    color: Colors.pink)),
                                            child: Row(
                                              children: [
                                                SizedBox(
                                                  width: 20,
                                                ),
                                                Text(
                                                  listOfDocumentSnap[index]
                                                      .get('name')
                                                      .toString()
                                                      .toUpperCase(),
                                                  style: TextStyle(
                                                      fontSize: 20,
                                                      color:
                                                          Colors.grey[800],
                                                      fontWeight:
                                                          FontWeight.w700),
                                                ),
                                                Spacer(flex: 1),
                                                Icon(
                                                  Icons.arrow_drop_down,
                                                  size: 45,
                                                  color: Colors.pink,
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),
                                      StreamBuilder<QuerySnapshot>(
                                        stream: episodes.snapshots(),
                                        builder: (BuildContext context,
                                            AsyncSnapshot asyncSnapshot) {
                                          if (asyncSnapshot.hasError) {
                                            return Center(
                                                child: Text(
                                                    'Error'));
                                          } else {
                                            if (asyncSnapshot.hasData) {
                                              List<DocumentSnapshot>
                                                  listOfDocumentSnap =
                                                  asyncSnapshot.data.docs;

                                              return Padding(
                                                padding:
                                                    const EdgeInsets.only(
                                                        top: 5, left: 18.0),
                                                child: Visibility(
                                                  visible: viewVisible,
                                                  child: Align(
                                                    alignment:
                                                        Alignment.topLeft,
                                                    child: Column(
                                                      children: [
                                                        ListView.builder(
                                                          shrinkWrap: true,
                                                          itemCount:
                                                              listOfDocumentSnap
                                                                  .length,
                                                          itemBuilder:
                                                              (context,
                                                                  index) {
                                                            return ListTile(
                                                              onTap: () {
                                                                setState(
                                                                    () {
                                                                  selectedIndex =
                                                                      index;
                                                                });
                                                              },
                                                              trailing:
                                                                  Icon(Icons
                                                                      .play_arrow),
                                                              title: Text(listOfDocumentSnap[
                                                                      index]
                                                                  .id
                                                                  .toString()),
                                                            );

                                                            /*   return Text(
                                                                listOfDocumentSnap[
                                                                        index]
                                                                    .get(
                                                                        'name'));*/
                                                          },
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                ),
                                              );
                                            } else {
                                              return Center(
                                                  child:
                                                      CircularProgressIndicator());
                                            }
                                          }
                                        },
                                      ),
                                      SizedBox(
                                        height: 30,
                                      )
                                    ],
                                  );
                                },
                              ),
                            ],
                          ),
                        ),
                      );
                    } else {
                      return Center(child: CircularProgressIndicator());
                    }
                  }
                },
              ),

Upvotes: 1

Views: 12465

Answers (2)

Eren
Eren

Reputation: 2663

Sorry, I did not check what is wrong with your code. But, instead of writing your own hide/show logic for each row, try to use Flutter's ExpansionTile widget. It will handle the expanded/collapsed states for you:

https://medium.com/flutterdevs/expansion-tile-in-flutter-d2b7ba4a1f4b

https://medium.com/flutterdevs/expansion-tile-in-flutter-d2b7ba4a1f4b

Upvotes: 4

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63569

There are two different way I know to handle widget visibility. Create a bool to switch visibility. I'm using _show.

Using if condition inside (column or any widget)

if (_show) Container(),

Using Visibility Widget

  Column(
        children: [
          Visibility(
            visible: _show,
            child: Container(),
          ),
      )

Upvotes: 3

Related Questions