IOWebSocketChannel? ioChannel;
List<CustomRunners>? oldData = [];
List<CustomRunners>? newData = [];
void getSocket(){
ioChannel = IOWebSocketChannel.connect(
Uri.parse(
'wss://xxx.xyz/port=${widget.port}/?logged=true&source=0'),
headers: {
'Origin': 'https://abc.io',
},
);
ioChannel?.stream.listen((message) {
final data = jsonDecode(message);
if (data[0] != null) {
final match = SocketModel.fromJson(data[0]);
newData!.clear();
for (var item in match.runners!) {
var obj = CustomRunners(
marketId: match.marketId,
runnerName: item.runnerName,
selectionId: item.selectionId,
status: item.status,
exchangePrices: item.exchangePrices,
lastPriceTraded: item.lastPriceTraded,
totalMatched: item.totalMatched);
newData?.add(obj);
}
if (newData!.isNotEmpty) {
oldData!.clear();
setState(() {
oldData?.addAll(newData!);
});
}
}
});
}
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: newData.length,
itemBuilder: (context, index) {
var obj = newData[index];
var obj2 = oldData[index];
if (obj.exchangePrices!.availableToBack!.length == 0 ||
obj.exchangePrices!.availableToBack![0].size ==
null ||
obj2.exchangePrices!.availableToBack![0].size ==
null) {
back = Color(0xff72bbef);
} else {
var newAvailSize =
obj.exchangePrices!.availableToBack![0].size!;
var oldAvailSize =
obj2.exchangePrices!.availableToBack![0].size!;
if (newAvailSize > oldAvailSize) {
back = Color(0xff00FFFF);
} else if (newAvailSize < oldAvailSize) {
back = Colors.yellowAccent;
print(
"get yellowwwwww $newAvailSize and $oldAvailSize");
} else if (newAvailSize == oldAvailSize) {
back = Color(0xff72bbef);
}
}
}
return Row(
children: [
Expanded(
flex: 2,
child: AnimatedContainer(
duration: Duration(
milliseconds: 50), // animation duration
curve: Curves.ease, //
height: 50,
decoration: BoxDecoration(
color: back,
border: Border(
top: BorderSide(
width: 0.2, color: Colors.black),
bottom: BorderSide(
width: 0.2, color: Colors.black))),
child: Text(obj.exchangePrices!.availableToBack![0]
.price
.toString())
),
),
],
);
},
),
),
I have two array oldData and newData. This array getting data from web socket and these data refreshing continuously.I'm trying to change color of container if newData array is greater than oldData array then color will be blue and if newData array is less than oldData array then color will be red else color will be white.I have written the code but unable to get exact output.
This is a code which i have written to change the color of container but color is not changing if i'm clearing the oldData array.In both array getting same data after clearing oldData array.
Can someone help me with this.