115 lines
3.6 KiB
Dart
115 lines
3.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
const String _name = "Ivan Murashov";
|
|
|
|
void main() {
|
|
runApp(new DemoApp());
|
|
}
|
|
|
|
class DemoApp extends StatelessWidget {
|
|
@override Widget build(BuildContext context) {
|
|
return new MaterialApp(title: "DemoApp", home: new ChatScreen());
|
|
}
|
|
}
|
|
|
|
class ChatScreen extends StatefulWidget {
|
|
@override State createState() => new ChatScreenState();
|
|
}
|
|
|
|
class ChatScreenState extends State<ChatScreen> {
|
|
|
|
static const platform = const MethodChannel('samples.flutter.io/battery');
|
|
final List<ChatMessage> _messages = <ChatMessage>[];
|
|
final TextEditingController _textController = new TextEditingController();
|
|
|
|
@override Widget build(BuildContext context) {
|
|
return new Scaffold(appBar: new AppBar(
|
|
title: new Text("DemoApp")), body: _buildScreen());
|
|
}
|
|
|
|
Widget _buildScreen() {
|
|
return new Column(children: <Widget>[
|
|
new Flexible(child: new ListView.builder(
|
|
padding: new EdgeInsets.all(8.0),
|
|
reverse: true,
|
|
itemBuilder: (_, int index) => _messages[index],
|
|
itemCount: _messages.length)),
|
|
new Divider(height: 1.0),
|
|
new Container(decoration: _getDecoratedScreenComposer(),
|
|
child: _buildStyledTextComposer())
|
|
]);
|
|
}
|
|
|
|
BoxDecoration _getDecoratedScreenComposer() {
|
|
return new BoxDecoration(color: Theme.of(context).cardColor);
|
|
}
|
|
|
|
Widget _buildStyledTextComposer() {
|
|
return new IconTheme(data: _getIconThemeData(), child: _buildTextComposer());
|
|
}
|
|
|
|
IconThemeData _getIconThemeData() {
|
|
return new IconThemeData(color: Theme.of(context).accentColor);
|
|
}
|
|
|
|
Widget _buildTextComposer() {
|
|
return new Container(margin: const EdgeInsets.symmetric(horizontal: 8.0),
|
|
child: _buildInputFieldRow());
|
|
}
|
|
|
|
Row _buildInputFieldRow() {
|
|
var textField = new InputDecoration.collapsed(hintText: "Send a message");
|
|
return new Row(children: <Widget>[
|
|
new Flexible(child: new TextField(controller: _textController,
|
|
onSubmitted: _handleSubmitted,
|
|
decoration: textField)),
|
|
new Container(margin: new EdgeInsets.symmetric(horizontal: 4.0),
|
|
child: new IconButton(icon: new Icon(Icons.send),
|
|
onPressed: () => startScanner()))
|
|
// onPressed: () => _handleSubmitted(_textController.text)))
|
|
]);
|
|
}
|
|
|
|
Future<Null> startScanner() async {
|
|
try {
|
|
platform.invokeMethod('startScanner');
|
|
} on PlatformException catch (e) {
|
|
|
|
}
|
|
}
|
|
|
|
void _handleSubmitted(String text) {
|
|
_textController.clear();
|
|
ChatMessage message = new ChatMessage(text);
|
|
setState(() {_messages.insert(0, message);});
|
|
}
|
|
}
|
|
|
|
class ChatMessage extends StatelessWidget {
|
|
|
|
final String text;
|
|
|
|
ChatMessage(this.text);
|
|
|
|
@override Widget build(BuildContext context) {
|
|
return new Container(margin: const EdgeInsets.symmetric(vertical: 8.0),
|
|
child: _getMessageRow(context));
|
|
}
|
|
|
|
Widget _getMessageRow(BuildContext context) {
|
|
return new Row(crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
new Container(margin: const EdgeInsets.only(right: 16.0),
|
|
child: new CircleAvatar(child: new Text(_name[0]))),
|
|
new Column(crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
new Text(_name, style: Theme.of(context).textTheme.subhead),
|
|
new Container(margin: const EdgeInsets.only(top: 4.0),
|
|
child: new Text(text))
|
|
])
|
|
]);
|
|
}
|
|
} |