简体   繁体   English

在 Stack Layout Flutter 中未触发 onPressed 函数

[英]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.我有一个旋转木马、定位布局中的两个图标按钮、gridView 和其他 2 个定位布局都在堆栈布局内。

The onPressed() functions on the iconButtons specifically or on any of the elements general do not get triggered.图标按钮上的 onPressed() 函数或任何一般元素上的函数都不会被触发。 I figured out that the issue is because of the Parent Stack Layout because when place them outside of it the onPressed works.我发现这个问题是因为父堆栈布局,因为当将它们放在它之外时,onPressed 工作。

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.解决方案是将所有不应接收点击事件的小部件包装在 IgnorePointer 小部件中。

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.如果您的 CarouselSlider 不应该接收点击事件,那么将它和所有其他不应该在 IgnorePointer 小部件中接收点击事件的小部件包装起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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