How to change color dynamically in Flutter? - dart

I would like to dynamically change color to some elements. Is there a way to get colors using a variable? Something like
Colors[myvar] where myvar = "green"

What you can do is have a list of colors that you want like this -
static const List<_MyColor> myBgColors = const <_MyColor>[
const _MyColor(null, 'Clear'),
const _MyColor(const Color(0xFFFFC100), 'Orange'),
const _MyColor(const Color(0xFF91FAFF), 'Light Blue'),
const _MyColor(const Color(0xFF00D1FF), 'Cyan'),
const _MyColor(const Color(0xFF00BCFF), 'Cerulean'),
const _MyColor(const Color(0xFF009BEE), 'Blue'),
];
Here _MyColor is a class like -
class _MyColor {
const _MyColor(this.color, this.name);
final Color color;
final String name;
}
You can then use this list to access the colors in view you want.
Hope this helps.

1. Using a Function
You could use functions with a Color parameter, i.e., the type of the input parameter would be Color. For example, in a simple widget:
Container buildContainer({Color color}) {
return Container(
color: color,
);
}
In this block, we specified the return type as being the widget Container class. And we've also specified a named parameter called color, which takes a Color type. Later, if we wish to call this function we would do it like this (you might need to replace the ; for a ,):
buildContainer(color: Colors.red);
2. final vs const
I still think the question has insufficient description, but, if this answer above didn't solve it, I suspect this other StackOverflow question might be of help.
As for the keyword final, it is used when you want to declare constants inside (custom) widgets. That's because widgets will need constants defined at runtime, not at compile-time, like const. Otherwise, whenever a widget is destroyed and rebuilt, it wouldn't know how to properly do it if the constant was related to something that happened during runtime. As per the docs:
If you never intend to change a variable, use final or const, either instead of var or in addition to a type. A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.) A final top-level or class variable is initialized the first time it’s used.
3. Using a Class
For example, another way of doing the same thing I mentioned above, but now creating a new class for a new widget, instead of returning modifications on a widget with a function is:
class CustomContainer extends StatelessWidget {
final Color customColor;
CustomContainer({this.customColor});
#override
Widget build(BuildContext context) {
return Center(
child: Container(
height: 10.0,
width: 10.0,
color: customColor,
),
);
}
}

using final works for me.
final text_color = Colors.blue;
below is an example of how i use it in my class
class ColorTest extends StatefulWidget {
ColorTest();
#override
_ColorTest createState() => _ColorTest();
}
class _ColorTest extends State<ColorTest> {
#override
Widget build(
BuildContext context,
) {
final my_color_variable = Colors.red; // my_color_variable is now the color red
return new Container(
width: 100,
height: 100,
color: my_color_variable, // its red
);
}
}

