Android_devNL
Android_devNL

Reputation: 147

How can i pass string data from one file to the other flutter

I want to pass the data of this string: String pathPDF = "assets/borst-oei.pdf"; to an other dart file named arbo.dart

The problem is that is don't know how I can do this with this: GestureDetector( onTap: () => Navigator.of(context).push(PageTransition( child: PDFScreen(), type: PageTransitionType.fade)),

i can't do this: GestureDetector( onTap: () => Navigator.of(context).push(PageTransition( child: PDFScreen(pathPDF: ''),type: PageTransitionType.fade)), because it isn't defined then

this is the string: String pathPDF = "assets/borst-oei.pdf";

How can I fix this?

this is a bit of the code:

import 'package:get/get.dart';
import 'package:hetmaantje/utils1/colors.dart';
import 'package:page_transition/page_transition.dart';
import 'arbo.dart';
import 'assets.dart';

class oeiikgroei extends StatefulWidget {
  const oeiikgroei({Key? key}) : super(key: key);

  @override
  State<oeiikgroei> createState() => _oeiikgroeiState();
}


  class _oeiikgroeiState extends State<oeiikgroei> {
    String pathPDF = "assets/borst-oei.pdf";

  @override
  Widget build(BuildContext context) {

    return Scaffold(

        appBar: AppBar(
          elevation: 0,
          title: const Text(
            "Oei ik groei",
            style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
          ),
        ),
        body: Container(
        decoration: BoxDecoration(
        gradient: LinearGradient(colors: [
        hexStringToColor("no"),
    hexStringToColor("notshowing"),
    hexStringToColor("this is not an mistake"),
    hexStringToColor("#sorry")

    ], begin: Alignment.topCenter, end: Alignment.bottomCenter)),


          child: Card(

            color: Colors.transparent,
           elevation: 0,

              shape: RoundedRectangleBorder(


              ),

           child: ListView(
             padding: EdgeInsets.all(8),
             physics: const BouncingScrollPhysics(),
             children: [
           Column(



           children: [

           SizedBox(

               width: double.infinity,
               child: Image.asset(Assets.imagesIcon17, fit: BoxFit.cover)),
            SizedBox(
              height: Get.size.height * .02,
            ),]),

             ListTile(
contentPadding: EdgeInsets.only(left: 5, top: 1 , right: 4),
           tileColor: Colors.transparent,
             onTap: () {

               Navigator.of(context).push(PageTransition(
                   child: PDFScreen(),
                   type: PageTransitionType.fade));

             },
             textColor: Colors.white,
             shape: RoundedRectangleBorder(

               side: BorderSide(color: Colors.white, width: 3 ),
               borderRadius: BorderRadius.circular(10),
             ),  


I have extended the PDFScreen now and it works, but i will use the way below instead.

Is there any preformence matters between the methodes?

Upvotes: 0

Views: 372

Answers (2)

Amit Bahadur
Amit Bahadur

Reputation: 259

Navigation from here with data

 GestureDetector(
 onTap: () {
   Navigator.push(
     context,
     MaterialPageRoute(
       builder: ((context) => PDFScreen(pathPDF: pathPDF)),
     ),
    );
  },
);

Get data in another file

class PDFScreen extends StatelessWidget {
  const PDFScreen({Key? key, required this.pathPdf}) : super(key: key);
  final String pathPdf;      
  // The rest of your code
}

Upvotes: 0

Robert Sandberg
Robert Sandberg

Reputation: 8607

What isn't "defined then" ? The parameter to the PDFScreen or the String pathPDF itself?

Just add the parameter to the constructor of the widget named PDFScreen (if it is StatefulWidget that doesn't matter).

class PDFScreen extends StatelessWidget {
  const PDFScreen({Key? key, required this.pathPdf}) : super(key: key);
  final String pathPdf;
  ...
}

Then use it as:

GestureDetector(
  onTap: () => Navigator.of(context).push(
    PageTransition(
      child: PDFScreen(pathPDF: pathPDF),
      type: PageTransitionType.fade),
  ..
)

Upvotes: 1

Related Questions