您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

367 行
12 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using ChatComponents;
using uiwidgets;
using Unity.UIWidgets.async;
using Unity.UIWidgets.cupertino;
using Unity.UIWidgets.engine;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.widgets;
using UnityEditor.Build.Content;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
using Random = System.Random;
using TextStyle = Unity.UIWidgets.ui.TextStyle;
namespace UIWidgetsSample
{
public class ChatRoomDemo : UIWidgetsPanel
{
protected void OnEnable()
{
base.OnEnable();
}
protected override void main()
{
ui_.runApp(new MyApp());
}
class MyApp : StatelessWidget
{
public override Widget build(BuildContext context)
{
return new CupertinoApp(
home: new DateAndTimePickerWidget()
);
}
}
}
[System.Serializable]
public class ChatMessages
{
public List<ChatMessage> chatMessages;
}
[System.Serializable]
public class ChatMessage
{
public ChatUser author;
public int createdAt;
public string id;
public string status;
public string text;
public string type;
}
[System.Serializable]
public class ChatUser
{
public string firstName;
public string id;
public string imageUrl;
}
public class ChatPage : StatefulWidget
{
public ChatPage(Key key = null) : base(key)
{
}
public override State createState()
{
return new _ChatPageState();
}
}
public class _ChatPageState : State<ChatPage>
{
public readonly ChatComponents.User _user = new ChatComponents.User("06c33e8b-e835-4736-80f4-63f44b66666c");
private List<ChatComponents.Message> _messages = new List<ChatComponents.Message>();
public int _page = 0;
public override void initState()
{
base.initState();
_loadMessages();
}
private void _addMessage(ChatComponents.Message message)
{
setState(() =>
{
_messages.Insert(0, message);
});
}
private void _handleAtachmentPressed()
{
material_.showModalBottomSheet<object>(
context,
context =>
{
return new SizedBox(
height: 144,
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: new List<Widget>
{
new CupertinoButton(
onPressed: () =>
{
Navigator.pop(context);
//_handleImageSelection();
},
child: new Align(
alignment: Alignment.centerLeft,
child: new Text("Photo")
)
),
new CupertinoButton(
onPressed: () =>
{
Navigator.pop(context);
//_handleFileSelection();
},
child:
new Align(
alignment: Alignment.centerLeft,
child: new Text("File")
)
),
new CupertinoButton(
onPressed: () => Navigator.pop(context),
child: new Align(
alignment: Alignment.centerLeft,
child: new Text("Cancel")
)
)
}
)
);
}
);
}
/* private void _handleFileSelection()
{
var result = FilePicker.platform.pickFiles(
type: FileType.any
);
if (result != null)
{
var message = new ChatComponents.FileMessage(
_user,
createdAt: DateTime.Now.Millisecond,
name: result.files.single.name,
id: Uuid().v4(),
mimeType:
lookupMimeType(result.files.single.path ?? ""),
size:
result.files.single.size,
uri:
result.files.single.path ?? ""
);
_addMessage(message);
}
}*/
/* private void _handleImageSelection()
{
var result = new ImagePicker().getImage(
imageQuality: 70,
maxWidth: 1440,
source: ImageSource.gallery
);
if (result != null)
{
var bytes = result.readAsBytes();
var image = decodeImageFromList(bytes);
var name = result.path.split("/").last;
var message = new ChatComponents.ImageMessage(
_user,
createdAt: DateTime.Now.Millisecond,
height: image.height.toDouble(),
id: Uuid().v4(),
name:
name,
size:
bytes.length,
uri:
result.path,
width:
image.width.toDouble()
);
_addMessage(message);
}
}*/
private void _handleMessageTap(ChatComponents.Message message)
{
if (message is ChatComponents.FileMessage)
//OpenFile.open(message.uri);
Debug.Log("OPEN FILE");
}
private void _handlePreviewDataFetched(
ChatComponents.TextMessage message,
ChatComponents.PreviewData previewData
)
{
var index = 0;
foreach (var element in _messages)
{
if (element.id == message.id)
{
index = _messages.IndexOf(element);
}
}
var updatedMessage = _messages[index].copyWith(previewData: previewData);
WidgetsBinding.instance?.addPostFrameCallback(_ =>
{
setState(() => { _messages[index] = updatedMessage; });
});
}
private void _handleSendPressed(ChatComponents.PartialText message)
{
var textMessage = new ChatComponents.TextMessage(
_user,
createdAt: DateTime.Now.Millisecond,
id: "b4878b96-efbc-479a-8291-474ef323aa" + _messages.Count.ToString(),
text: message.text
);
_addMessage(textMessage);
}
private Future _handleEndReached()
{
List<ChatComponents.TextMessage> messages = new List<ChatComponents.TextMessage>();
List<string> ids = new List<string>() { };
for (int i = 1; i < 10; i++)
{
ids.Add(i.ToString());
}
foreach (var id in ids)
{
messages.Add(new ChatComponents.TextMessage(
_user,
id: "b4878b96-efbc-479a-8291-474ef323a"+ _page + id,
text: id + "......"
) );
}
setState(()=> {
{
_messages.AddRange(messages);
_page = _page + 1;
}
});
return Future.value();
}
private void _loadMessages()
{
List<ChatComponents.Message> results = new List<ChatComponents.Message>();
TextAsset info = Resources.Load<TextAsset>("assets/messages");
List<ChatMessage> chatMessages = new List<ChatMessage>();
List<string> jsoninfo = info.text.Split('&').ToList();
foreach (var _info in jsoninfo)
{
var _message = JsonUtility.FromJson<ChatMessage>(_info);
chatMessages.Add(_message);
}
foreach (var _message in chatMessages)
{
results.Add( new ChatComponents.TextMessage(
author: new User(
id: _message.author.id,
firstName: _message.author.firstName,
imageUrl: _message.author.imageUrl
),
createdAt: _message.createdAt,
id: _message.id,
status: ChatRoomUtils.getStatusFromString(_message.status),
text: _message.text
)
);
}
setState(() => { _messages = results; });
}
public override Widget build(BuildContext context)
{
// return new Container(
// color: Color.fromARGB(0, 0, 0, 0),
// child: new Chat(
// messages: _messages,
// onAttachmentPressed: _handleAtachmentPressed,
// onMessageTap: _handleMessageTap,
// onPreviewDataFetched: (
// previewData,
// message) =>
// {
// _handlePreviewDataFetched(message, previewData);
// },
// onTextChanged: (_str) => { },
// onSendPressed: _handleSendPressed,
// onEndReached: _handleEndReached,
// //onEndReachedThreshold: 0.65f,
// user: _user
// )
// );
return new MaterialApp(
home: new Scaffold(
backgroundColor:Color.fromARGB(0,0,0,0),
appBar: new AppBar(
backgroundColor: Color.white,
title: new IconButton(
icon: new Icon(Icons.arrow_back, color: Color.black),
onPressed: () => { ChatPanelManager.Switch(); },
padding: EdgeInsets.zero
)
),
body: new Container(
child: new Chat(
messages: _messages,
onAttachmentPressed: _handleAtachmentPressed,
onMessageTap: _handleMessageTap,
onPreviewDataFetched: (
previewData,
message) =>
{
_handlePreviewDataFetched(message, previewData);
},
onTextChanged: (_str) => { },
onSendPressed: _handleSendPressed,
onEndReached: _handleEndReached,
//onEndReachedThreshold: 0.65f,
user: _user
)
)
)
);
}
}
}