Guest User

Untitled

a guest
Jul 17th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_dialogflow/flutter_dialogflow.dart';
  3.  
  4. class TessChat extends StatefulWidget {
  5. TessChat({Key key, this.title}) : super(key: key);
  6.  
  7. final String title;
  8.  
  9. @override
  10. _MyHomePageState createState() => new _MyHomePageState();
  11. }
  12.  
  13. class _MyHomePageState extends State<TessChat> {
  14. final List<ChatMessage> _messages = <ChatMessage>[];
  15. final TextEditingController _textController = new TextEditingController();
  16.  
  17. Widget _buildTextComposer() {
  18. return new IconTheme(
  19. data: new IconThemeData(color: Theme.of(context).accentColor),
  20. child: new Container(
  21. margin: const EdgeInsets.symmetric(horizontal: 8.0),
  22. child: new Row(
  23. children: <Widget>[
  24. new Flexible(
  25. child: new TextField(
  26. controller: _textController,
  27. onSubmitted: _handleSubmitted,
  28. decoration:
  29. new InputDecoration.collapsed(hintText: "Send a message"),
  30. ),
  31. ),
  32. new Container(
  33. margin: new EdgeInsets.symmetric(horizontal: 4.0),
  34. child: new IconButton(
  35. icon: new Icon(Icons.send),
  36. onPressed: () => _handleSubmitted(_textController.text)),
  37. ),
  38. ],
  39. ),
  40. ),
  41. );
  42. }
  43.  
  44. void Response(query) async {
  45. _textController.clear();
  46. Dialogflow dialogflow =Dialogflow(token: 'b02b3962a90f49078242484edb1602d1');
  47. AIResponse response = await dialogflow.sendQuery(query);
  48. print(response.getMessageResponse());
  49. ChatMessage message = new ChatMessage(
  50. text: response.getMessageResponse(),
  51. name: "Tess",
  52. type: false,
  53. );
  54. setState(() {
  55. _messages.insert(0, message);
  56. });
  57. }
  58.  
  59. void _handleSubmitted(String text) {
  60. _textController.clear();
  61. ChatMessage message = new ChatMessage(
  62. text: text,
  63. name: "Frank T.H.N.",
  64. type: true,
  65. );
  66. setState(() {
  67. _messages.insert(0, message);
  68. });
  69. Response(text);
  70. }
  71.  
  72. @override
  73. Widget build(BuildContext context) {
  74. return new Scaffold(
  75. appBar: new AppBar(
  76. title: new Text('TessChat'),
  77. ),
  78. body: new Column(children: <Widget>[
  79. new Flexible(
  80. child: new ListView.builder(
  81. padding: new EdgeInsets.all(8.0),
  82. reverse: true,
  83. itemBuilder: (_, int index) => _messages[index],
  84. itemCount: _messages.length,
  85. )),
  86. new Divider(height: 1.0),
  87. new Container(
  88. decoration: new BoxDecoration(color: Theme.of(context).cardColor),
  89. child: _buildTextComposer(),
  90. ),
  91. ]),
  92. );
  93. }
  94. }
  95.  
  96. class ChatMessage extends StatelessWidget {
  97. ChatMessage({this.text, this.name, this.type});
  98.  
  99. final String text;
  100. final String name;
  101. final bool type;
  102.  
  103. List<Widget> otherMessage(context) {
  104. return <Widget>[
  105. new Container(
  106. margin: const EdgeInsets.only(right: 16.0),
  107. child: new CircleAvatar(child: new Icon(Icons.face, size: 42.0,)),
  108. ),
  109. new Expanded(
  110. child: new Column(
  111. crossAxisAlignment: CrossAxisAlignment.start,
  112. children: <Widget>[
  113. new Text(this.name, style:new TextStyle(fontWeight:FontWeight.bold )),
  114. new Container(
  115. margin: const EdgeInsets.only(top: 5.0),
  116. child: new Text(text),
  117. ),
  118. ],
  119. ),
  120. ),
  121. ];
  122. }
  123.  
  124. List<Widget> myMessage(context) {
  125. return <Widget>[
  126. new Expanded(
  127. child: new Column(
  128. crossAxisAlignment: CrossAxisAlignment.end,
  129. children: <Widget>[
  130. new Text(this.name, style: Theme.of(context).textTheme.subhead),
  131. new Container(
  132. margin: const EdgeInsets.only(top: 5.0),
  133. child: new Text(text),
  134. ),
  135. ],
  136. ),
  137. ),
  138. new Container(
  139. margin: const EdgeInsets.only(left: 16.0),
  140. child: new CircleAvatar(child: new Text(this.name[0])),
  141. ),
  142. ];
  143. }
  144.  
  145. @override
  146. Widget build(BuildContext context) {
  147. return new Container(
  148. margin: const EdgeInsets.symmetric(vertical: 10.0),
  149. child: new Row(
  150. crossAxisAlignment: CrossAxisAlignment.start,
  151. children: this.type ? myMessage(context) : otherMessage(context),
  152. ),
  153. );
  154. }
  155. }
Add Comment
Please, Sign In to add comment