flutter_ai_agent_tool 0.1.0 copy "flutter_ai_agent_tool: ^0.1.0" to clipboard
flutter_ai_agent_tool: ^0.1.0 copied to clipboard

A comprehensive toolkit for building AI-powered Flutter applications with multi-provider support, streaming responses, memory management, and extensible tool system.

example/lib/main.dart

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter AI Toolkit Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter AI Toolkit Examples'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          _ExampleCard(
            title: 'OpenAI Chat',
            description: 'Chat with GPT models from OpenAI',
            icon: Icons.chat_bubble,
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => const ChatScreen(
                    title: 'OpenAI Chat',
                    providerType: ProviderType.openai,
                  ),
                ),
              );
            },
          ),
          _ExampleCard(
            title: 'Anthropic (Claude)',
            description: 'Chat with Claude from Anthropic',
            icon: Icons.psychology,
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => const ChatScreen(
                    title: 'Claude Chat',
                    providerType: ProviderType.anthropic,
                  ),
                ),
              );
            },
          ),
          _ExampleCard(
            title: 'Google AI (Gemini)',
            description: 'Chat with Gemini from Google',
            icon: Icons.auto_awesome,
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => const ChatScreen(
                    title: 'Gemini Chat',
                    providerType: ProviderType.google,
                  ),
                ),
              );
            },
          ),
          _ExampleCard(
            title: 'xAI (Grok)',
            description: 'Chat with Grok - The world\'s most intelligent model',
            icon: Icons.flash_on,
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => const ChatScreen(
                    title: 'Grok Chat',
                    providerType: ProviderType.grok,
                  ),
                ),
              );
            },
          ),
          _ExampleCard(
            title: 'Groq',
            description: 'Ultra-fast inference with Llama, Mixtral & more',
            icon: Icons.speed,
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => const ChatScreen(
                    title: 'Groq Chat',
                    providerType: ProviderType.groq,
                  ),
                ),
              );
            },
          ),
          _ExampleCard(
            title: 'With Tools',
            description: 'AI agent with calculator and datetime tools',
            icon: Icons.build,
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => const ToolsExampleScreen(),
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}

class _ExampleCard extends StatelessWidget {
  final String title;
  final String description;
  final IconData icon;
  final VoidCallback onTap;

  const _ExampleCard({
    required this.title,
    required this.description,
    required this.icon,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.only(bottom: 16),
      child: ListTile(
        leading: Icon(icon, size: 32),
        title: Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
        subtitle: Text(description),
        trailing: const Icon(Icons.arrow_forward_ios),
        onTap: onTap,
      ),
    );
  }
}

enum ProviderType { openai, anthropic, google, grok, groq }

class ChatScreen extends StatefulWidget {
  final String title;
  final ProviderType providerType;

  const ChatScreen({
    super.key,
    required this.title,
    required this.providerType,
  });

  @override
  State<ChatScreen> createState() => _ChatScreenState();
}

class _ChatScreenState extends State<ChatScreen> {
  late AIAgent agent;
  final TextEditingController _apiKeyController = TextEditingController();
  bool _isConfigured = false;

  @override
  void dispose() {
    _apiKeyController.dispose();
    if (_isConfigured) {
      agent.dispose();
    }
    super.dispose();
  }

  void _configureAgent() {
    if (_apiKeyController.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Please enter an API key')),
      );
      return;
    }

    AIProvider provider;
    switch (widget.providerType) {
      case ProviderType.openai:
        provider = OpenAIProvider(
          config: OpenAIConfig(
            apiKey: _apiKeyController.text,
            model: 'gpt-4',
          ),
        );
        break;
      case ProviderType.anthropic:
        provider = AnthropicProvider(
          config: AnthropicConfig(
            apiKey: _apiKeyController.text,
            model: 'claude-3-5-sonnet-20241022',
          ),
        );
        break;
      case ProviderType.google:
        provider = GoogleAIProvider(
          config: GoogleAIConfig(
            apiKey: _apiKeyController.text,
            model: 'gemini-pro',
          ),
        );
        break;
      case ProviderType.grok:
        provider = GrokProvider(
          config: GrokConfig(
            apiKey: _apiKeyController.text,
            model: 'grok-beta',
          ),
        );
        break;
      case ProviderType.groq:
        provider = GroqProvider(
          config: GroqConfig(
            apiKey: _apiKeyController.text,
            model: 'llama-3.3-70b-versatile',
          ),
        );
        break;
    }

