dimanche 27 juin 2021

setting state with if else

long time listener, first time caller. I am not a developer, but trying to make a useful program for myself and some colleagues. The app is used to display pdf's that I have saved on firebase, but I want the UI to vary based on if the file has been downloaded or not. The pdf would be displayed on a card with options to view online or download, while a second car would have options to view, share, and delete. To do this I am trying to use an if else after a check if the file exists. But I don't really know how to manipulate the set state to change my isDownloaded variable to alter the state. Here is the code snippet that I'm working with, I don't get an error, just the isDownloaded state never changes unless hardcoded. localPath is a variable for the local documents directory. Thanks for the help with this!

class RefCardType extends StatefulWidget {
  const RefCardType(
  {Key? key,
  required this.urlPath,
  required this.fileName,
  required this.page,
  required this.title,
  required this.cardTitle,
  required this.cardSubTitle})
  : super(key: key);
   final String urlPath;
   final String fileName;
   final int page;
   final String title;
   final String cardTitle;
   final String cardSubTitle;

   @override
   _RefCardTypeState createState() => _RefCardTypeState();}

class _RefCardTypeState extends State<RefCardType> {
  void checkFile() async {
  File file = File('$localPath/${widget.fileName}');

  if (await file.exists() == true) {
  setState(() {
    isDownloaded = true;
  });
  {
    setState(() {
      isDownloaded = false;
    });
   }
  }
 }

bool isDownloaded = true;

@override
 Widget build(BuildContext context) {
  if (isDownloaded == true) {
  return refFileCard(context,
      urlPath: widget.urlPath,
      fileName: widget.fileName,
      page: widget.page,
      title: widget.title,
      cardTitle: widget.cardTitle,
      cardSubTitle: widget.cardSubTitle);
   } else {
   return refUrlCard(context,
      urlPath: widget.urlPath,
      fileName: widget.fileName,
      page: widget.page,
      title: widget.title,
      cardTitle: widget.cardTitle,
      cardSubTitle: widget.cardSubTitle);
   }
  }
 }

Aucun commentaire:

Enregistrer un commentaire