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.
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:
- GET,
- POST,
- PUT,
- DELETE,
- and so on.
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');
}
}
flutter_slidable:
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:
- Circle,
- CubeGrid,
- FadingFour,
- FadingCircle,
- Pulse,
- RotatingCircle,
- and so no.
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,
),
);
}
}
Comments
Post a Comment