skip to Main Content

I’m using Flutter pdf package inside my project (https://pub.dev/packages/pdf). The package generates a document which I show on the screen. I want to add localisation (localised Text fields) into my generated pdf document but AppLocalizations.of(context) requires BuildContext, not just a Context.

As I understood, generated pdf file is not a part of material Scaffold, so there is no way to access BuildContext.
Page constructor

Any ideas how to get access to BuildContext when I configure localised Text strings in my pdf Page?

I tried to find the answer on package page and package repo, but I have not found any solution so far.

2

Answers


  1. instead of passing the context, try to call AppLocalizations as a singleton without passing the context. some intl libraries provide singlton, if you couldn’t find any, try to store the instance in GetIt , or build your own technique and store it in main widget.

    Login or Signup to reply.
  2. 1) Define a Global Modal
    2) Create a Stateless widget
    3) Define a Local Modal inside of this widget like :
       
    
     DrawingModel localModelForDevelop = DrawingModel(
        musteriId: AppLocalizations.of(context).turnBack,
    
    4) in this widget 
        return Container(
                height: Get.height,
                color: AppTheme().kirlibeyaz,
                child: PdfPreview(
                  build: (format) => _generatePdf(
                      localModelForDevelop,
                      const PdfPageFormat(21.0 * cm, 29.7 * cm, marginAll: 0.5 * cm),
                      "Test"),),);
    
    5) Define your pdf widget inside of your stateless 
    Future<Uint8List> _generatePdf(DrawingModel insidelocalModelForDevelop) async{
    you can define finals here  like :
    final font = await PdfGoogleFonts.openSansLight();
    final fontbold = await PdfGoogleFonts.openSansBold();
    
    final _yourPdf = [
    pw.Text('Tarih : ${alocalModelForDevelop.cizimTarihi.toString()}',
    style: const pw.TextStyle(fontSize: 8),) ]
    
    pdf.addPage(
    // Page Ayarları
          pw.MultiPage(
              theme: pw.ThemeData(defaultTextStyle: pw.TextStyle(font: font)),  
              build: (context) {
                return _yourPdf ;
              }),
        );
    

    A bit complicated but it works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search