LukeFalcox / App-Freelancer

0 stars 0 forks source link

Tela ListView pré-chat #7

Closed Mduardar27 closed 1 month ago

Mduardar27 commented 1 month ago

_AFAZERES:__

Mduardar27 commented 1 month ago

Código Base ( sem estilização e correção de erros extras): `import 'package:flutter/material.dart';

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

class ChatApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Chat App', theme: ThemeData( primarySwatch: Colors.blue, ), home: ConversationListScreen(), ); } }

class Conversation { final String name; final String lastMessage; final DateTime timestamp;

Conversation({required this.name, required this.lastMessage, required this.timestamp}); }

class ConversationListScreen extends StatefulWidget { @override _ConversationListScreenState createState() => _ConversationListScreenState(); }

class _ConversationListScreenState extends State { final List _conversations = [ Conversation(name: 'Alice', lastMessage: 'Oie, tudo bem?', timestamp: DateTime.now().subtract(Duration(minutes: 5))), Conversation(name: 'Roberto', lastMessage: 'Podemos marcar para amanhã?', timestamp: DateTime.now().subtract(Duration(hours: 1))), Conversation(name: 'João', lastMessage: 'Obrigada pela resposta!', timestamp: DateTime.now().subtract(Duration(minutes:10 ))), Conversation(name: 'Amanda', lastMessage: 'Nos encontramos amanhã?', timestamp: DateTime.now().subtract(Duration(minutes: 1))), Conversation(name: 'Carlos', lastMessage: 'Confirmado o pagamento.', timestamp: DateTime.now().subtract(Duration(hours: 5))), Conversation(name: 'Rebecca', lastMessage: 'A empresa ja foi contatada', timestamp: DateTime.now().subtract(Duration(hours: 2))), // Add more conversations here ];

List _filteredConversations = []; final TextEditingController _searchController = TextEditingController();

@override void initState() { super.initState(); _filteredConversations = _conversations; _searchController.addListener(_filterConversations); }

void _filterConversations() { final query = _searchController.text.toLowerCase(); setState(() { _filteredConversations = _conversations.where((conversation) { return conversation.name.toLowerCase().contains(query); }).toList(); }); }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Conversas'), ), body: Column( children: [ Padding( padding: const EdgeInsets.all(8.0), child: TextField( controller: _searchController, decoration: InputDecoration( hintText: 'Pesquise contatos', border: OutlineInputBorder(), prefixIcon: Icon(Icons.search), ), ), ), Expanded( child: ListView.builder( itemCount: _filteredConversations.length, itemBuilder: (context, index) { final conversation = _filteredConversations[index]; return ListTile( title: Text(conversation.name), subtitle: Text(conversation.lastMessage), trailing: Text('${conversation.timestamp.hour}:${conversation.timestamp.minute.toString().padLeft(2, '0')}'), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => ChatScreen(conversation: conversation), ), ); }, ); }, ), ), ], ), ); } }

class ChatScreen extends StatelessWidget { final Conversation conversation;

ChatScreen({required this.conversation});

@override Widget build(BuildContext context) { final TextEditingController _messageController = TextEditingController(); final List _messages = [ 'Oie, tudo bem com vc?', 'Como andam as coisas?', 'Estou bem! Obrigada. E vc?', 'Andam bem, espero que melhorem sempre.' ];

void _sendMessage() {
  if (_messageController.text.isNotEmpty) {
    _messages.add(_messageController.text);
    _messageController.clear();
  }
}

return Scaffold(
  appBar: AppBar(
    title: Text(conversation.name),
  ),
  body: Column(
    children: <Widget>[
      Expanded(
        child: ListView.builder(
          itemCount: _messages.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(_messages[index]),
            );
          },
        ),
      ),
      Padding(
        padding: const EdgeInsets.all(8.0),
        child: Row(
          children: <Widget>[
            Expanded(
              child: TextField(
                controller: _messageController,
                decoration: InputDecoration(
                  hintText: 'Enviar mensagem',
                ),
              ),
            ),
            IconButton(
              icon: Icon(Icons.send),
              onPressed: _sendMessage,
            ),
          ],
        ),
      ),
    ],
  ),
);

} } `