I am very new to flutter and i want a widget to display an image of LED when last element of the list contain 'LED:on'. The element is from arduino thru hc05.
List<String> led = ['LED:on', 'LED:off', 'LED:on'];
CodePudding user response:
Try with this
List<String> led = ['LED:on', 'LED:off', 'LED:on'];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
if (led.last.contains('LED:on'))
Image.network('https://eample.com.png')
],
));
}
CodePudding user response:
You can know if the last element of the array contains 'LED:on' string with
led.last.contains('LED:on'), //It returns bool.
Use the Visibility widget which controls whether the given child is visible or not based on the bool value passed on the visible parameter.
Here is the complete code.
Visibility(
visible: led.last.contains('LED:on'),
child: Image.network('https://yourimageurl.com'), // Use Image.asset() for local image
);
CodePudding user response:
You can do this way also.
List<String> led = ['LED:on', 'LED:off'];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
if (led.last.contains('LED:on')) ...
[Image.network('https://eample.com.png')],
if (led.last.contains('LED:off')) ...
[Image.network('https://eample2.com.png')],
],
));
}