I have found another solution.
In my app,
1. I want the color from the number A to change when I change this value with a slider.
2. But when I use the slider for the number B I want the color of B the change and that the number A is normal.
What have I done:
I have made constants for my text style:
const numbers = TextStyle(
fontFamily: 'B612 Mono',
fontSize: 18.0,
color: white,
);
const changeValueAB = TextStyle(
fontFamily: 'B612 Mono',
fontSize: 18.0,
color: cRelayMiddle,
backgroundColor: white,
);
I have made two booleans:
bool aActive = false;
bool bActive = false;
Then I made two functions:
void changeColorA() {
aActive = true;
bActive = false;
}
void changeColorB() {
aActive = false;
bActive = true;
}
Then when I change the values (I take now only as example value A.
B you just have to place the right void in it:
onPressed: () {
setState(() {
// your commands AND the void the change the booleans:
changeColorA();
});
},
Where the textcolor has to change I use:
Text(
'$inputNumberA',
style: aActive ? changeValueAB : numbers,),
It worked for me.

100% working and easy method
just create a variable
final Color myColor;
create a constructor to pass the color dynamically
required this.myColor
print the color
return Container(
color: myColor,
);
Pass the color as like - myColor: Colors.red

Start with 2 bracket pairs...
color: () (),
Then add anonymous function: (){} to the first brackets ...
color: ( (){} ) (),
Finally add code to it...
color: ( (){ if (true) return Colors.green;} ) (),
Done

Related

dart - does `this` depend on the point of invocation

class mButton extends StatefulWidget{
final VoidCallback onPressed;
final Widget child;
const mButton({Key key, this.onPressed, this.child}):super(key: key);
#override
_mButtonState createState()=>_mState();
}
class _mButtonState extends State<mButton>{
#override
Widget build(BuildContext context){
return Container(
child: RaisedButton(
color: _getColors(), #notice this line
child: widget.child,
onPressed: widget.onPressed,
)
);
}
Color _getColors(){
return _buttonColors.putIfAbsent(this, ()=> colors[next(0,5)]); #notice `this` in here
}
Map<_mButtonState, Color> _buttonColors = {};
final _random = Random();
int next(int min, int max) => min+_random.nextInt(max-min);
List<Color> colors = [
Colors.blue,
Colors.green,
Colors.orange,
Colors.purple,
Colors.amber,
Colors.lightBlue
];
}
In the above code, notice the two lines marked with #notice. The this keyword is supposed to refer to the current class instance, but from the above code, it almost makes the impression that the _getColors() method is trying to make this refer to every new instance of RaisedButton instantiated.
I'm a bit confused, does this refer to the instance of _mButtonState or
every new instance of RaisedButton instantiated(this would be done by instantiating mButton stateful widget) ?
it almost makes the impression that the _getColors() method is trying to make this refer to every new instance of RaisedButton instantiated
This wouldn't actually be possible here, because the instance of RaisedButton cannot be created until after _getColors() has been called (since the return value of _getColors() is passed into RaiseButton's constructor).
this will be _mButtonState. Although this can be a bit confusing in JavaScript, in Dart you can always tell what this will be from looking at the definition of the method, without concern for how/where it's called.

Problem with flutter Bloc rebuild/listening/re-drawing "trigger"

Im trying to change a value using the state is XX techniche, though flutter doesnt listen to stream or maybe no redrawing is triggerd
class AnimatedBox extends StatelessWidget {
#override
Widget build(BuildContext context) {
final _animatedBoxBloc = BlocProvider.of<AnimatedboxBloc>(context);
return BlocBuilder(
bloc: _animatedBoxBloc,
builder: (context, state) {
return AnimatedContainer(
duration: Duration(milliseconds: 500),
curve: Curves.easeIn,
width: 200.0,
height: 200.0,
color:
state is YellowAnimatedboxState ? Colors.yellow :
state is BlueAnimatedboxState ? Colors.blue :
Colors.transparent,
);
}
);
}
}
i know that using that method can be applied but when it's applied at the return of the builder i.e Blocbuilder .. if (state is A) return , if (state is B) return ; and then the builder return and entirly new build.
but, i only need to change a VALUE and i want to change it via Bloc pattern.
is it even possible? maybe im doing it wrong or the changed value need to be in the state that changes.
by that i mean write the bloc with these properties:
... extends

How to perserve toggle switch state when changing tabs for flutter?