    agent = AIAgent(
      provider: provider,
      config: const AIAgentConfig(
        systemPrompt: 'You are a helpful AI assistant.',
      ),
      memoryManager: ConversationMemory(maxMessages: 50),
    );

    setState(() {
      _isConfigured = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (!_isConfigured) {
      return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Icon(Icons.key, size: 64, color: Colors.grey),
              const SizedBox(height: 24),
              const Text(
                'Enter your API key to continue',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 16),
              TextField(
                controller: _apiKeyController,
                decoration: const InputDecoration(
                  labelText: 'API Key',
                  border: OutlineInputBorder(),
                  hintText: 'sk-...',
                ),
                obscureText: true,
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: _configureAgent,
                child: const Text('Start Chat'),
              ),
            ],
          ),
        ),
      );
    }

    return Scaffold(
      body: ChatView(
        agent: agent,
        title: widget.title,
      ),
    );
  }
}

class ToolsExampleScreen extends StatefulWidget {
  const ToolsExampleScreen({super.key});

  @override
  State<ToolsExampleScreen> createState() => _ToolsExampleScreenState();
}

class _ToolsExampleScreenState extends State<ToolsExampleScreen> {
  late AIAgent agent;
  final TextEditingController _apiKeyController = TextEditingController();
  bool _isConfigured = false;

  @override
  void dispose() {
    _apiKeyController.dispose();
    if (_isConfigured) {
      agent.dispose();
    }
    super.dispose();
  }

  void _configureAgent() {
    if (_apiKeyController.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Please enter an OpenAI API key')),
      );
      return;
    }

    final provider = OpenAIProvider(
      config: OpenAIConfig(
        apiKey: _apiKeyController.text,
        model: 'gpt-4',
      ),
    );

    agent = AIAgent(
      provider: provider,
      config: const AIAgentConfig(
        systemPrompt: 'You are a helpful assistant with access to tools. '
            'Use the calculator for math and get_datetime for time information.',
      ),
      memoryManager: ConversationMemory(),
    );

    // Add tools
    agent.addTool(CalculatorTool());
    agent.addTool(DateTimeTool());

    setState(() {
      _isConfigured = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (!_isConfigured) {
      return Scaffold(
        appBar: AppBar(
          title: const Text('AI with Tools'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Icon(Icons.build, size: 64, color: Colors.grey),
              const SizedBox(height: 24),
              const Text(
                'AI Agent with Calculator & DateTime Tools',
                style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 8),
              const Text(
                'Try asking: "What is 25 * 47?" or "What time is it?"',
                style: TextStyle(color: Colors.grey),
                textAlign: TextAlign.center,
              ),
              const SizedBox(height: 24),
              TextField(
                controller: _apiKeyController,
                decoration: const InputDecoration(
                  labelText: 'OpenAI API Key',
                  border: OutlineInputBorder(),
                  hintText: 'sk-...',
                ),
                obscureText: true,
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: _configureAgent,
                child: const Text('Start Chat'),
              ),
            ],
          ),
        ),
      );
    }

    return Scaffold(
      body: ChatView(
        agent: agent,
        title: 'AI with Tools',
      ),
    );
  }
}
2
likes
150
points
41
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A comprehensive toolkit for building AI-powered Flutter applications with multi-provider support, streaming responses, memory management, and extensible tool system.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

equatable, flutter, http, json_annotation, rxdart, shared_preferences, uuid

More

Packages that depend on flutter_ai_agent_tool