Skip to content

Instantly share code, notes, and snippets.

@aliyazdi75
Created October 13, 2023 23:20
Show Gist options
  • Save aliyazdi75/db316b95ec63afb8a6480b42c6790131 to your computer and use it in GitHub Desktop.
Save aliyazdi75/db316b95ec63afb8a6480b42c6790131 to your computer and use it in GitHub Desktop.
flutter_chat_firebase_config

flutter_chat_firebase_config

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
Future<void> main() async {
await Firebase.initializeApp(
options: const FirebaseOptions(
apiKey: 'AIzaSyBFQIi3pNiQNoM4y7dBKxBCp-lzeqSPfg8',
appId: '1:702148614087:web:7615cf62cdbb20e7c52be2',
messagingSenderId: '702148614087',
projectId: 'flutter-chat-aliyazdi',
authDomain: 'flutter-chat-aliyazdi.firebaseapp.com',
storageBucket: 'flutter-chat-aliyazdi.appspot.com',
),
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Chat',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final textMessageController = TextEditingController();
final messages = List<ChatMessage>.empty(growable: true);
bool _isTextValidated = true;
void _sendMessage() {
if (textMessageController.text.isEmpty) {
setState(() {
_isTextValidated = false;
});
} else {
setState(() {
messages.insert(
0,
ChatMessage(
time: DateTime.now().millisecondsSinceEpoch,
message: textMessageController.text,
),
);
});
textMessageController.clear();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('Chat Page'),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: ListView.builder(
itemCount: messages.length,
reverse: true,
padding: const EdgeInsets.symmetric(vertical: 8.0),
itemBuilder: (context, index) {
return ListTile(
leading: DefaultTextStyle.merge(
style: const TextStyle(color: Colors.indigo),
child: Text(messages[index].timestamp),
),
title: Text(messages[index].message),
);
},
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: TextField(
controller: textMessageController,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: const Icon(Icons.send),
onPressed: _sendMessage,
),
border: const OutlineInputBorder(),
labelText: 'Enter your message',
errorText:
_isTextValidated ? null : 'Message can not be empty',
),
onChanged: (value) {
setState(() {
_isTextValidated = true;
});
},
onSubmitted: (value) => _sendMessage(),
),
),
],
),
),
);
}
}
class ChatMessage {
ChatMessage({
required this.time,
required this.message,
});
final int time;
final String message;
String get timestamp =>
'${DateTime.fromMillisecondsSinceEpoch(time).hour}:${DateTime.fromMillisecondsSinceEpoch(time).minute}';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment