flutter_api_kit 1.0.0
flutter_api_kit: ^1.0.0 copied to clipboard
A robust Flutter package to handle API requests with built-in loading buttons, snackbars, and in-button download progress indicators.
example/lib/main.dart
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_api_kit/flutter_api_kit.dart';
void main() {
ApiKit.config(
baseUrl: 'https://jsonplaceholder.typicode.com',
enableLogging: true, // New logging feature
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ApiKit Demo'),
backgroundColor: Colors.deepPurple,
foregroundColor: Colors.white,
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'ApiKit Features',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 30),
KitLoadingButton(
label: 'GET Request',
width: double.infinity,
onPressed: (_) async {
final res = await ApiKit.get(
context: context,
endpoint: '/posts/1',
);
if (res.success) log('Data: ${res.data}');
},
),
const SizedBox(height: 16),
KitLoadingButton(
label: 'POST Request',
width: double.infinity,
backgroundColor: Colors.deepPurple,
borderRadius: 10,
onPressed: (_) async {
await ApiKit.post(
context: context,
endpoint: '/posts',
body: {'title': 'Hello', 'body': 'World', 'userId': 1},
successMessage: 'Post created successfully!',
);
},
),
const SizedBox(height: 16),
KitLoadingButton(
label: 'PUT Request (Update)',
width: double.infinity,
backgroundColor: Colors.orange,
onPressed: (_) async {
await ApiKit.put(
context: context,
endpoint: '/posts/1',
body: {'id': 1, 'title': 'Updated Title', 'body': 'Updated Body', 'userId': 1},
successMessage: 'Post updated successfully!',
);
},
),
const SizedBox(height: 16),
KitLoadingButton(
label: 'DELETE Request',
width: double.infinity,
backgroundColor: Colors.redAccent,
onPressed: (_) async {
await ApiKit.delete(
context: context,
endpoint: '/posts/1',
successMessage: 'Post deleted successfully!',
);
},
),
const SizedBox(height: 16),
KitLoadingButton(
label: 'Download File (In-Button Progress)',
width: double.infinity,
backgroundColor: Colors.blueAccent,
progressColor: Colors.yellow,
onPressed: (setProgress) async {
await ApiKit.download(
context: context,
url: 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf',
savePath: 'dummy.pdf',
showDialog: false,
onProgress: (p) => setProgress(p),
onComplete: () {
KitSnackbar.success(context, 'File downloaded successfully!');
},
);
},
),
],
),
),
),
);
}
}