Chat Page
Theme
Create a new file theme.dart - this is just a text box decoration
import 'package:flutter/material.dart';
InputDecoration textFieldDecoration(BuildContext context, String hintText) =>
InputDecoration(
contentPadding: const EdgeInsets.all(15),
hintText: hintText,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
Radius.circular(14),
),
borderSide: BorderSide(
color: Theme.of(context).colorScheme.secondary,
),
),
focusedBorder: OutlineInputBorder(
borderRadius: const BorderRadius.all(
Radius.circular(14),
),
borderSide: BorderSide(
color: Theme.of(context).colorScheme.secondary,
),
),
);
Create a new dart file chat_screen.dart
import 'package:flutter/material.dart';
import 'chat_widget.dart';
class ChatScreen extends StatefulWidget {
const ChatScreen({super.key, required this.title});
final String title;
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Text(
widget.title,
style: const TextStyle(color: Colors.white),
),
),
body: SingleChildScrollView(
child: Column(
children: [
Image.asset(
"assets/images/movie.jpeg",
height: 200,
width: double.infinity,
fit: BoxFit.cover,
),
const Text(
"Hint - List of Emoji's",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
const ChatWidget(),
],
),
),
);
}
}
Chat Screen needs a chat widget
import 'package:flutter/material.dart';
import 'theme.dart';
class ChatWidget extends StatefulWidget {
const ChatWidget({super.key});
@override
State<ChatWidget> createState() => _ChatWidgetState();
}
class _ChatWidgetState extends State<ChatWidget> {
final ScrollController _scrollController = ScrollController();
final TextEditingController _textController = TextEditingController();
final FocusNode _textFieldFocus = FocusNode(debugLabel: 'TextField');
bool _loading = false;
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListView.builder(
shrinkWrap: true,
controller: _scrollController,
itemBuilder: (context, idx) {
return Container();
},
itemCount: 2,
),
Padding(
padding: const EdgeInsets.symmetric(
vertical: 25,
horizontal: 15,
),
child: Row(
children: [
Expanded(
child: TextField(
style: const TextStyle(color: Colors.white),
autofocus: true,
focusNode: _textFieldFocus,
decoration: textFieldDecoration(
context, 'Enter a name of the movie...'),
controller: _textController,
onSubmitted: (String value) {
_sendChatMessage(value);
},
),
),
const SizedBox.square(
dimension: 15,
),
if (!_loading)
IconButton(
onPressed: () async {
_sendChatMessage(_textController.text);
},
icon: Icon(
Icons.send,
color: Theme.of(context).primaryColor,
),
)
else
const CircularProgressIndicator(),
],
),
),
],
),
);
}
Future<void> _sendChatMessage(String message) async {
setState(() {
_loading = true;
});
}
void _scrollDown() {
WidgetsBinding.instance.addPostFrameCallback(
(_) => _scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(
milliseconds: 750,
),
curve: Curves.easeOutCirc,
),
);
}
void _showError(String message) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Something went wrong'),
content: SingleChildScrollView(
child: Text(message),
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('OK'),
)
],
);
},
);
}
}
Modify main.dart and add Chat screen as
home: const ChatScreen(title: 'Flutter + Generative AI'),
Last updated