5

I have a widget with a lot of contents like image, text and more, which make it heavy widget in flutter app, But when the app is navigated to the widget having the complex widget the app faces the jank since the widget is too large to load at an instant,

I want to show simple lite loading widget until the original widget is loaded thus removing the jank from the app and enable lazy loading of the widget,

How to achieve this in flutter?

EDIT:- To make it clear, I am not loading any data from the Internet, and this is not causing the delay. For Loading the data from Internet we have FutureBuilder. Here my widget is itself heavy such that it takes some time to load.

How to display loading Widget while the main widget is being loaded.

rahul Kushwaha
  • 2,395
  • 7
  • 32
  • 64

3 Answers3

7

First you have to create a variable to keep the state

bool isLoading = true; //this can be declared outside the class 

then you can return the loading widget or any other widget according to this variable

return isLoading ? 
  CircularProgressIndicator() //loading widget goes here
  : Scaffold() //otherwidget goes here

you can change between these two states using setState method

Once your data is loaded use the below code

setState(() {
        isLoading = false;
      });

Sample Code

class SampleClass extends StatefulWidget {
  SampleClass({Key key}) : super(key: key);

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

bool isLoading = true; // variable to check state

class _SampleClassState extends State<SampleClass> {
  loadData() {
    //somecode to load data
    setState(() {
      isLoading = false;//setting state to false after data loaded
    });
  }

  @override
  void initState() {
    loadData(); //call load data on start
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: isLoading ? //check loadind status
      CircularProgressIndicator() //if true
      :Container(), //if false
    );
  }
}
Suthura Sudharaka
  • 643
  • 10
  • 25
3

This is a perfect place to use a FutureBuilder.

Widget loadingWidget = ...;

Future<Widget> buildHeavyWidget() async {
  // build and return heavy widget
}
FutureBuilder(
  future: buildHeavyWidget(),
  builder: (context, snapshot) {
    if(snapshot.hasData) {
      // after the future is completed
      // the heavy widget is availabe as snapshot.data
      return snapshot.data;
    }
    return loadingWidget;
  },
)
Navaneeth P
  • 1,428
  • 7
  • 13
0

First define a bool value.

bool isLoading = false;

In your function.

yourfunction(){
setState(){
 isLoading = true;
 }

 setState(){
 isLoading = false;
 }
}

In your widget.

isLoading?CircularProgressIndicator():Widget()

S.R Keshav
  • 1,965
  • 1
  • 11
  • 14