What is Flutter Package ? and why we are using the Package?

A Flutter package is a pre-built module of reusable code that can be added to a Flutter app to perform specific tasks. Packages are created by the Flutter community and can include anything from UI widgets to helper classes and utilities. 

Using packages can greatly speed up app development, as they provide ready-made solutions for common problems, and can help to keep code organized and maintainable. Packages can be added to a Flutter project by including a reference to the package in the pubspec.yaml file.

Packages are published on the Pub Dart Package Repository (Pub.dev), which is a centralized repository for Dart packages, including Flutter packages. Pub.dev makes it easy to find and use packages and provides tools for managing dependencies and publishing packages of your own.

Packages

Some of the most popular Flutter packages include:

  • url_launcher: Launches a URL in the default browser or an app.
  • http: Provides a simple way to make HTTP requests.
  • provider: A simple and efficient state management solution.
  • flutter_slidable: An easy-to-use sliding widget.
  • flutter_spinkit: A collection of loading indicators animated with Flutter.

url_launcher:

url_launcher is a Flutter package that provides a simple API to launch a URL in the mobile platform's default web browser or a URL scheme-supported app. It can be used to open web pages, send emails, make phone calls, send SMS messages and more, depending on the URL scheme. 

The package provides a single function, launch, which takes a URL as an argument and returns a Future<void> that completes when the URL has been launched. If the URL cannot be launched, the returned future completes with an error. 

You can try out this Example:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class MapButton extends StatelessWidget {
  final String destination;

  const MapButton({Key key, this.destination}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () async {
        final url = "https://www.google.com/maps/dir/?api=1&destination=$destination";
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      },
      child: Text("Open in Google Maps"),
    );
  }
}

http:

The http package in Flutter is a package that provides an HTTP client for making network requests in a Flutter application. It allows you to make HTTP requests to APIs and receive data as a response in various formats like JSON, XML, etc. It also supports various HTTP methods like 
  • GET, 
  • POST, 
  • PUT, 
  • DELETE, 
  • and so on. 
This package provides a simple way to make HTTP requests in Flutter and helps to handle responses and errors in a unified manner.

Example of making a GET request using the http package in Flutter:

import 'dart:convert';
import 'package:http/http.dart' as http;

// Make a GET request to an API endpoint
Future<void> fetchData() async {
  final response = await http.get('https://jsonplaceholder.typicode.com/posts');

  if (response.statusCode == 200) {
    // If the server returns a 200 OK response, parse the JSON
    List<dynamic> data = jsonDecode(response.body);
    print(data);
  } else {
    // If the server returns a non-200 response, throw an error
    throw Exception('Failed to load data');
  }
}

For example, the http.get function is used to make a GET request to the specified API endpoint. The response from the server is checked for a 200 OK status code, and if it returns a 200, the JSON data is decoded and printed. If the status code is not 200, an error is thrown.


flutter_slidable:

flutter_slidable is a Flutter package that provides a sliding widget to be used in a list view. It allows you to add slide actions (e.g. delete, archive, etc.) to items in a list. When the user slides an item to the right or left, the sliding widget appears and reveals the slide actions. The user can then perform an action by tapping on one of the slide actions. 

It is used for creating sliding list items with action buttons that are revealed on the swipe. This package provides a customizable and easy-to-use sliding widget for Flutter apps. The sliding widget can be used in a variety of ways, such as for creating swipe-to-delete or swipe-to-archive functionality in a list view.

You can try out this Example:

import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

class MyList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: 20,
      itemBuilder: (context, index) {
        return Slidable(
          actionPane: SlidableDrawerActionPane(),
          actions: <Widget>[
            IconSlideAction(
              caption: 'Archive',
              color: Colors.blue,
              icon: Icons.archive,
              onTap: () => print('Archive'),
            ),
            IconSlideAction(
              caption: 'Share',
              color: Colors.indigo,
              icon: Icons.share,
              onTap: () => print('Share'),
            ),
          ],
          secondaryActions: <Widget>[
            IconSlideAction(
              caption: 'More',
              color: Colors.black45,
              icon: Icons.more_horiz,
              onTap: () => print('More'),
            ),
            IconSlideAction(
              caption: 'Delete',
              color: Colors.red,
              icon: Icons.delete,
              onTap: () => print('Delete'),
            ),
          ],
          child: ListTile(
            title: Text('Item $index'),
            subtitle: Text('Slidable Sample'),
          ),
        );
      },
    );
  }
}

flutter_spinkit:

flutter_spinkit is a Flutter package that provides a collection of loading spinners/indicators. It allows you to easily add loading spinners to your Flutter app with a variety of styles and animation effects. The spinners are customizable, and you can set various parameters such as color, size, and animation duration.

This package provides a variety of loading spinners to choose from, including 
  • Circle, 
  • CubeGrid, 
  • FadingFour, 
  • FadingCircle, 
  • Pulse, 
  • RotatingCircle, 
  • and so no. 
The spinners can be used to indicate that the app is fetching data or performing a background task, and provides visual feedback to the user. The flutter_spinkit package is a popular choice for adding loading spinners to Flutter apps.

You can try out this Example:

import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class MyLoadingPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: SpinKitFadingCircle(
        color: Colors.orange,
        size: 50.0,
      ),
    );
  }
}

provider:

Comments

Popular posts from this blog

Error Handling in Flutter - Gradle issue

How to Make a Dynamic and Trending ListView with Flutter Widgets?

Understanding API integration with Getx State management