truthinscanner 0.0.2-dev4
truthinscanner: ^0.0.2-dev4 copied to clipboard
A Flutter package for barcode scanning with TruthIn product database integration. Supports multiple barcode formats and provides real-time product information.
TruthIn Scanner #
A Flutter package for barcode scanning with TruthIn product database integration. Scan barcodes and get instant access to detailed product information including nutritional facts, ingredients, ratings, and health insights.
Platform Support #
This package currently supports Android and iOS platforms only.
| Platform | Support |
|---|---|
| Android | ✅ |
| iOS | ✅ |
| Web | ❌ |
| Desktop | ❌ |
Features #
- Fast Barcode Scanning: Real-time barcode detection with camera preview
- Multiple Format Support: Supports EAN13, UPC-A, EAN8, UPC-E, ITF, Code39, and Code128
- Product Information: Detailed product data including:
- Product name, brand, and images
- TruthIn health ratings
- Nutritional facts and serving sizes
- Ingredient lists and additives
- Allergen information
- Health tags and dietary information
- Integrated API: Built-in API client for TruthIn product database
- Customizable: Support for custom base URLs and configuration
- Easy Integration: Simple API key initialization
Upcoming Features #
We're actively working on enhancing the package with the following features:
- UI Customization: Full customization support for fonts, colors, and other UI-related configurations
- Dark Mode Support: Native dark mode theme support for better user experience
- Reduced Package Footprint: Optimizations to decrease the overall package size and improve performance
- Performance Optimizations: Enhanced scanning speed and reduced memory consumption
- Dependency Override Protocols: Ability to use app-level dependencies for API calls, giving you more control over HTTP clients and networking libraries
Stay tuned for these updates in future releases!
Installation #
Add truthinscanner to your pubspec.yaml:
Option 1: Local Path (for development) #
dependencies:
truthinscanner:
path: ../truthinscanner
Option 2: Git Repository #
dependencies:
truthinscanner:
git:
url: https://github.com/yourusername/truthinscanner.git
ref: main
Then run:
flutter pub get
Requirements #
Platforms: Android and iOS only (Web and Desktop are not supported)
- Flutter SDK: ^3.9.2
- Dart SDK: ^3.9.2
- iOS: 11.0 or higher
- Android: API level 21 (Android 5.0) or higher
Platform-Specific Setup #
iOS
Add camera permissions to your ios/Runner/Info.plist:
<key>NSCameraUsageDescription</key>
<string>This app needs camera access to scan barcodes</string>
Android
Add camera permissions to your android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera"/>
Usage #
1. Initialize the Package #
Initialize TruthInScanner with your API key in main.dart before running your app:
import 'package:flutter/material.dart';
import 'package:truthinscanner/truthinscanner.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize TruthInScanner with your API key
TruthInScanner.initialize(
apiKey: 'your-api-key-here',
// baseUrl: 'https://custom-api-url.com/', // Optional: custom base URL
);
runApp(MyApp());
}
2. Use the Barcode Scanner #
Add the BarcodeScannerView widget to your app:
import 'package:flutter/material.dart';
import 'package:truthinscanner/truthinscanner.dart';
class ScannerPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Scan Product'),
),
body: BarcodeScannerView(),
);
}
}
3. Navigate to Scanner #
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BarcodeScannerView(),
),
);
},
child: Text('Scan Barcode'),
)
Complete Example #
import 'package:flutter/material.dart';
import 'package:truthinscanner/truthinscanner.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize TruthInScanner
TruthInScanner.initialize(
apiKey: 'your-api-key-here',
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TruthIn Scanner Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TruthIn Scanner'),
centerTitle: true,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.qr_code_scanner,
size: 100,
color: Colors.blue,
),
SizedBox(height: 40),
Text(
'Scan Product Barcodes',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 60),
ElevatedButton.icon(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BarcodeScannerView(),
),
);
},
icon: Icon(Icons.qr_code_scanner),
label: Text('Start Scanning'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(
horizontal: 32,
vertical: 16,
),
),
),
],
),
),
);
}
}
API Reference #
TruthInScanner #
Main initialization class for the package.
Methods
initialize({required String apiKey, String? baseUrl})
Initialize the scanner with your API key. Must be called before using any scanner functionality.
Parameters:
apiKey(String, required): Your TruthIn API keybaseUrl(String, optional): Custom base URL for API calls
TruthInScanner.initialize(
apiKey: 'your-api-key',
baseUrl: 'https://custom-url.com/', // Optional
);
apiKey (getter)
Get the configured API key. Throws an exception if not initialized.
isInitialized (getter)
Check if TruthInScanner has been initialized.
if (TruthInScanner.instance.isInitialized) {
// Scanner is ready
}
BarcodeScannerView #
The main barcode scanner widget with camera preview.
class BarcodeScannerView extends StatefulWidget
Features:
- Real-time barcode detection
- Camera preview with detection visualization
- Automatic product lookup
- Bottom sheet with product details
- Support for multiple barcode formats
ProductsApi (Advanced) #
Direct access to the TruthIn API for advanced use cases.
import 'package:truthinscanner/truthinscanner.dart';
// Get product details
final api = ProductsApi();
final productDetails = await api.getProductDetail(
barcode: '1234567890123',
);
Supported Barcode Formats #
- EAN13 - European Article Number (13 digits)
- UPC-A - Universal Product Code (12 digits)
- EAN8 - European Article Number (8 digits)
- UPC-E - Compressed UPC code
- ITF - Interleaved 2 of 5
- Code39 - Standard barcode format
- Code128 - High-density barcode format
Configuration #
Custom API Base URL #
To use a custom API endpoint:
TruthInScanner.initialize(
apiKey: 'your-api-key',
baseUrl: 'https://your-custom-api.com/',
);
Environment Variables #
You can also configure the base URL using environment variables:
flutter run --dart-define=API_URL=https://your-custom-api.com/
Error Handling #
The package handles various error scenarios:
- Product Not Found: Displays a not found screen with options to upload product images
- Product Under Review: Shows a review pending message
- Network Errors: Handled gracefully with user feedback
- Camera Permissions: Prompts user to grant camera access
Troubleshooting #
Camera Not Working #
iOS: Ensure camera permissions are added to Info.plist
Android: Verify camera permissions in AndroidManifest.xml and request runtime permissions
API Key Issues #
If you see "TruthInScanner not initialized" error:
- Ensure
TruthInScanner.initialize()is called inmain()beforerunApp() - Verify your API key is correct
Build Errors #
Run these commands to resolve dependency issues:
flutter clean
flutter pub get
Dependencies #
This package uses:
camera- Camera functionalitygoogle_mlkit_barcode_scanning- ML Kit barcode scanningdio- HTTP client for API callsflutter_svg- SVG renderingshimmer- Loading animationsauto_size_text- Responsive textvisibility_detector- Widget visibility detectionpermission_handler- Runtime permissions handling
Example App #
Check the example/ directory for a complete working example:
cd example
flutter run
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Support #
For issues, questions, or feature requests, please file an issue on the GitHub repository.
API Key #
To get your TruthIn API key, contact TruthIn support or visit the TruthIn developer portal.
Made with ❤️ by the TruthIn team