I'm building three pages of alarms (stored in different lists) that I have displayed via a Scaffold and TabViewer. Each alarm is stored as a row with a toggle switch to enable it. The rows for each page are stored as a List. Despite making sure to use set state when changing values and even trying to assign unique keys nothing I do seems to preserve the state of the switches when I changed tabs.
This is my first time coding in Flutter/Dart or designing an app for mobile in general. As such I'm still learning about some basic features of this language.
I've tried adding keys to everything using Uniquekey() to generate up keys didn't work so I've removed them.
I've made sure all variable changes are inside set state functions.
I've tried to store the variable inside the immutable super class of AlarmToggle which is both ill-advised and doesn't work anyways.
I haven't tired using PageStorageKey as I'm not sure how they'd be implemented in my code but I feel this is likely the only solution.
class Alarms {
List<Widget> allAlarms = []; // Store all alarms for the object
buildAlarm(
GlobalKey<ScaffoldState>
pageKey,
[int hour,
int minute,
List<bool> alarmDaysOfWeek]) {
TimeOfDay alarmTime = TimeOfDay(hour: hour, minute: minute);
AlarmRow _newAlarm = new AlarmRow(UniqueKey(), alarmTime, alarmDaysOfWeek);
allAlarms.add(_newAlarm);
}
void removeAlarm(GlobalKey<ScaffoldState> pageKey) {allAlarms.removeLast();}}
class AlarmRow extends StatefulWidget {
final TimeOfDay _alarmTime;
final List<bool> _alarmDaysofWeek;
final UniqueKey key;
AlarmRow(this.key, this._alarmTime, this._alarmDaysofWeek);
AlarmRowState createState() => new AlarmRowState();
}
class AlarmRowState extends State<AlarmRow> {
bool _alarmIsActive;
AlarmRowState(){_alarmIsActive = _alarmIsActive ?? false;}
void toggleChanged(bool state) {this.setState(() {_alarmIsActive = state;});}
#override
Widget build(BuildContext context) {
return new Container(
child: Row(
children: <Widget>[
new AlarmIcon(_alarmIsActive),
new Column(
children: <Widget>[
new AlarmTime(widget._alarmTime),
new AlarmWeekly(widget._alarmDaysofWeek),
],
),
new AlarmToggle(
_alarmIsActive,
() => toggleChanged(!_alarmIsActive),
),
],
),
);
} // Build
} // Class
No matter what I seem to try the _alarmIsActive variable in AlarmRow() gets reset to null each time the tab is changed. I'm trying to preserve its state when changing pages.
The solution is as jdv stated to use AutomaticKeepAliveClientMixin, it's not hard to use but I figured I'd include the instruction that I found after searching here in case anyone searches and finds this and doesn't know automatically how to implement it like myself.
class AlarmRowState extends State<AlarmRow> with AutomaticKeepAliveClientMixin {
#override bool get wantKeepAlive => true;
It's implemented in the state class with any modifiable variables you want to preserve. The 'with' after the 'extends' adds a Mixin which is a sort of class inheritance. Finally, it requires you to set the 'wantKeepAlive' to true and it compiles and state is no longer lost while the widget is not being rendered.
Why a stateful widget loses state while it's not rendered is something I'm still searching for. But at least I have a solution.
I hope this will help you #Ender ! check for this code, you can create a Global shared preference and use it, as shown below:-
class AlarmRow extends StatefulWidget {
#override
State<StatefulWidget> createState() => new _AlarmRowState();
}
class _AlarmRowState extends State<AlarmRow>{
bool alarmIsActive;
#override
void initState() {
alarmIsActive = Global.shared.alarmIsActive;
super.initState();
}
#override
Widget build(BuildContext context) {
....
.....
....
....
body: new Container(
child: Switch(
value: alarmIsActive,
onChanged: (bool isEnabled) {
setState(() {
alarmIsActive = isEnabled;
Global.shared.alarmIsActive = isEnabled;
isEnabled =!isEnabled;
});
},
.....
......
),
),
);
}
}
class Global{
static final shared =Global();
bool alarmIsActive = false;
}
Switch enabled and will maintain its state

How can I customize / rotate a BoxDecoration for a Container widget in Flutter?

