Детектор жестов не может сработать внутри StackView - PullRequest
0 голосов
/ 05 марта 2020

Проблема аналогична в Flutter Github: https://github.com/flutter/flutter/issues/23454. У меня беспорядочная проблема с детектором жестов. метод onTap() не дает мне распечатать. Это выглядит как stati c, однако я поместил его в виджет с сохранением состояния и написал его в Stateful Widget, фрагмент кода которого приведен ниже:

class HeaderData extends StatefulWidget {

  @override
  _HeaderDataState createState() => _HeaderDataState();
}

class _HeaderDataState extends State<HeaderData> {
  List<String> lsDummy = ['image1',
    'image2',
    'image3'
  ];

  @override
  Widget build(BuildContext context) {

    return Stack(
      children: <Widget>[
        Container(
          height: 250,
          child: Swiper(
            autoplayDelay: 3000,
              itemCount: lsDummy.length,
            autoplay: true,
            pagination: SwiperPagination(),
            itemBuilder: (context, index){
                return PNetworkImage(image: lsDummy[index], fit: BoxFit.cover, height: 200,);
            },
          ),
        ),
        Container(
          padding: const EdgeInsets.only(left: 16.0, right: 16),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const SizedBox(height: 70),
                  Image(
                    image: AssetImage('images/travel_logo.png'),
                    alignment: Alignment.center,
                    width: 200,
                    color: Colors.blue,
                  ),
                  const SizedBox(height: 20.0),
                ],
              ),
              GestureDetector(
              onTap: (){
                print('test');
              }, 
                  child: Container(
                    padding: EdgeInsets.all(4),
                      child: Icon(Icons.settings, color: Colors.white,)
                  )
              ),

            ],
          ),
        ),

        Padding(
          padding: const EdgeInsets.only(top: 150),
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 20),
            height: 50,
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.blue.withOpacity(0.8),
              borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('PILIH DESTINASI', style: TextStyle(color: Colors.white),),
                SizedBox(width: 10,),
                Icon(Icons.keyboard_arrow_down, color: Colors.white,)
              ],
            ),
          ),
        ),
        SizedBox(height: 10.0),
      ],
    );
  }
}

Если мы запустим приложения, этот виджет будет отображаться так рисунок. Может ли кто-нибудь помочь мне, почему это произошло? если вы думаете, что этот вопрос неясен, дайте мне знать. Так что я могу обновить вопрос

picture

ОБНОВЛЕНИЕ Я пробовал код с помощью iconButton, но все еще не может вызвать print

return Stack(
      children: <Widget>[
        Container(
          height: 250,
          child: Swiper(
            autoplayDelay: 3000,
              itemCount: lsDummy.length,
            autoplay: true,
            pagination: SwiperPagination(),
            itemBuilder: (context, index){
                return PNetworkImage(image: lsDummy[index], fit: BoxFit.cover, height: 200,);
            },
          ),
        ),
        Container(
          padding: const EdgeInsets.only(left: 16.0, right: 16),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const SizedBox(height: 70),
                  Image(
                    image: AssetImage('images/travel_logo.png'),
                    alignment: Alignment.center,
                    width: 200,
                    color: Colors.blue,
                  ),
                  const SizedBox(height: 20.0),
                ],
              ),
              IconButton(
                  icon: Icon(Icons.settings, color: Colors.white,),
                  onPressed: ()=> print('test')
              )
            ],
          ),
        ),

        Padding(
          padding: const EdgeInsets.only(top: 150),
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 20),
            height: 50,
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.blue.withOpacity(0.8),
              borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('PILIH DESTINASI', style: TextStyle(color: Colors.white),),
                SizedBox(width: 10,),
                Icon(Icons.keyboard_arrow_down, color: Colors.white,)
              ],
            ),
          ),
        ),
        SizedBox(height: 10.0),
      ],
    );

ОБНОВЛЕНИЕ 2 Я провел новый эксперимент для этого. Я пытаюсь сделать фиктивный контейнер, который выше его другого виджета. Затем я установил детектор жестов. но все же я не достаю распечатку. Вот мой код и картинка виджетов. Кстати, в github флаттера есть похожий вопрос: https://github.com/flutter/flutter/issues/23454 Проблема с билетом еще открыта.

picture2

