Reputation: 81
I need to pass the asset path which is a text file to a new Screen. I want to pass that asset path from a ListTile by using OnTap() and pass that path to a new screen class.
Asset path:
'lib/asset/textfile/cs_one.txt'
My ListTile:
ListTile(
leading: CircleAvatar(
backgroundImage: AssetImage("image/icon.png"),
),
title: Text("Story One"),
trailing: Icon(Icons.arrow_forward),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => cs_one() ),
);
}
I want to pass the path to another class which is a new screen from where I will load the asset data. I know how to load the asset data I just needed help passing it from ListTile to the new screen class.
Upvotes: 1
Views: 234
Reputation: 1351
Here's how you can pass data to another widget in Flutter:
class MyWidget extends StatelessWidget {
const MyWidget({Key? key}) : super(key: key);
final String assetPath = 'lib/asset/textfile/cs_one.txt';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: ListTile(
leading: CircleAvatar(
backgroundImage: AssetImage(assetPath),
),
title: const Text("Story One"),
trailing: const Icon(Icons.arrow_forward),
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) => NextScreen(assetPath: assetPath)
));
}
),
)
);
}
}
class NextScreen extends StatelessWidget {
final String assetPath;
const NextScreen({Key? key, required this.assetPath}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(assetPath),
),
);
}
}
Upvotes: 1