I have a Widget that builds a circular profile picture for bus stops, and as of right now it has a circular border surrounding the profile picture like such. I want to change the circular border to be dashed instead and to also animate by circling/rotating around the profile picture in its dashed form. Is there any easy way to do that? Thanks so much for any help you can provide!
return new Transform(
transform: new Matrix4.diagonal3Values(
animation.avatarSize.value,
animation.avatarSize.value,
1.0,
),
alignment: Alignment.center,
child: new Container(
width: 110.0,
height: 110.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
border: new Border.all(
color: Colors.white30,
),
),
margin: const EdgeInsets.only(
top: 24.0,
left: 16.0,
),
padding: const EdgeInsets.all(3.0),
child: new ClipOval(
child: new Image.asset(
stopName.avatar,
),
),
),
);
Unfortunately the simple answer is that there is no easy way to do that. The flutter people in their infinite wisdom have made the conclusion that dashed lines are not performant enough to be included in flutter and therefore no-one will ever need to draw a dashed line. (Yes, the logical discontinuity in that sentence is intended. Don't get me wrong - I love flutter and the devs have done a great job, but they do seem to have made a few semi-arbitrary decisions based on performance rather than functionality).
The interpretation I've seen of why is that basically the C++ version would be doing something similar to dart code (unless it were done directly on the GPU), and so they expect someone to eventually implement dashed lines in dart instead (possibly as part of a library?). See this github bug for progress and discussion... and +1 it if you want to see dashes in future. If enough people do that then eventually the flutter people may decide to implement it.
But for now, that means that there is no easy way to make CircleBorder, or any other type of border, with a dashed line.
However, with maximum effort, exactly what you want can be achieved =). Below is a quick cut of the code for you that does what you want. Be warned - it isn't very optimized, and there are probably easier ways to do it, and you could probably use a Decoration and implement the paint etc there or something... but this does work.
import 'dart:math';
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: new SafeArea(
child: Column(
children: [
new DashedCircle(
child: new ClippedDrawing(),
)
],
),
),
);
}
}
class ClippedDrawing extends StatelessWidget {
#override
Widget build(BuildContext context) => new ClipOval(
child: new Container(
color: Colors.red,
),
);
}
class DashedCircle extends StatefulWidget {
final Widget child;
const DashedCircle({Key key, this.child}) : super(key: key);
#override
DashedBorderState createState() => new DashedBorderState();
}
class DashedBorderState extends State<DashedCircle> with TickerProviderStateMixin<DashedCircle> {
AnimationController controller;
Animation<double> animation;
#override
void initState() {
super.initState();
controller = new AnimationController(vsync: this, duration: Duration(seconds: 10));
animation = new Tween(begin: 0.0, end: pi * 2.0).animate(controller);
controller.repeat();
}
#override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animation,
builder: (context, child) {
return new CustomPaint(
painter: OvalPainter(
color: Colors.blue, borderWidth: 1.0, dashLength: 5.0, spaceLength: 2.0, offset: animation.value),
child: child,
);
},
child: Container(
width: 110.0,
height: 110.0,
padding: EdgeInsets.all(3.0),
child: widget.child,
),
);
}
}
class OvalPainter extends CustomPainter {
final Color color;
final double borderWidth;
final double dashLength;
final double spaceLength;
final double offset;
OvalPainter(
{#required this.borderWidth,
#required this.dashLength,
#required this.spaceLength,
#required this.offset,
#required this.color});
double lastShortestSide;
double lastDashLength;
double lastSpaceLength;
Path lastPath;
#override
void paint(Canvas canvas, Size size) {
Rect rect = Offset.zero & size;
var radius = rect.shortestSide / 2;
canvas.translate(radius, radius);
canvas.rotate(offset);
Path path;
if (lastShortestSide == rect.shortestSide &&
dashLength == lastDashLength &&
spaceLength == lastSpaceLength &&
lastPath != null) {
path = lastPath;
} else {
path = _getDashedCircularPath(rect.shortestSide / 2, dashLength, spaceLength);
lastPath = path;
lastShortestSide = rect.shortestSide;
lastDashLength = dashLength;
lastSpaceLength = spaceLength;
}
canvas.drawPath(
path,
new Paint()
..style = PaintingStyle.stroke
..color = color
..strokeWidth = borderWidth,
);
}
#override
bool shouldRepaint(OvalPainter oldDelegate) {
return offset != oldDelegate.offset ||
color != oldDelegate.color ||
borderWidth != oldDelegate.borderWidth ||
dashLength != oldDelegate.dashLength ||
spaceLength != oldDelegate.spaceLength;
}
static Path _getDashedCircularPathFromNumber(double radius, int numSections, double dashPercentage) {
var tau = 2 * pi;
var actualTotalLength = tau / numSections;
var actualDashLength = actualTotalLength * dashPercentage;
double offset = 0.0;
Rect rect = new Rect.fromCircle(center: Offset.zero, radius: radius);
Path path = new Path();
for (int i = 0; i < numSections; ++i) {
path.arcTo(rect, offset, actualDashLength, true);
offset += actualTotalLength;
}
return path;
}
static Path _getDashedCircularPath(double radius, double dashLength, double spaceLength) {
// first, find number of radians that dashlength + spacelength take
var tau = 2 * pi;
var circumference = radius * tau;
var dashSpaceLength = dashLength + spaceLength;
var num = circumference / (dashSpaceLength);
// we'll floor the value so that it's close-ish to the same amount as requested but
// instead will be the same all around
var closeNum = num.floor();
return _getDashedCircularPathFromNumber(radius, closeNum, dashLength / dashSpaceLength);
}
}

How to rebuild widget in Flutter when a change occurs

Edit: I've edited the code below to feature the method that fetches the data along with the widgets that build the train estimates (replacing any API information along the way with "API_URL" and "API_STOP_ID"). I hope this even better helps us figure out the problem! I really appreciate any information anyone can give -- I've been working very hard on this project! Thank you all again!
Original post:
I have a ListView of ListTiles that each have a trailing widget which builds train arrival estimates in a new Text widget. These trailing widgets are updated every five seconds (proven by print statements). As a filler for when the app is fetching data from the train's API, it displays a "no data" Text widget which is built by _buildEstimatesNull().
However, the problem is that "no data" is still being shown even when the app has finished fetching data and _isLoading = false (proven by print statements). Still, even if that was solved, the train estimates would become quickly outdated, as the trailing widgets are updating every five seconds on their own but this would not be reflected in the actual app as the widgets were built on page load. Thus, I need a way to rebuild those trailing widgets whenever they fetch new information.
Is there a way to have Flutter automatically rebuild the ListTile's trailing widget every five seconds as well (or whenever _buildEstimatesS1 is updated / the internals of the trailing widget is updated)?
class ShuttleApp extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return new ShuttleState();
}
}
class ShuttleState extends State<ShuttleApp> {
#override
Widget build(BuildContext context) {
return new MaterialApp(
home: new HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return new HomeState();
}
}
class HomeState extends State<HomeScreen> {
var _isLoading = true;
void initState() {
super.initState();
_fetchData();
const fiveSec = const Duration(seconds: 5);
new Timer.periodic(fiveSec, (Timer t) {
_fetchData();
});
}
var arrivalsList = new List<ArrivalEstimates>();
_fetchData() async {
arrivalsList.clear();
stopsList.clear();
final url = "API_URL";
print("Fetching: " + url);
final response = await http.get(url);
final busesJson = json.decode(response.body);
if (busesJson["service_id"] == null) {
globals.serviceActive = false;
} else {
busesJson["ResultSet"]["Result"].forEach((busJson) {
if (busJson["arrival_estimates"] != null) {
busJson["arrival_estimates"].forEach((arrivalJson) {
globals.serviceActive = true;
final arrivalEstimate = new ArrivalEstimates(
arrivalJson["route_id"], arrivalJson["arrival_at"], arrivalJson["stop_id"]
);
arrivalsList.add(arrivalEstimate);
});
}
});
}
setState(() {
_isLoading = false;
});
}
Widget _buildEstimateNull() {
return new Container(
child: new Center(
child: new Text("..."),
),
);
}
Widget _buildEstimateS1() {
if (globals.serviceActive == false) {
print('serviceNotActive');
_buildEstimateNull();
} else {
final String translocStopId = "API_STOP_ID";
final estimateMatches = new List<String>();
arrivalsList.forEach((arrival) {
if (arrival.stopId == translocStopId) {
estimateMatches.add(arrival.arrivalAt);
}
});
estimateMatches.sort();
if (estimateMatches.length == 0) {
print("zero");
return _buildEstimateNull();
} else {
return new Container(
child: new Center(
child: new Text(estimateMatches[0]),
),
);
}
}
}
#override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: const Color(0xFF171717),
appBar: new AppBar(),
body: new DefaultTextStyle(
style: new TextStyle(color: const Color(0xFFaaaaaa),),
child: new ListView(
children: <Widget>[
new ListTile(
title: new Text('S1: Forest Hills',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: new Text('Orange Line'),
contentPadding: new EdgeInsets.symmetric(vertical: 16.0, horizontal: 16.0),
trailing: _isLoading ? _buildEstimateNull() : _buildEstimateS1(),
),
],
),
)
);
}
class ArrivalEstimates {
final String routeId;
final String arrivalAt;
final String stopId;
ArrivalEstimates(this.routeId, this.arrivalAt, this.stopId);
}
Thank you so much in advance for any help you can give! I really super appreciate it! :)
There are a few ways you could tackle this. It is slightly difficult however to tell what's going on without seeing a bit more of your code - specifically how you're getting the data and what you're doing with it. But I think I can give you a sufficient answer anyways.
The simple way of doing this is to either:
Have a StatefulWidget which keeps track of the build estimates for all of the items in the list. It should request data from your API, get the results, and then call setState(() => this.listData = data);. The call to setState is what tells the widget that it needs to rebuild.
Have a StatefulWidget for each item in the list. They would all each perform an API request every 5 seconds, get the results, and then each would call setState(() => this.itemData = data);. This means multiple calls to the API etc.
The advantage of #1 is that you can batch API calls, whereas the advantage to #2 is that your build would change less overall (although the way flutter works, this would be pretty minimal)... so I would probably go with #1 if possible.
However, there is a better way of doing this!
The better way of doing this is to have some sort of API Manager (or whatever you want to call it) which handles the communication with your API. It probably would live higher up in your widget tree and would be started/stopped with whatever logic you want. Depending on how far up the widget tree is, you could either pass it into each child or more likely hold it in an InheritedWidget which could then be used to retrieve it from each list element or from the overall list.
The API manager would provide various streams - either with a bunch of named fields/methods or with a getStream(id) sort of structure depending on your API.
Then, within your various list elements, you would use StreamBuilder widgets to build each of the elements based on the data - by using a StreamBuilder you get a ConnectionState object that lets you know whether the stream has received any data yet so you can choose to show an isLoading type widget instead of the one that shows data.
By using this more advanced method, you get:
Maintainability
If your API changes, you only have to change the API manager
You can write better testing as the API interactions and the UI interactions are separated
Extensibility
If you, later on, use push notifications for updates rather than pinging a server every 5 seconds, that can be incorporated into the API manager so that it can simply update the stream without touching the UI
EDIT: as per OP's comments, they have already implemented more or less the first suggestion. However, there are a few problems with the code. I'll list them below and I've posted the code with a couple of changes.
The arrivalsList should be replaced each time a new build is done rather than simply being changed. This is because dart compares the lists and if it finds the same list, it doesn't necessarily compare all of the elements. Also, while changing it in the middle of a function isn't necessarily going to cause problems, it's generally better to use a local variable and then change the value at the end. Note that the member is actually set within setState.
If serviceActive == false, the return was missed from return _buildEstimateNull();.
Here's the code:
class HomeState extends State<HomeScreen> {
var _isLoading = true;
void initState() {
super.initState();
_fetchData();
const fiveSec = const Duration(seconds: 5);
new Timer.periodic(fiveSec, (Timer t) {
_fetchData();
});
}
var arrivalsList = new List<ArrivalEstimates>();
_fetchData() async {
var arrivalsList = new List<ArrivalEstimates>(); // *********** #1
stopsList.clear();
final url = "API_URL";
print("Fetching: " + url);
final response = await http.get(url);
final busesJson = json.decode(response.body);
if (busesJson["service_id"] == null) {
print("no service id");
globals.serviceActive = false;
} else {
busesJson["ResultSet"]["Result"].forEach((busJson) {
if (busJson["arrival_estimates"] != null) {
busJson["arrival_estimates"].forEach((arrivalJson) {
globals.serviceActive = true;
final arrivalEstimate = new ArrivalEstimates(
arrivalJson["route_id"], arrivalJson["arrival_at"], arrivalJson["stop_id"]
);
arrivalsList.add(arrivalEstimate);
});
}
});
}
setState(() {
_isLoading = false;
this.arrivalsList = arrivalsList; // *********** #1
});
}
Widget _buildEstimateNull() {
return new Container(
child: new Center(
child: new Text("..."),
),
);
}
Widget _buildEstimateS1() {
if (globals.serviceActive == false) {
print('serviceNotActive');
return _buildEstimateNull(); // ************ #2
} else {
final String translocStopId = "API_STOP_ID";
final estimateMatches = new List<String>();
print("arrivalsList length: ${arrivalsList.length}");
arrivalsList.forEach((arrival) {
if (arrival.stopId == translocStopId) {
print("Estimate match found: ${arrival.stopId}");
estimateMatches.add(arrival.arrivalAt);
}
});
estimateMatches.sort();
if (estimateMatches.length == 0) {
print("zero");
return _buildEstimateNull();
} else {
return new Container(
child: new Center(
child: new Text(estimateMatches[0]),
),
);
}
}
}
#override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: const Color(0xFF171717),
appBar: new AppBar(),
body: new DefaultTextStyle(
style: new TextStyle(color: const Color(0xFFaaaaaa),),
child: new ListView(
children: <Widget>[
new ListTile(
title: new Text('S1: Forest Hills',
style: new TextStyle(fontWeight: FontWeight.w500, fontSize: 20.0)),
subtitle: new Text('Orange Line'),
contentPadding: new EdgeInsets.symmetric(vertical: 16.0, horizontal: 16.0),
trailing: _isLoading ? _buildEstimateNull() : _buildEstimateS1(),
),
],
),
)
);
}
Instead of clearing and re-using the arrivalsList, create a new list every time the data is fetched. Otherwise Flutter is unable to detect if the list has changed.
Also, the code would clearer if you called setState whenever you change the list.
_fetchData() async {
final url = "API_URL";
print("Fetching: " + url);
final response = await http.get(url);
final busesJson = json.decode(response.body);
if (busesJson["service_id"] == null) {
globals.serviceActive = false;
setState(() {
_isLoading = false;
});
} else {
final newArrivalsList = new List<ArrivalEstimates>();
busesJson["ResultSet"]["Result"].forEach((busJson) {
if (busJson["arrival_estimates"] != null) {
busJson["arrival_estimates"].forEach((arrivalJson) {
globals.serviceActive = true;
final arrivalEstimate = new ArrivalEstimates(
arrivalJson["route_id"], arrivalJson["arrival_at"], arrivalJson["stop_id"]
);
newArrivalsList.add(arrivalEstimate);
});
}
});
setState(() {
arrivalsList = newArrivalsList;
_isLoading = false;
});
}
}
A few side notes:
I'm not sure if you actually want to clear the list before you fetch the data. If the state was updated properly, that would cause a flicker every 5 seconds.
I'm not sure if you simplified the code, but calling the _fetchData method every five seconds may become a problem if the network is slow.
If you are certain that you want a child widget to rebuild every time you call setState() and it is stubbornly refusing, you can give it a UniqueKey(). This will ensure that when setState() triggers a rebuild the child widget keys will not match, the old widget will be popped and disposed of, and, the new widget will replace it in the widget tree.
Note that this is using keys in sort of the opposite way for which they were intended (to reduce rebuilding) but if something beyond your control is hindering necessary rebuilds then this is a simple, built-in way to achieve the desired goal.
Here is a very helpful Medium article on keys from one the Flutter team members, Emily Fortuna:
https://medium.com/flutter/keys-what-are-they-good-for-13cb51742e7d
I am not sure if this is what your looking for but and im probably late on this but i believe you can use a change notifier efficiently to achieve this. Basically a change notifier is hooked to your backed logic() for instance an api data fetch. A widget is then registered with a change notifier of the same type as the change notifier provider. In event of data change, the widgets registered with the change notifier will be rebuild.
For instance
// extend the change notifier class
class DataClass extends ChangeNotifier {
....
getData(){
Response res = get('https://data/endpoint')
notifyListeners()
}
void onChange() {
notifyListeners();
}
....
}
Every time there is change in data you call the notifyListeners() that will trigger rebuild of consuming widgets.
Register you widget with a changenotifier
class View extends StatefulWidget {
Widget create(BuildContext context) {
return ChangeNotifierProvider<ModelClass>(
builder: (context) => DataClass(auth: auth),
child: Consumer<ModelClass>(
builder: (context, model, _) => View(model: model),
),
);
}
}
You can also user a Consumer for the same. Get more on this from the Documentation

Resources