Open aju-annaseem opened 1 year ago
my code as below: i want to show 1 to 34 numbers orderly.. pls help
import 'package:flutter/material.dart'; import 'package:grouped_list/grouped_list.dart'; import 'package:intl/intl.dart';
`void main() => runApp(const MyApp());
class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const TestChat()); } }
class TestChat extends StatelessWidget { const TestChat({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
List
itemBuilder: (context, Message element) => GroupedMessageBubble(
chatData: element,
),
useStickyGroupSeparators: true, // optional
floatingHeader: true, // optional
order: GroupedListOrder.DESC, // optional
),
);
} }
class Message { final String msg; final DateTime date; final bool isme; final String? chatImage;
Message( {required this.msg, required this.date, required this.isme, this.chatImage}); }
class GroupedMessageBubble extends StatelessWidget { final Message chatData; const GroupedMessageBubble({Key? key, required this.chatData}) : super(key: key);
@override Widget build(BuildContext context) { return Align( alignment: chatData.isme ? Alignment.centerRight : Alignment.centerLeft, child: Column( crossAxisAlignment: chatData.isme ? CrossAxisAlignment.end : CrossAxisAlignment.start, children: [ Container( constraints: BoxConstraints( maxWidth: MediaQuery.of(context).size.width * 0.7), // alignment: Alignment.centerRight, margin: EdgeInsets.only( left: chatData.isme ? 80 : 20, right: chatData.isme ? 20 : 80, top: 10), padding: const EdgeInsets.symmetric( horizontal: 5, vertical: 5, ), decoration: BoxDecoration( borderRadius: BorderRadius.circular(7), color: chatData.isme ? Colors.green : const Color(0xFFF2F2F2)), child: Padding( padding: const EdgeInsets.symmetric(horizontal: 5), child: Text( chatData.msg, style: chatData.isme ? const TextStyle( fontWeight: FontWeight.w400, fontSize: 14, color: Colors.white) : const TextStyle( color: Colors.grey, fontSize: 14, fontWeight: FontWeight.w400, ), ), )), Align( alignment: chatData.isme ? Alignment.centerRight : Alignment.centerLeft, child: Padding( padding: const EdgeInsets.only( left: 20, right: 20, top: 5, bottom: 10), child: Text( // DateFormat.Hm().format(DateTime.parse(chatData.date!)), '', style: TextStyle( color: Colors.grey[400], fontSize: 10, fontWeight: FontWeight.w500, ), ), ), ) ], ), ); } } `
Hello @aju-annaseem, can you edit your message to have a better version to reproduce your code, so we can support your issue easier? (e.g.: dartpad.dev)
Same issue
Same here, the order in which I have my sample data is not what it rendered in the list. Mine has just four elements. Any solution to this?
I fixed it by disabling the sort parameter which, for some reason, is enabled by default. Since my list had custom types in it at some point (33rd message as other people), the sort algorithm broke. I will investigate more later on to see if I can find why such behavior exists.
You can also fix this by specifying your own itemComparator
. Otherwise, if your list item type doesn't implement Comparable
, it claims to use the natural ordering of your source list, but actually what it does is calls sort
on the list and returns null from the comparator for all elements, which is bad because sort
isn't stable in the presence of elements that compare equal, which results in this weird behavior.
So, in rough order of preference, I would say the workarounds for this bug are:
itemComparator
,Comparable
and implement compareTo
,sort: false
.The code could be fixed by checking for this condition (no itemComparator
, item type is not Comparable
, and sort is true
) and acting as though sort is false
. I'd be happy to make a PR for this if someone can give me evidence that the package is still being maintained and my PR would be merged.
Hi @Caffeinix, I would appreciate if you can open a PR to fix this behavior. I am planning to release a new version shortly and this fix would be very helpful.
Hello, can @Dimibe please perform a PR to fix this, as it does not seem to be done by @Caffeinix. I need to use sort for scrollController, to be able to navigate in list.
33 elements in list working fine
if more than 33 elements shuffling