In Flutter, how do I pass data into a Stateless Widget?
class MainMenuCard extends StatelessWidget {
final Choice choice;
final int index;
const MainMenuCard({Key key, this.choice, this.index, Choice Choice, int int})
: super(key: key);
And Call like this
MainMenuCard(choice : choices[index],int: index)
there 'Choice' is Model class (when we using with List.generate) otherwise you can easily pass Data.
final Choice choice;
const List<Choice> choices = const <Choice>[]
Yes you can do this simply by passing the info to the constructor. Something like this:
class MyApp extends StatelessWidget {
MyApp(this.yourData);
final int yourData;
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'App Title',
theme: new ThemeData(
primarySwatch: Colors.green,
),
home: new MainBody(yourData),
);
}
}
class MainBody extends StatelessWidget {
MainBody(this.yourData);
final int yourData;
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20.0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new TimeCheck(yourData),
],
),
),
);
}
}