I have a class in flutter where i am initializing some class object. I want to call that class, object will be initialize and i want to access that object in the caller class.
below is my code
import 'package:flutter/material.dart';
@immutable
class CardviewListItem {
//final FlatButton icon;
final IconData icon;
final String title;
final String amount;
const CardviewListItem({
@required this.icon,
@required this.title,
@required this.amount,
});
}
class SomeOtherClass {
final summaryListItems = <CardviewListItem>[
CardviewListItem(
title: 'Total Income',
amount:'4434.65',
icon: makeIconWithCircle(Icons.attach_money) // call function to create an icon with a circle in the background.
),
];
static Widget makeIconWithCircle(IconData iconData) { // function takes in the icon you want to create with a green background
final circle = Container(
height: 25.0,
width: 25.0,
decoration: BoxDecoration(
color: Colors.green,
shape: BoxShape.circle
),
);
final icon = Icon(
iconData, // iconData paramater is used here
color: Colors.white,
size: 18,
);
final iconWithCircle = Stack(
alignment: Alignment.center,
children: <Widget>[
circle,
icon
],
);
return iconWithCircle;
}
}
i want to call SomeOtherClass and get access to summaryListItems object here is my caller class
import 'package:flutter/material.dart';
import 'package:finsec/widget/cardview_widget.dart';
import 'package:finsec/utils/strings.dart';
import 'package:finsec/utils/dimens.dart';
import 'package:finsec/utils/colors.dart';
import 'package:finsec/data/cardview_list_item.dart';
void main() {
runApp(new HomeScreen());
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: new Stack(
children: <Widget>[
new Container(
height: margin_200dp,
color: colorPrimary,
),
new Container( //container to overlay on top of blue container
alignment: Alignment.topCenter,
padding: new EdgeInsets.only(top: margin_0dp, right: margin_8dp, left: margin_8dp, bottom: margin_5dp),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
zeroAmount,
style: TextStyle(color: white, fontSize: 40.0, fontWeight: FontWeight.bold)
),
Text(
currentBalance, style: TextStyle(color: white, fontSize: 20.0, fontWeight: FontWeight.bold)
),
cardView(
header: summaryCard,
elevation: margin_4dp,
height: margin_220dp,
padding: margin_15dp,
listItems: <<I WANT TO ACCESS summaryListItems HERE>>
),
//cardView(context)
],
),
)
],
),
);
}
}
i want to be able to call SomeOtherClass and access summaryListItems in the caller class. see line listItems: <> in the above code
You just need to get an instance of SomeOtherClass in HomeScreen, depends of how you want to model it some class need to create and inject the instance on the screen or just let the screen create it. The easiest way is create the instance on the screen and is basically :
class HomeScreen extends StatelessWidget {
SomeOtherClass someotherClass = SomeOtherClass();
And then on list just use
cardView(
header: summaryCard,
elevation: margin_4dp,
height: margin_220dp,
padding: margin_15dp,
listItems: someotherClass.summaryListItems,
),
Hope it helps,