@override
  Widget build(BuildContext context) {

    return Stack(
      children: <Widget>[
        Container(
          height: 250,
          child: Swiper(
            autoplayDelay: 3000,
              itemCount: lsDummy.length,
            autoplay: true,
            pagination: SwiperPagination(),
            itemBuilder: (context, index){
                return PNetworkImage(image: lsDummy[index], fit: BoxFit.cover, height: 200,);
            },
          ),
        ),

        Padding(
          padding: const EdgeInsets.only(top: 150),
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 50),
            height: 50,
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.blue.withOpacity(0.8),
              borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('PILIH DESTINASI', style: TextStyle(color: Colors.white),),
                SizedBox(width: 10,),
                Icon(Icons.keyboard_arrow_down, color: Colors.white,)
              ],
            ),
          ),
        ),
        Container(
          padding: EdgeInsets.only(top: 50),
          child: Image(image: AssetImage('images/travel_logo.png'),
            alignment: Alignment.center,
            width: 200,
            color: Colors.blue,),
        ),
        GestureDetector(
          onTap: (){
            print('teste');
          },
          child: Container(
            width: 100,
            height: 100,
            color: Colors.grey,
          ),
        )
      ],
    );

  }

Ответы [ 2 ]

0 голосов
/ 05 марта 2020

Хорошо, я только что нашел твою проблему. Я просто использовал ваш код и обнаружил, что под значком «Контейнер» отображается строка значков. Решением может быть увеличение нижнего отступа значка следующим образом:

             GestureDetector(
                  onTap: (){
                    print('test');
                  },
                  child: Container(
                         padding: EdgeInsets.only(bottom: 50), //this is the changes
                         child: Icon(Icons.settings, color: 
                         Colors.white,))

Другим решением является увеличение поля симметрии c контейнера, содержащего дочерний текст «PILIH DESTINASI», как показано ниже:

      Padding(
           padding: const EdgeInsets.only(top: 150),
           child: Container(
            margin: EdgeInsets.symmetric(horizontal: 50), //this is the changes
            height: 50,
            width: double.infinity,
            decoration: BoxDecoration(
                color: Colors.blue.withOpacity(0.8),
                borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('PILIH DESTINASI', style: TextStyle(color: 
                Colors.white),),
                SizedBox(width: 10,),
                Icon(Icons.keyboard_arrow_down, color: Colors.white,)
              ],
            ),
          ),
        ),
0 голосов
/ 05 марта 2020

Попробуйте добавить контейнер внизу списка, чтобы убедиться, что он не покрыт другими виджетами.


class HeaderData extends StatefulWidget {

  @override
  _HeaderDataState createState() => _HeaderDataState();
}

class _HeaderDataState extends State<HeaderData> {
  List<String> lsDummy = ['image1',
    'image2',
    'image3'
  ];

  @override
  Widget build(BuildContext context) {

    return Stack(
      children: <Widget>[
        Container(
          height: 250,
          child: Swiper(
            autoplayDelay: 3000,
              itemCount: lsDummy.length,
            autoplay: true,
            pagination: SwiperPagination(),
            itemBuilder: (context, index){
                return PNetworkImage(image: lsDummy[index], fit: BoxFit.cover, height: 200,);
            },
          ),
        ),

        Padding(
          padding: const EdgeInsets.only(top: 150),
          child: Container(
            margin: EdgeInsets.symmetric(horizontal: 20),
            height: 50,
            width: double.infinity,
            decoration: BoxDecoration(
              color: Colors.blue.withOpacity(0.8),
              borderRadius: BorderRadius.all(Radius.circular(10))
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('PILIH DESTINASI', style: TextStyle(color: Colors.white),),
                SizedBox(width: 10,),
                Icon(Icons.keyboard_arrow_down, color: Colors.white,)
              ],
            ),
          ),
        ),


        SizedBox(height: 10.0),

 Container(
          padding: const EdgeInsets.only(left: 16.0, right: 16),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const SizedBox(height: 70),
                  Image(
                    image: AssetImage('images/travel_logo.png'),
                    alignment: Alignment.center,
                    width: 200,
                    color: Colors.blue,
                  ),
                  const SizedBox(height: 20.0),
                ],
              ),
              GestureDetector(
              onTap: (){
                print('test');
              }, 
                  child: Container(
                    padding: EdgeInsets.all(4),
                      child: Icon(Icons.settings, color: Colors.white,)
                  )
              ),

            ],
          ),
        ),

      ],
    );
  }
}


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...