Skip to content

Instantly share code, notes, and snippets.

@collinjackson
Last active March 16, 2016 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save collinjackson/b1c75a02f47c9611aa63 to your computer and use it in GitHub Desktop.
Save collinjackson/b1c75a02f47c9611aa63 to your computer and use it in GitHub Desktop.
jackson-macpro:firechat-flutter jackson$ ./tool/show_diff.sh
================= 0...1 =====================
4a5,6
> import 'dart:math' show Random;
>
9a12,15
> theme: new ThemeData(
> primarySwatch: Colors.purple,
> accentColor: Colors.orangeAccent[400]
> ),
11,13c17
< '/': (BuildContext context) => new Scaffold(
< appBar: new AppBar(title: new Text("Firechat"))
< )
---
> '/': (BuildContext context) => new ChatScreen()
16a21,37
>
> class ChatScreen extends StatefulWidget {
> @override
> State createState() => new ChatScreenState();
> }
>
> class ChatScreenState extends State<ChatScreen> {
> String _name = "Guest${new Random().nextInt(1000)}";
>
> Widget build(BuildContext context) {
> return new Scaffold(
> appBar: new AppBar(
> title: new Text("Chatting as $_name")
> )
> );
> }
> }
================= 1...2 =====================
28a29,71
> InputValue _currentMessage = InputValue.empty;
>
> void _handleMessageChanged(InputValue value) {
> setState(() {
> _currentMessage = value;
> });
> }
>
> void _handleMessageAdded(InputValue value) {
> setState(() {
> _currentMessage = InputValue.empty;
> });
> }
>
> bool get _isComposing => _currentMessage.text.length > 0;
>
> Widget _buildTextComposer() {
> ThemeData themeData = Theme.of(context);
> return new Column(
> children: <Widget>[
> new Row(
> children: <Widget>[
> new Flexible(
> child: new Input(
> value: _currentMessage,
> hintText: 'Enter message',
> onSubmitted: _handleMessageAdded,
> onChanged: _handleMessageChanged
> )
> ),
> new Container(
> margin: new EdgeInsets.symmetric(horizontal: 4.0),
> child: new IconButton(
> icon: Icons.send,
> onPressed: _isComposing ? () => _handleMessageAdded(_currentMessage) : null,
> color: _isComposing ? themeData.accentColor : themeData.disabledColor
> )
> )
> ]
> )
> ]
> );
> }
34c77,78
< )
---
> ),
> body: _buildTextComposer()
================= 2...3 =====================
28a29,30
> Color _color = Colors.accents[new Random().nextInt(Colors.accents.length)][700];
> List<ChatMessage> _messages = <ChatMessage>[];
39a42,44
> ChatUser sender = new ChatUser(name: _name, color: _color);
> ChatMessage message = new ChatMessage(sender: sender, text: value.text);
> _messages.add(message);
78c83,123
< body: _buildTextComposer()
---
> body: new Column(
> children: <Widget>[
> new Flexible(
> child: new Block(
> padding: new EdgeInsets.symmetric(horizontal: 8.0),
> scrollAnchor: ViewportAnchor.end,
> children: _messages.map((m) => new ChatMessageListItem(m)).toList()
> )
> ),
> _buildTextComposer(),
> ]
> )
> );
> }
> }
>
> class ChatUser {
> ChatUser({ this.name, this.color });
> final String name;
> final Color color;
> }
>
> class ChatMessage {
> ChatMessage({ this.sender, this.text });
> final ChatUser sender;
> final String text;
> }
>
> class ChatMessageListItem extends StatelessWidget {
> ChatMessageListItem(this.message);
> final ChatMessage message;
>
> Widget build(BuildContext context) {
> return new ListItem(
> dense: true,
> leading: new CircleAvatar(
> child: new Text(message.sender.name[0]),
> backgroundColor: message.sender.color
> ),
> title: new Text(message.sender.name),
> subtitle: new Text(message.text)
================= 3...4 =====================
32a33,38
> @override
> void dispose() {
> for (ChatMessage message in _messages)
> message.animationController.dispose();
> }
>
41a48,50
> AnimationController animationController = new AnimationController(
> duration: new Duration(milliseconds: 300)
> );
43c52,56
< ChatMessage message = new ChatMessage(sender: sender, text: value.text);
---
> ChatMessage message = new ChatMessage(
> sender: sender,
> text: value.text,
> animationController: animationController
> );
44a58
> animationController.forward();
89c103,111
< children: _messages.map((m) => new ChatMessageListItem(m)).toList()
---
> children: _messages.map((ChatMessage message) {
> return new ChatMessageListItem(
> message: message,
> animation: new CurvedAnimation(
> parent: message.animationController,
> curve: Curves.easeIn
> )
> );
> }).toList()
106c128
< ChatMessage({ this.sender, this.text });
---
> ChatMessage({ this.sender, this.text, this.animationController });
108a131
> final AnimationController animationController;
111,112c134,137
< class ChatMessageListItem extends StatelessWidget {
< ChatMessageListItem(this.message);
---
> class ChatMessageListItem extends AnimatedWidget {
> ChatMessageListItem({ this.message, Animation animation })
> : super(animation: animation);
>
116,123c141,152
< return new ListItem(
< dense: true,
< leading: new CircleAvatar(
< child: new Text(message.sender.name[0]),
< backgroundColor: message.sender.color
< ),
< title: new Text(message.sender.name),
< subtitle: new Text(message.text)
---
> return new SizeTransition(
> sizeFactor: animation,
> axisAlignment: 0.0,
> child: new ListItem(
> dense: true,
> leading: new CircleAvatar(
> child: new Text(message.sender.name[0]),
> backgroundColor: message.sender.color
> ),
> title: new Text(message.sender.name),
> subtitle: new Text(message.text)
> )
================= 4...5 =====================
6a7
> import 'package:firebase/firebase.dart';
30a32
> Firebase _firebase = new Firebase("https://firechat-flutter.firebaseio.com/");
33a36,59
> void initState() {
> _firebase.onChildAdded.listen((Event event) {
> setState(() {
> var val = event.snapshot.val();
> AnimationController animationController = new AnimationController(
> duration: new Duration(milliseconds: 300)
> );
> ChatUser sender = new ChatUser(
> name: val['sender']['name'],
> color: new Color(val['sender']['color'])
> );
> ChatMessage message = new ChatMessage(
> sender: sender,
> text: val['text'],
> animationController: animationController
> );
> _messages.add(message);
> animationController.forward();
> });
> });
> super.initState();
> }
>
> @override
45a72,76
> var message = {
> 'sender': { 'name': _name, 'color': _color.value },
> 'text': value.text,
> };
> _firebase.push().set(message);
48,58d78
< AnimationController animationController = new AnimationController(
< duration: new Duration(milliseconds: 300)
< );
< ChatUser sender = new ChatUser(name: _name, color: _color);
< ChatMessage message = new ChatMessage(
< sender: sender,
< text: value.text,
< animationController: animationController
< );
< _messages.add(message);
< animationController.forward();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment