flutter_sync_manager_pro 1.0.1 copy "flutter_sync_manager_pro: ^1.0.1" to clipboard
flutter_sync_manager_pro: ^1.0.1 copied to clipboard

Offline-first data synchronization engine with delta sync, conflict resolution, and background syncing for Flutter applications.

example/main.dart

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize the sync engine
  final syncEngine = SyncEngine(
    adapter: RestApiAdapter(
      baseUrl: 'https://api.example.com', // Replace with your API
      headers: {
        'Authorization': 'Bearer YOUR_TOKEN', // Replace with your token
      },
    ),
    config: SyncConfig(
      autoSync: true,
      syncInterval: Duration(minutes: 5),
      enableDeltaSync: true,
      enableLogging: true,
    ),
    conflictResolver: ConflictResolver(
      strategy: ConflictStrategy.serverWins,
    ),
  );

  await syncEngine.initialize();

  runApp(MyApp(syncEngine: syncEngine));
}

class MyApp extends StatelessWidget {
  final SyncEngine syncEngine;

  const MyApp({required this.syncEngine, Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sync Engine Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: TodoListScreen(syncEngine: syncEngine),
    );
  }
}

class TodoListScreen extends StatefulWidget {
  final SyncEngine syncEngine;

  const TodoListScreen({required this.syncEngine, Key? key}) : super(key: key);

  @override
  State<TodoListScreen> createState() => _TodoListScreenState();
}

