Is there a way to display RaisedButtons in a ButtonBar lazily? - flutter

I have a set of buttons I want to display on a flutter app to represent the days of the week. I currently have a widget that builds 7 RaisedButtons to represent the various days of the week. Is there a way I can do this lazily instead of having to build all 7 RaisedButtons in flutter?
Note I am using the ButtonBar widget to display the set of buttons.

You can declare a list like following:
List<Widget> _optionList = List<Widget>();
and add as many and any kind of widget in the list, like following:
void setupHintButtonsUI() {
_optionList.clear();
for (var i = 0; i < 7; i++) {
_optionList.add(
FlatButton(),
);
}
}

You can make an empty list of strings and add days name in it and then iterate through it. Create a function using the for loop like
var i = "";
main() {
final List<String> days = <String>['Sunday', 'Monday', 'Tuesday','Wednesday','Thurday','Friday'];
days.sort();
// print(days);
for (i in days){
print(i);
}
}
and then you can loop through the code and create widgets

Related

Dynamically added widget state is null in Flutter

I am developing a feature where users can press a button to add a new group of text fields to the screen. Each group of text fields is stored in its own stateful widget. The abridged code to add the new widget is shown below:
List<EducationField> fieldList = [];
List<GlobalKey<EducationFieldState>> keyList = [];
// Function that adds new widgets to the list view
onTap: () {
GlobalKey<EducationFieldState> key = new GlobalKey<EducationFieldState>();
setState(() {
fieldList.add(EducationField(key: key));
keyList.add(key);
});
},
I can dynamically add the new widgets just fine. However when I try to access the state of the widgets, I get an error saying that the state of the respective widget is null. There is a function in each widget state that gets the values from their text fields. The code I'm using to do that is also shown below:
void _getUserData(){
List<EducationModel> modelList = [];
for(int i = 0; i < fieldList.length; i++){
modelList.add(keyList[i].currentState!.getData()); // this line is causing the error
modelList.last.printModel();
}
}
I have done a lot of research on this issue and still have no idea why I am getting a null error. Is my approach wrong or is it something more minor? I can also give more code if necessary.
Thanks in advance!
Checkout GlobalKey docs.
Your dynamically added Text widget doesn't exist in the Widget tree yet, as you just created it and you're trying to add it to the Widget tree. So it doesn't have a current state.
Maybe this helps?
keyList[i].currentState?.getData() ?? '' // I'm guessing getData return a String
Something else you could try:
// only call _getUserData() after widget finished building
WidgetsBinding.instance!.addPostFramecallback(() => _getUserData());

I have a stored List data and how can I localize it in flutter?

I can translate the text inside the widget using AppLocalizations.of(context)!.translated and it work fine
However, when I want to localize a stored List<> data which will be used in listView Builder, I am not able to do so, since it need (context).
My question is how to localize a stored list data(outside widget) and pass to list view builder?
Thanks for your help.
I think you should do 2 things
In case if you have a list in another file, then it should be static like this:
class Presets {
static var presetData = [];
}
after you loaded up your list, you can do these steps:
First,
Create Widgets as children like this:
List<Widget> presetsLoadUp() {
List<Widget> children = <Widget>[];
for(i < yourList.length){
children.add(Text(yourList[i]));
}
return children;
}
You can create your own unique widget inside the children.add method.
Secondly,
Create the list:
...
child: ListView(
children: presetsLoadUp(),
),
If anyone has similar problem, please take it for reference.
I just build another widget to solve this.
What I do is :
build another widget,
then put the list data inside and
return ListViewBuilder.
Everything is same,
just put your list data inside another widget can solve the problem

how to display random image on a container in flutter

I am new at flutter. and I want to add random picture in a container how to add random picture from assets()?
For example when user use my application and refresh the page the picture in the container will change automatically.
You shall make a list of all the asset files like this and so on...
List<String> assets=['assets/file1.png','assets/file2.png'];
in your build function call assets.shuffle(). This will reorder the assets file in random order and then make a variable imagepath as:-
String imagePath=assets[0];//now this will store a random image path ..
Whole code:-
class MyApp extends StatelessWidget {
List<String> assets=['assets/file1.png','assets/file2.png'];
String imagePath;
#override
Widget build(BuildContext context) {
assets.shuffle();//shuffle over here
imagePath=assets[0];//store random image over here
return Container(
child: Image.asset(imagePath),//show random image
);
}
}
You can also perform shuffle and other tasks in the initState of a statefull widget.
You have to implement something yourself. I would suggest adding possible images to a list
const List<String> randomImages = [
"assets/images/image1",
"assets/images/image2",
"assets/images/image3",
"assets/images/image4",
"assets/images/image5",
];
Then using darts random class, Find a random int to use as an index that is bounded by the length of the string
Random random = Random()
int randomIndex = random.nextInt(randomImages.length)
Image randomImage = randomImages[randomIndex];

How to have multiple arguments to a list in Flutter

I have a function which returns a List of ListTiles.
List<ListTile> tinyImage = [];
List<ListTile> croppedImages() {
ListTile newImage =
ListTile(title: crop1 == null ? Text('No Image') : ListPart());
tinyImage.add(newImage);
return tinyImage;
}
But i want this list to have other arguments like Text, DrawerHeader and other stuffs. Like,
List<ListTile, DrawerHeader> tinyImage = [];
I am trying to create a Drawer. For now i am able to return a list of images with the above function. I want to add a header which is not possible at the moment. Please help me out.
You have to set the list type to something more generic, like Widget.
List<Widget> items = [];
items.add(DrawerHeader());
items.add(ListTile());

How to add items that generated through random method to a list and display it?

By using random function it displayed some of random element from list in a container how can i add that specific item into a list and display it as favourites? Please help me with this.
List<String> favourites = [];
onLongPress: () {
setState(() {
randomIndex = Random().nextInt(hes.length);
favourites.add(hes[randomIndex].name);
});
},
you just need to add a random element into a list and display it using listview builder may be