State not updating when using pdfViewerController.searchText(text)

Hello,

On some devices the text search can take a while, so I want to show a CircularProgressIndicator while the search is taking place.

This code gives the result I'm expecting:

onSubmitted: (text) async {
if (text.isNotEmpty) {
setState(() {
_pdfAppBarState = PdfAppBarState.searching;
});
await Future.delayed(const Duration(seconds: 5));
setState(() {
_pdfAppBarState = PdfAppBarState.searchComplete;
});
}
},


But when I replace the delay with the controller searchText as in the following, the first setState doesn't appear to rebuild my stateful widget, but the second does, so the CircularProgressIndicator doesn't show at all. As far as my stateful widget is concerned, the _pdfAppBarState goes straight to PdfAppBarState.searchComplete.

Any ideas why this might be?


onSubmitted: (text) async {
if (text.isNotEmpty) {
setState(() {
_pdfAppBarState = PdfAppBarState.searching;
});
_searchResult = await _pdfViewerController.searchText(text);
setState(() {
_pdfAppBarState = PdfAppBarState.searchComplete;
});
}
},

7 Replies 1 reply marked as answer

LU Luke December 20, 2021 09:17 AM UTC

I thought that maybe searchText is blocking the main thread and perhaps I could run searchText in a separate Isolate, but it doesn't appear possible to isolate the searchText method due to dependencies in the main thread (I tried passing the PdfViewerController as a message to the new isolate but got an error relating to dependency on dart:ui).



DR Deepika Ravi Syncfusion Team December 20, 2021 12:43 PM UTC

Hi Luke, 
 
We can reproduce the reported issue from our side. We are analyzing on this and will update the further details on 22 December 2021. 
 
Regards, 
Deepika Ravi 



LU Luke December 20, 2021 12:54 PM UTC

Thank you Ravi



DR Deepika Ravi Syncfusion Team December 22, 2021 11:27 AM UTC

Hi Luke, 
 
We created the sample without the searchText() API in that also the reported issue is reproduced. In the sample, we have set the visibility of the progress bar in the onSubmitted property of the text field. In the first setState, we have set the visibility of the progress bar as true, but the widget is not rebuilt, and the progress bar visibility is also not changed. In the second Setstate, the widget is rebuilt, and the visibility of the progress bar is changed. When setState method is invoked multiple in a method, then only setState will be considered which is default behavior of Flutter. 
 
Code Snippet: 
 
import 'package:flutter/material.dart';

void main() {
  runApp(
MaterialApp(home: HomePage()));
}

/// Represents Homepage for Navigation
class HomePage extends StatefulWidget {
 
@override
 
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool
_showProgressbar = false;

 
@override
 
Widget build(BuildContext context) {
   
return Scaffold(
        appBar:
AppBar(
            title:
const Text('Home Page'), backgroundColor: Colors.deepOrange),
        body:
Column(
            mainAxisAlignment: MainAxisAlignment.
center,
            children: <Widget>[
             
TextField(
                  decoration:
const InputDecoration(hintText: 'Enter text'),
                  onSubmitted: (String str) {
                    setState(() {
                     
_showProgressbar = true;
                    });

                    setState(() {
                     
_showProgressbar = false;
                    });
                  }),
             
Visibility(
                  visible:
_showProgressbar,
                  child:
const CircularProgressIndicator()),
            ]));
  }
}
 
 
 
Regards, 
Deepika Ravi 



LU Luke December 22, 2021 01:38 PM UTC

Thanks Ravi. I see what you mean. Strange that it works fine if the searchText() is replaced with:


await Future.delayed(const Duration(seconds: 5));


If I combine the Future.delayed along with searchText(), I get the state changes I expect, but the CircularProgressIndicator only spins during the Future.delayed. It freezes when searchText() is called.


I'll see if I can find a similar example that gives me an alternative way to achieve the user experience that I'm looking for.




DB Dilli Babu Nandha Gopal Syncfusion Team December 24, 2021 10:32 AM UTC

Hi Luke, 
 
We can reproduce the reported UI freeze issue while performing text search. This is a known limitation due to synchronous text searching in Syncfusion Flutter PDF package. We have used text search methods from Syncfusion PDF package. Since, the requested support is yet to be given in that package, we are unable to do in our widget. Once the asynchronous support is provided, UI freeze issue will be resolved. The status of this feature can be tracked using following link. 
 
We will let you know once the feature is implemented. 
 
Regards, 
Dilli babu. 


Marked as answer

LU Luke December 24, 2021 11:08 AM UTC

Thanks Dilli babu, I have added my vote to this feature request. 


Loader.
Up arrow icon