sgrysoft
sgrysoft

Reputation: 628

Flutter automaticaly open atached url on the 'ontap' event in ListTile

I am having a problem because when I attach the URL to each element on my list, on the first element it takes the URL and opens it automatically, so, whats work with it?

This is My container

        Container(
          height: 44.0,
          child: ListView(
              scrollDirection: Axis.horizontal,
              children: _pageList(webpage, linkedin, twitter, github)),
        ),

This is the load of the list elements

List<Widget> _pageList(webpage, linkedin, twitter, github) {
  var listSkills = new List<ClipRRect>();
  if (webpage != null) listSkills.add(_createUrlElement(webpage, 'webpage'));
  if (linkedin != null) listSkills.add(_createUrlElement(linkedin, 'linkedin'));
  if (twitter != null) listSkills.add(_createUrlElement(twitter, 'twitter'));
  if (github != null) listSkills.add(_createUrlElement(github, 'github'));

  return listSkills;
}

This is the element creation. ... yessss, I know than there's better ways to do this, but, I just test all possibilities than I saw on net and it was the last code than I have, so, forgive me and don't kill me for this part of the code, jeje

ClipRRect _createUrlElement(webpage, icon) {
  return ClipRRect(
    borderRadius: BorderRadius.circular(50.0),
    child: Container(
        height: 60,
        width: 60,
        color: Colors.black26, //.gray.fromRGBO(52, 58, 64, 0),
        child: ListTile(
          leading: Image.asset(
            'assets/$icon.png',
            width: 35,
            height: 35,
          ),
          subtitle: Text(''),
          title: Text(''),
          onTap: launcherUrl(webpage), // TODO: this open the url automatic
        )),
  );
}

I guess than this is enough to understand the problem, but, if you prefer you can check the repository

https://github.com/sgermosen/DominicanWhoCodes/blob/master/DominicanWhoCodes.Flutter/lib/providers/cardType1.dart

Upvotes: 1

Views: 851

Answers (1)

sameer kashyap
sameer kashyap

Reputation: 1166

Don't reference a function but call it in the onTap handler.

Do this,

ClipRRect _createUrlElement(webpage, icon) {
  return ClipRRect(
    borderRadius: BorderRadius.circular(50.0),
    child: Container(
        height: 60,
        width: 60,
        color: Colors.black26, //.gray.fromRGBO(52, 58, 64, 0),
        child: ListTile(
          leading: Image.asset(
            'assets/$icon.png',
            width: 35,
            height: 35,
          ),
          subtitle: Text(''),
          title: Text(''),
          onTap: (){
             launcherUrl(webpage)
           }, 
        )),
  );
}

This should fix it.

Upvotes: 2

Related Questions