I’m making an attempt to arrange an RSS feed for my software, so it might be attention-grabbing to place a number of totally different RSS hyperlinks into it. Nonetheless, I’ve already tried a number of methods to execute this job, like placing varied hyperlinks straight in a Listing, and every time I failed and could not discover a answer wherever.

So the massive query is: is it attainable to do that?

import 'package deal:webfeed/webfeed.dart';
import 'package deal:http/http.dart' as http;
import 'package deal:url_launcher/url_launcher.dart';
import 'package deal:cached_network_image/cached_network_image.dart';

class Feed extends StatefulWidget {
  const Feed({Key? key}) : tremendous(key: key);
  closing String title="Últimas atualizações";

  @override
  State<Feed> createState() => _FeedState();
}

class _FeedState extends State<Feed> {
  static Uri feedUrl = Uri(
    /* 'https://nasa.gov/rss/dyn/lg_image_of_the_day.rss' */
    scheme: 'https',
    host: 'rss.tecmundo.com.br',
    path: 'feed'
  );
  RssFeed _feed = RssFeed(hyperlink: 'https://rss.tecmundo.com.br/feed');
  static const String placeholderImg = 'property/pictures/noImage.jpg';
  
  late GlobalKey<RefreshIndicatorState> _refreshKey;

  @override
  void initState() {
    tremendous.initState();
    _refreshKey = GlobalKey<RefreshIndicatorState>();
    load();
  }

  // GETTING THE FEED
  Future<RssFeed?> loadFeed() async {
    attempt {
      closing shopper = http.Consumer();
      closing response = await shopper.get(feedUrl);
      return RssFeed.parse(response.physique);
    } catch (e) {
      //
    }
    return null;
  }

  load() async {
    loadFeed().then((consequence) {
      if (null == consequence || consequence.toString().isEmpty) {
        return showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: const Textual content('Erro ao carregar.'),
              content material: const Textual content('Tente novamente mais tarde.'),
              actions: [
                TextButton(
                  child: const Text('OK'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              ],
            );
          }
        );
      }
      updateFeed(consequence);
    });
  }

  updateFeed(feed) {
    setState(() {
      _feed = feed;
    });
  }

  Future<void> openFeed (String url) async {
    closing Uri uri = Uri.parse(url);
    if(!await launchUrl(uri, mode: LaunchMode.inAppWebView)) {
      print('Error');
    }
  }

  Textual content title (title) {
    return Textual content(
      title,
      fashion: const TextStyle(
        fontSize: 18.0,
        fontWeight: FontWeight.w500,
      ),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  Textual content subtitle (String? subtitle) {
    return Textual content(
      subtitle ?? '',
      fashion: const TextStyle(
        fontSize: 14.0,
        fontWeight: FontWeight.w100,
      ),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
  }

  Padding thumbnail (imageUrl) {
    return Padding(
      padding: const EdgeInsets.solely(left: 15.0),
      little one: imageUrl != null ? CachedNetworkImage(
        placeholder: (context, url) => Picture.asset(placeholderImg),
        imageUrl: imageUrl,
        peak: 50,
        width: 50,
        alignment: Alignment.middle,
        match: BoxFit.cowl,
      ) :
      SizedBox(
        peak: 50,
        width: 50,
        little one: Picture.asset(placeholderImg),
      ),
    );
  }

  ListView checklist () {
    return ListView.builder(
      itemCount: _feed.gadgets!.size,
      itemBuilder: (BuildContext context, int index) {
        closing merchandise = _feed.gadgets![index];
        return ListTile(
          title: title(merchandise.title),
          subtitle: subtitle(merchandise.dc?.creator),
          main: thumbnail(merchandise.enclosure!.url),
          trailing: Icon(Icons.bookmark_add_outlined),
          contentPadding: const EdgeInsets.all(5.0),
          onTap: () => openFeed(merchandise.hyperlink ?? '')
        );
      },
    );
  }

  bool isFeedEmpty ()  null == _feed.gadgets;
  

  Widget physique () => isFeedEmpty() ?
      const Heart(
        little one: CircularProgressIndicator(),
      ) :
      RefreshIndicator(
        little one: checklist(),
        onRefresh: () => load(),
        key: _refreshKey,
      );
  
  @override
  Widget construct(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Textual content('Newest updates'),
      ),
      physique: physique(),
    );
  }
}```

By admin

Leave a Reply

Your email address will not be published.