flexible message
This commit is contained in:
@@ -23,7 +23,8 @@ class _PartScreenState extends State<PartScreen> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
final messageService = Provider.of<MessageService>(context, listen: false);
|
final messageService =
|
||||||
|
Provider.of<MessageService>(context, listen: false);
|
||||||
messageService.removeNonListeningMessages();
|
messageService.removeNonListeningMessages();
|
||||||
setState(() {
|
setState(() {
|
||||||
_isInitialized = true;
|
_isInitialized = true;
|
||||||
@@ -65,18 +66,9 @@ class _PartScreenState extends State<PartScreen> {
|
|||||||
if (!_isInitialized) {
|
if (!_isInitialized) {
|
||||||
return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
}
|
}
|
||||||
final double minHeight = constraints.maxHeight * 0.18;
|
final double minHeight = constraints.maxHeight * 0.16;
|
||||||
final double maxHeight = constraints.maxHeight * 0.4;
|
final double maxHeight = constraints.maxHeight * 0.4;
|
||||||
final double chatWidth = constraints.maxWidth * 0.94;
|
final double chatWidth = constraints.maxWidth * 0.94;
|
||||||
final int messageCount =
|
|
||||||
context.watch<MessageService>().messages.length;
|
|
||||||
double chatHeight;
|
|
||||||
if (messageCount <= 1) {
|
|
||||||
chatHeight = minHeight + 70;
|
|
||||||
} else {
|
|
||||||
final height = minHeight + (messageCount * 78);
|
|
||||||
chatHeight = height < maxHeight ? height : maxHeight;
|
|
||||||
}
|
|
||||||
return Stack(
|
return Stack(
|
||||||
children: [
|
children: [
|
||||||
Positioned.fill(
|
Positioned.fill(
|
||||||
@@ -103,19 +95,17 @@ class _PartScreenState extends State<PartScreen> {
|
|||||||
padding: EdgeInsets.only(bottom: constraints.maxHeight * 0.22),
|
padding: EdgeInsets.only(bottom: constraints.maxHeight * 0.22),
|
||||||
child: Align(
|
child: Align(
|
||||||
alignment: Alignment.bottomCenter,
|
alignment: Alignment.bottomCenter,
|
||||||
child: Selector<MessageService, int>(
|
child: Consumer<MessageService>(
|
||||||
selector: (_, service) => service.messages.length,
|
builder: (context, messageService, child) {
|
||||||
builder: (context, messageCount, child) {
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
_scrollToBottom();
|
_scrollToBottom();
|
||||||
});
|
});
|
||||||
return Consumer<MessageService>(
|
return Container(
|
||||||
builder: (context, messageService, child) {
|
|
||||||
return AnimatedContainer(
|
|
||||||
duration: const Duration(milliseconds: 180),
|
|
||||||
curve: Curves.easeInOut,
|
|
||||||
width: chatWidth,
|
width: chatWidth,
|
||||||
height: chatHeight,
|
constraints: BoxConstraints(
|
||||||
|
minHeight: minHeight,
|
||||||
|
maxHeight: maxHeight,
|
||||||
|
),
|
||||||
child: ClipRRect(
|
child: ClipRRect(
|
||||||
borderRadius: BorderRadius.circular(24),
|
borderRadius: BorderRadius.circular(24),
|
||||||
child: BackdropFilter(
|
child: BackdropFilter(
|
||||||
@@ -131,24 +121,17 @@ class _PartScreenState extends State<PartScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(
|
padding: const EdgeInsets.only(
|
||||||
top: 42,
|
top: 50,
|
||||||
left: 6,
|
left: 6,
|
||||||
right: 6,
|
right: 6,
|
||||||
bottom: 6),
|
bottom: 30),
|
||||||
child: Column(
|
child: LayoutBuilder(
|
||||||
children: [
|
builder: (context, boxConstraints) {
|
||||||
Expanded(
|
return ChatBox(
|
||||||
child: Padding(
|
scrollController: _scrollController,
|
||||||
padding: EdgeInsets.only(
|
|
||||||
top: 12, bottom: 12),
|
|
||||||
child: ChatBox(
|
|
||||||
scrollController:
|
|
||||||
_scrollController,
|
|
||||||
messages: messageService.messages,
|
messages: messageService.messages,
|
||||||
),
|
);
|
||||||
),
|
}
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Positioned(
|
Positioned(
|
||||||
@@ -171,8 +154,6 @@ class _PartScreenState extends State<PartScreen> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -13,11 +13,23 @@ class ChatBox extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ListView.builder(
|
return Container(
|
||||||
|
constraints: const BoxConstraints(
|
||||||
|
minHeight: 60,
|
||||||
|
),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
border: Border.all(
|
||||||
|
color: Colors.grey, // 边框颜色
|
||||||
|
width: 1.0, // 边框宽度
|
||||||
|
),
|
||||||
|
borderRadius: BorderRadius.circular(8.0), // 边框圆角
|
||||||
|
),
|
||||||
|
child: ListView.builder(
|
||||||
controller: scrollController,
|
controller: scrollController,
|
||||||
itemCount: messages.length,
|
itemCount: messages.length,
|
||||||
reverse: true,
|
reverse: true,
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 12),
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
||||||
|
shrinkWrap: true,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final message = messages[messages.length - 1 - index];
|
final message = messages[messages.length - 1 - index];
|
||||||
final isTop = index == messages.length - 1;
|
final isTop = index == messages.length - 1;
|
||||||
@@ -30,6 +42,7 @@ class ChatBox extends StatelessWidget {
|
|||||||
child: ChatBubble(message: message),
|
child: ChatBubble(message: message),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user