william

 avatar
unknown
plain_text
a year ago
5.1 kB
6
Indexable
import 'package:flutter/material.dart';
import 'package:flutter_tts/flutter_tts.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;

void main() {
  runApp(VirtualAssistantApp());
}

class VirtualAssistantApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Virtual Assistant',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: VirtualAssistantScreen(),
    );
  }
}

class VirtualAssistantScreen extends StatefulWidget {
  @override
  _VirtualAssistantScreenState createState() => _VirtualAssistantScreenState();
}

class _VirtualAssistantScreenState extends State<VirtualAssistantScreen> {
  String _assistantName = "William"; // Default name
  String _assistantVoice = "en-US"; // Default voice
  List<String> _openApps = [];
  stt.SpeechToText _speech;
  FlutterTts _flutterTts;

  @override
  void initState() {
    super.initState();
    _speech = stt.SpeechToText();
    _flutterTts = FlutterTts();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Virtual Assistant'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Hello, I\'m $_assistantName!',
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                _openApp("Calendar");
              },
              child: Text('Open Calendar'),
            ),
            ElevatedButton(
              onPressed: () {
                _changeName("Vicky");
              },
              child: Text('Change Name to Vicky'),
            ),
            ElevatedButton(
              onPressed: () {
                _changeVoice("en-US"); // Change to desired default voice
              },
              child: Text('Change Voice to English'),
            ),
            ElevatedButton(
              onPressed: () {
                _startListening();
              },
              child: Text('Start Listening for Commands'),
            ),
          ],
        ),
      ),
    );
  }

  // Open the specified app
  Future<void> _openApp(String appName) async {
    if (!_openApps.contains(appName)) {
      _openApps.add(appName);
      print("Opening $appName app...");
      _showSnackBar("Opening $appName app...");
    } else {
      print("$appName app is already open.");
      _showSnackBar("$appName app is already open.");
    }
  }

  // Change the name of the virtual assistant
  void _changeName(String newName) {
    setState(() {
      _assistantName = newName;
    });
    print("Great! You can call me $_assistantName from now on.");
    _showSnackBar("Name changed to $_assistantName.");
  }

  // Change the voice of the virtual assistant
  void _changeVoice(String newVoice) async {
    List<dynamic> voices = await _flutterTts.getVoices;
    bool isSupported = voices.any((voice) => voice["locale"] == newVoice);
    if (isSupported) {
      await _flutterTts.setLanguage(newVoice);
      setState(() {
        _assistantVoice = newVoice;
      });
      print("Sure! I'll speak with a $_assistantVoice voice from now on.");
      _showSnackBar("Voice changed to $_assistantVoice.");
    } else {
      print("Voice $newVoice is not supported.");
      _showSnackBar("Voice $newVoice is not supported.");
    }
  }

  // Start listening for voice commands
  void _startListening() async {
    if (!_speech.isListening) {
      bool available = await _speech.initialize();
      if (available) {
        _speech.listen(
          onResult: (result) {
            String command = result.recognizedWords;
            print("You said: $command");
            _processVoiceCommand(command);
          },
        );
        _showSnackBar("Listening for commands...");
      } else {
        print('Speech recognition not available.');
        _showSnackBar("Speech recognition not available.");
      }
    }
  }

  // Process voice command and take appropriate action
  void _processVoiceCommand(String command) {
    command = command.toLowerCase(); // Convert command to lowercase for case insensitivity
   
    if (command.contains("open")) {
      _openApp(_extractAppName(command));
    } else if (command.contains("change name to")) {
      _changeName(_extractName(command));
    } else if (command.contains("change voice to")) {
      _changeVoice(_extractVoice(command));
    } else {
      print("Sorry, I didn't understand that command.");
      _showSnackBar("Sorry, I didn't understand that command.");
    }
  }

  // Extract app name from voice command
  String _extractAppName(String command) {
    List<String> words = command.split(" ");
Editor is loading...
Leave a Comment