I have a FAB which creates a new Card
when using onPressed
. The List
is created using this code :
List<Widget> cardList = new List.generate(_count, (int i) => new createCard());
. Is it possible to access this list in another class?
main.dart:
import 'package:flutter/material.dart';
import 'package:todo_list/starting_screen.dart';
void main(){
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(brightness: Brightness.dark, backgroundColor: Colors.black87),
home: Starting_screen()
));
}
starting_screen.dart:
import 'all_classes.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
class Starting_screen extends StatefulWidget {
@override
_Starting_screenState createState() => _Starting_screenState();
}
class _Starting_screenState extends State<Starting_screen> {
int _count = 0;
@override
Widget build(BuildContext context) {
List<Widget> cardList = new List();
SystemChrome.setEnabledSystemUIOverlays([]);
void functionToGenerateTheList(){
cardList = new List.generate(_count, (int i) => new createCard());
}
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () async {
setState(() {
_count += 1;
});
},
heroTag: "btn2",
child: Icon(Icons.add, color: Color(whitecolor),), // this is just a custom color
backgroundColor: Color(redcolor),), // this is just a custom color
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
flexibleSpace: FlexibleSpaceBar(
),
actions: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.only(top: 20, right: 10),
child: whitefontstyle(text: "Remaining tasks for today - ${cardList.length}", size: 20,), // this is just a custom textstyle
),
),
],
),
SliverGrid(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2
),
delegate: new SliverChildBuilderDelegate((context,
index) {
return cardList[index]; // this is where the cards are displayed in a list
},
childCount: cardList.length
)
),
]
)
);
}
}
all_classes.dart:
import 'package:flutter/material.dart';
const redcolor = 0xFFF0002F;
const whitecolor = 0xFFFFFFFF;
const blackcolor = 0xFF000000;
class createCard extends StatefulWidget {
// createCard();
@override
_createCardState createState() => _createCardState();
}
class _createCardState extends State<createCard> {
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text("project 1"),
trailing: new IconButton(
key: Key(UniqueKey().toString()),
onPressed: (){
setState(() {
}
);
},
icon: Icon(Icons.close),
color: Color(redcolor),
highlightColor: Color(whitecolor),
),
// subtitle: whitefontstylemont(text: "project 1", size: 20,)) //this is just a custom TextStyle
),
);
}
}
class whitefontstyle extends StatelessWidget{
Color color = Color(whitecolor);
String text = "";
double size;
whitefontstyle({@required this.text, this.size,});
@override
Widget build(BuildContext context) {
// TODO: implement build
return Text("$text", style: TextStyle(height: 1.2, fontSize: size, color: color),);
}
}
EDIT: I want to essentially implement a delete Card
functionality to this app. But i need to access the list cardList
in the createCard
class under the file "all_classes.dart"
You can declare the list being a member of the class Card
.
So the Card
(or the class where you are generating the list) class will be as follow:
public class Card {
private List<Widget> cardList;
public void functionToGenerateTheList(){
cardList = new List.generate(_count, (int i) => new createCard());
}
public List<Widget> getCardList(){
return cardList;
}
}
So the idea is to make cardList
a private member of the class (will name it X
) where you are creating it, then create a public getter method to be able to retrieve it from whatever class having an instance of the class X
(having cardList
as member)
Note: this code snippet is in java
User contributions licensed under CC BY-SA 3.0