1

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.

Emranul_Rakib
  • 101
  • 2
  • 7

1 Answers1

1

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),
      ),
    );
  }
}
Will Hlas
  • 1,241
  • 1
  • 6
  • 14
  • My nextpage is showing empty instead of text content. What have I done wrongly? can you please take a look? here is my code - https://flutlab.io/editor/c0644d92-7ded-47ab-960e-84eb2614e628 – Emranul_Rakib Sep 23 '21 at 19:59