erpnext_sdk_flutter 1.0.2
erpnext_sdk_flutter: ^1.0.2 copied to clipboard
A production-ready ERPNext SDK for Flutter/Dart focused on mobile apps.
import 'package:flutter/material.dart';
import 'package:erpnext_sdk_flutter/erpnext_sdk_flutter.dart';
// Actually, I should probably not add extra dependencies to example unless requested, but image picker is standard for "upload attachment".
// I'll stick to basic file picking or just a dummy file for the example code to keep it runnable without too many plugins.
// Or I can just use a text field to create a dummy file.
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ERPNext SDK Demo',
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: const LoginPage(),
);
}
}
// Global client instance for demo purposes
final client = ERPNextClient(
'https://demo.erpnext.com',
); // Replace with your instance
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
bool _isLoading = false;
String? _error;
Future<void> _login() async {
setState(() {
_isLoading = true;
_error = null;
});
try {
await client.auth.loginWithCredentials(
_usernameController.text,
_passwordController.text,
);
if (mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const HomeScreen()),
);
}
} catch (e) {
setState(() => _error = e.toString());
} finally {
setState(() => _isLoading = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Login')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _usernameController,
decoration: const InputDecoration(labelText: 'Username/Email'),
),
TextField(
controller: _passwordController,
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
),
const SizedBox(height: 20),
if (_error != null)
Text(_error!, style: const TextStyle(color: Colors.red)),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _isLoading ? null : _login,
child: _isLoading
? const CircularProgressIndicator()
: const Text('Login'),
),
],
),
),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<dynamic> _items = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadItems();
}
Future<void> _loadItems() async {
try {
// Listing ToDo items as an example
final items = await client.doctype.list(
'ToDo',
fields: ['name', 'description', 'status'],
limitPageLength: 10,
);
setState(() {
_items = items;
_isLoading = false;
});
} catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error loading items: $e')));
setState(() => _isLoading = false);
}
}
Future<void> _createItem() async {
final description = await showDialog<String>(
context: context,
builder: (context) {
final controller = TextEditingController();
return AlertDialog(
title: const Text('New ToDo'),
content: TextField(
controller: controller,
decoration: const InputDecoration(hintText: 'Description'),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.pop(context, controller.text),
child: const Text('Create'),
),
],
);
},
);
if (description != null && description.isNotEmpty) {
try {
await client.document.createDocument('ToDo', {
'description': description,
'status': 'Open',
});
_loadItems();
} catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Error creating item: $e')));
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ToDo List'),
actions: [
IconButton(
icon: const Icon(Icons.logout),
onPressed: () async {
await client.auth.logout();
if (mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const LoginPage()),
);
}
},
),
],
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: _items.length,
itemBuilder: (context, index) {
final item = _items[index];
return ListTile(
title: Text(item['description'] ?? 'No Description'),
subtitle: Text(item['status'] ?? ''),
trailing: IconButton(
icon: const Icon(Icons.delete),
onPressed: () async {
try {
await client.document.deleteDocument(
'ToDo',
item['name'],
);
_loadItems();
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error deleting: $e')),
);
}
},
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: _createItem,
child: const Icon(Icons.add),
),
);
}
}