简体   繁体   中英

onPressed functions not triggered inside Stack Layout Flutter

I have a carousel, two iconButtons in a Positioned Layout, gridView and 2 other positioned layouts All Inside A Stack Layout.

The onPressed() functions on the iconButtons specifically or on any of the elements general do not get triggered. I figured out that the issue is because of the Parent Stack Layout because when place them outside of it the onPressed works.

I need the Stack Layout for the design so im wondering if there is an alternative for it.

Here's an example of the code:

child: Stack(
                    children: <Widget>[
                      Container(
                        child: Column(
                          children: <Widget>[
                            carouselSlider = CarouselSlider(
                              height: 270.0,
                              initialPage: 0,
                              viewportFraction: 1.0,
                              aspectRatio: 1.0,
                              enlargeCenterPage: false,
                              autoPlay: true,
                              reverse: false,
                              enableInfiniteScroll: true,
                              autoPlayInterval: Duration(seconds: 2),
                              autoPlayAnimationDuration:
                                  Duration(milliseconds: 2000),
                              pauseAutoPlayOnTouch: Duration(seconds: 10),
                              scrollDirection: Axis.horizontal,
                              onPageChanged: (index) {
                                setState(() {
                                  current = index;
                                });
                              },
                              items: carouselImages.map((i) {
                                return Builder(
                                  builder: (BuildContext context) {
                                    return Container(
                                        child: GestureDetector(
                                            // behavior:
                                            //     HitTestBehavior.translucent,
                                            child: Image.asset(
                                              i,
                                              fit: BoxFit.cover,
                                            ),
                                            onTap: () {
                                              // Navigator.push<Widget>(
                                              //   context,
                                              //   MaterialPageRoute(
                                              //     builder: (context) => ImageScreen(i),
                                              //   ),
                                              // );
                                            }));
                                  },
                                );
                              }).toList(),
                            ),
                            SizedBox(
                              height: 20,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children:
                                  map<Widget>(carouselImages, (index, url) {
                                return Container(
                                  width: 10.0,
                                  height: 10.0,
                                  margin: EdgeInsets.symmetric(
                                      vertical: 10.0, horizontal: 2.0),
                                  decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    color: current == index
                                        ? Color.fromRGBO(221, 40, 42, 0.7)
                                        : Colors.white,
                                  ),
                                );
                              }),
                            ),
                          ],
                        ),
                      ),
                      Positioned(
                        right: 5,
                        top: 10,
                        child: IconButton(
                          icon: Icon(Icons.menu),
                          color: Color.fromRGBO(221, 40, 42, 0.7),
                          iconSize: 20.0,
                          onPressed: () {
                            print('Clicked');
                            scaffolddKey.currentState.openDrawer();
                          },
                        ),
                      ),

The solution is to wrap all of your widgets that are not supposed to receive tap events in IgnorePointer widget.

If your CarouselSlider is not supposed to receive tap events then wrap it and all other widgets that are not supposed to receive tap events in IgnorePointer widget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM