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

378 行
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 UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
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>();
private List<ChatComponents.Message> init_messages = new List<ChatComponents.Message>();
public int _page = 0;
private bool isReached = false;
public override void initState()
{
base.initState();
_loadMessages();
}
private void _addMessage(int index, ChatComponents.Message message)
{
setState(() =>
{
_messages.Insert(index, 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(0, textMessage);
}
public List<ChatComponents.Message> InitMessages()
{
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
)
);
}
return results;
}
private Future _handleEndReached()
{
if (!isReached)
{
isReached = true;
if (init_messages.isEmpty())
{
init_messages = InitMessages();
}
return Future.delayed(TimeSpan.FromMilliseconds(1000), () =>
{
foreach (var message in init_messages)
{
_addMessage(_messages.Count, message);
}
_page = _page + 1;
isReached = false;
return Future.value();
});
}
else
{
return Future.value();
}
}
private void _loadMessages()
{
List<ChatComponents.Message> results = InitMessages();
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 Scaffold(
backgroundColor:Color.fromARGB(0,0,0,0),
appBar: new AppBar(
backgroundColor: Color.white,
centerTitle: false,
title: new IconButton(
icon: new Icon(Icons.arrow_back, color: Color.black),
onPressed: () =>
{
FocusScope.of(context).unfocus();
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
)
)
);
}
}
}