class _TodoListScreenState extends State<TodoListScreen> {
  List<Map<String, dynamic>> todos = [];
  SyncStats? currentStats;
  final _titleController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _loadTodos();
    _setupSyncListener();
  }

  void _setupSyncListener() {
    widget.syncEngine.statsStream.listen((stats) {
      setState(() {
        currentStats = stats;
      });
    });
  }

  Future<void> _loadTodos() async {
    final data = await widget.syncEngine.getAll('todos');
    setState(() {
      todos = data;
      todos.sort((a, b) {
        final aTime = DateTime.parse(a['createdAt'] as String);
        final bTime = DateTime.parse(b['createdAt'] as String);
        return bTime.compareTo(aTime);
      });
    });
  }

  Future<void> _addTodo() async {
    final title = _titleController.text.trim();
    if (title.isEmpty) return;

    final todo = {
      'id': DateTime.now().millisecondsSinceEpoch.toString(),
      'title': title,
      'completed': false,
      'createdAt': DateTime.now().toIso8601String(),
    };

    await widget.syncEngine.save('todos', todo);
    _titleController.clear();
    await _loadTodos();

    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Todo added (will sync when online)')),
    );
  }

  Future<void> _toggleTodo(Map<String, dynamic> todo) async {
    final updated = {
      ...todo,
      'completed': !(todo['completed'] as bool),
      'updatedAt': DateTime.now().toIso8601String(),
    };

    await widget.syncEngine.save('todos', updated);
    await _loadTodos();
  }

  Future<void> _deleteTodo(String id) async {
    await widget.syncEngine.delete('todos', id);
    await _loadTodos();

    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Todo deleted (will sync when online)')),
    );
  }

  Future<void> _manualSync() async {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Syncing...')),
    );

    await widget.syncEngine.sync();

    if (!mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('Sync completed!')),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Offline Todos'),
        actions: [
          IconButton(
            icon: Icon(Icons.sync),
            onPressed: _manualSync,
            tooltip: 'Sync now',
          ),
          IconButton(
            icon: Icon(Icons.info_outline),
            onPressed: () => _showStatsDialog(),
            tooltip: 'Sync stats',
          ),
        ],
      ),
      body: Column(
        children: [
          // Sync status banner
          if (currentStats != null) _buildSyncBanner(),

          // Add todo input
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: _titleController,
                    decoration: InputDecoration(
                      hintText: 'Enter todo title',
                      border: OutlineInputBorder(),
                    ),
                    onSubmitted: (_) => _addTodo(),
                  ),
                ),
                SizedBox(width: 8),
                ElevatedButton(
                  onPressed: _addTodo,
                  child: Text('Add'),
                ),
              ],
            ),
          ),

          // Todo list
          Expanded(
            child: todos.isEmpty
                ? Center(
                    child: Text(
                      'No todos yet.\nAdd one above!',
                      textAlign: TextAlign.center,
                      style: TextStyle(fontSize: 16, color: Colors.grey),
                    ),
                  )
                : ListView.builder(
                    itemCount: todos.length,
                    itemBuilder: (context, index) {
                      final todo = todos[index];
                      return ListTile(
                        leading: Checkbox(
                          value: todo['completed'] as bool,
                          onChanged: (_) => _toggleTodo(todo),
                        ),
                        title: Text(
                          todo['title'] as String,
                          style: TextStyle(
                            decoration: (todo['completed'] as bool)
                                ? TextDecoration.lineThrough
                                : null,
                          ),
                        ),
                        subtitle: Text(
                          _formatDate(todo['createdAt'] as String),
                          style: TextStyle(fontSize: 12),
                        ),
                        trailing: IconButton(
                          icon: Icon(Icons.delete, color: Colors.red),
                          onPressed: () => _deleteTodo(todo['id'] as String),
                        ),
                      );
                    },
                  ),
          ),
        ],
      ),
    );
  }

  Widget _buildSyncBanner() {
    final stats = currentStats!;
    final color = stats.pendingCount > 0 ? Colors.orange : Colors.green;

    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      color: color.withOpacity(0.1),
      child: Row(
        children: [
          Icon(
            stats.pendingCount > 0 ? Icons.sync : Icons.cloud_done,
            size: 16,
            color: color,
          ),
          SizedBox(width: 8),
          Text(
            stats.pendingCount > 0
                ? '${stats.pendingCount} items pending sync'
                : 'All synced',
            style: TextStyle(color: color, fontWeight: FontWeight.w500),
          ),
          if (stats.errorCount > 0) ...[
            SizedBox(width: 16),
            Icon(Icons.error, size: 16, color: Colors.red),
            SizedBox(width: 4),
            Text(
              '${stats.errorCount} errors',
              style: TextStyle(color: Colors.red),
            ),
          ],
        ],
      ),
    );
  }

  void _showStatsDialog() {
    if (currentStats == null) return;

    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('Sync Statistics'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildStatRow('Pending', currentStats!.pendingCount, Colors.orange),
            _buildStatRow('Synced', currentStats!.syncedCount, Colors.green),
            _buildStatRow('Errors', currentStats!.errorCount, Colors.red),
            _buildStatRow('Conflicts', currentStats!.conflictCount, Colors.purple),
            SizedBox(height: 8),
            if (currentStats!.lastSyncTime != null)
              Text(
                'Last sync: ${_formatDate(currentStats!.lastSyncTime!.toIso8601String())}',
                style: TextStyle(fontSize: 12, color: Colors.grey),
              ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text('Close'),
          ),
        ],
      ),
    );
  }

  Widget _buildStatRow(String label, int count, Color color) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(label, style: TextStyle(fontWeight: FontWeight.w500)),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 12, vertical: 4),
            decoration: BoxDecoration(
              color: color.withOpacity(0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Text(
              count.toString(),
              style: TextStyle(color: color, fontWeight: FontWeight.bold),
            ),
          ),
        ],
      ),
    );
  }

  String _formatDate(String isoDate) {
    final date = DateTime.parse(isoDate);
    final now = DateTime.now();
    final diff = now.difference(date);

    if (diff.inDays > 0) {
      return '${diff.inDays} day${diff.inDays > 1 ? 's' : ''} ago';
    } else if (diff.inHours > 0) {
      return '${diff.inHours} hour${diff.inHours > 1 ? 's' : ''} ago';
    } else if (diff.inMinutes > 0) {
      return '${diff.inMinutes} min ago';
    } else {
      return 'Just now';
    }
  }

  @override
  void dispose() {
    _titleController.dispose();
    super.dispose();
  }
}
1
likes
160
points
122
downloads

Publisher

unverified uploader

Weekly Downloads

Offline-first data synchronization engine with delta sync, conflict resolution, and background syncing for Flutter applications.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter, http, path, shared_preferences, sqflite, uuid

More

Packages that depend on flutter_sync_manager_pro