Created with <3 with dartpad.dev.
Created
November 23, 2022 08:20
-
-
Save gausoft/eca85552fc161e64588453e6cac992fe to your computer and use it in GitHub Desktop.
Counter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Chatiii', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const ChatPage(title: 'Chatiii Demo'), | |
); | |
} | |
} | |
class ChatPage extends StatefulWidget { | |
final String title; | |
const ChatPage({ | |
Key? key, | |
required this.title, | |
}) : super(key: key); | |
@override | |
State<ChatPage> createState() => _ChatPageState(); | |
} | |
class _ChatPageState extends State<ChatPage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: ListView( | |
reverse: true, | |
children: const [ | |
ChatBubble( | |
text: 'How was the concert?', | |
isCurrentUser: false, | |
), | |
ChatBubble( | |
text: 'Awesome! Next time you gotta come as well!', | |
isCurrentUser: true, | |
), | |
ChatBubble( | |
text: 'Ok, when is the next date?', | |
isCurrentUser: false, | |
), | |
ChatBubble( | |
text: 'They\'re playing on the 20th of November', | |
isCurrentUser: true, | |
), | |
ChatBubble( | |
text: 'Let\'s do it!', | |
isCurrentUser: false, | |
), | |
], | |
), | |
); | |
} | |
} | |
class ChatBubble extends StatelessWidget { | |
const ChatBubble({ | |
Key? key, | |
required this.text, | |
required this.isCurrentUser, | |
}) : super(key: key); | |
final String text; | |
final bool isCurrentUser; | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding: EdgeInsets.fromLTRB( | |
isCurrentUser ? 64.0 : 16.0, | |
4, | |
isCurrentUser ? 16.0 : 64.0, | |
4, | |
), | |
child: Align( | |
// align the child within the container | |
alignment: isCurrentUser ? Alignment.centerRight : Alignment.centerLeft, | |
child: DecoratedBox( | |
// chat bubble decoration | |
decoration: BoxDecoration( | |
color: isCurrentUser ? Colors.blue : Colors.grey[300], | |
borderRadius: BorderRadius.circular(16), | |
), | |
child: Padding( | |
padding: const EdgeInsets.all(12), | |
child: Text( | |
text, | |
style: Theme.of(context).textTheme.bodyText1!.copyWith( | |
color: isCurrentUser ? Colors.white : Colors.black87,), | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment