Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListTile OnTap is working when I use ListView. But when i use ListWheelScrollView it doesnt work

ListTile OnTap is working when I use ListView. But when i use ListWheelScrollView it doesn't work. I mean it doesn't get tapped. The view changes. But i can't seem to tap it. I looked for the solution in a lot of places and links but still couldn't find the solutions.

These are the code I did.

@override
  Widget build(BuildContext context) {
    return new ListWheelScrollView(
      physics:FixedExtentScrollPhysics(),
      children: getPostList(),
      itemExtent: 100.0,
    );
  }

  List<PostListItem> getPostList() {
    return _postModal
        .map((contact) => new PostListItem(contact))
        .toList();
  }

And this is where I have built the ListTile

@override
  Widget build(BuildContext context) {
    return new ListTile(
      onTap:  () {
          var route = new MaterialPageRoute(
            builder: (BuildContext context) =>
                new OnTapPost(value: _postModal.fullName),
          );
          Navigator.of(context).push(route);
        },
        leading: new Image.asset('assets/images/logo.png'),
        title: new Text(_postModal.fullName),
        subtitle: new Text(_postModal.email)
    );

  }

In the above code the list items doesn't get tapped. But if i replace the ListWheelScrollView with ListView as shown below, it works perfectly.

@override
  Widget build(BuildContext context) {
    return new ListView(
      children: getPostList(),
    );
  }

  List<PostListItem> getPostList() {
    return _postModal
        .map((contact) => new PostListItem(contact))
        .toList();
  }
like image 731
Althaf1467 Avatar asked May 22 '19 11:05

Althaf1467


1 Answers

Update: Another look no update yet from the flutter team but another hack solution. By wrapping the whole listscrollwheel widget with a Gesturedetector. Every scroll sets the state using the "onSelectedItemChanged" for listscrollview. But the gesture detector widget does something with it only on tap.

  import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
   
    
    class ServicesList extends StatefulWidget {
    
      final type;
    
      ServicesList({this.type});
    
      @override
      _ServicesListState createState() => _ServicesListState();
    }
    
    class _ServicesListState extends State<ServicesList> {
    
      double _height ,_width;
      int serviceIndex;
      final FixedExtentScrollController _controller = FixedExtentScrollController();

 

List<Service> serviceList = [
    Service(name: 'service 1'),
    Service(name: 'service 4'),
    Service(name: 'service 3'),

];
    
      @override
      void initState() {
    
        serviceIndex = 0;
        // TODO: implement initState
        super.initState();
      }
    
      detectService(index) {
        print('clicked container : $index'); //do whatever you want with the clicked index
      }
    
    
      @override
      Widget build(BuildContext context) {
    
        _height = MediaQuery.of(context).size.height;
        _width = MediaQuery.of(context).size.width;
               
        return scrollWheel();
      }
    
    
    
      Widget scrollWheel() {
        return GestureDetector(
          onTap: (){
            detectService(serviceIndex);
            },
          child: ListWheelScrollView(
            controller: _controller,
            diameterRatio: 1.5,
            itemExtent: 80,
            onSelectedItemChanged: (e)=>{
    
              setState(() {
                serviceIndex = e;
              })
            },
            magnification: 0.2,
            physics: FixedExtentScrollPhysics(),
            children: serviceList.map((e) {
    
    
                return Container(
                  padding:  const EdgeInsets.all(6.0),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10.0),
                    color: Colors.blue,
                  ),
                  child:Text('item $e.name');
                );
          }).toList(), //List of widgets
          ),
        );
      }
    }
  
   class Service {
      String name;
 
     Service( {this.name} );
  }
like image 128
Marshall Fungai Avatar answered Oct 17 '22 15:10

Marshall Fungai