0

I am using a Tabbar widget which controls a TabbarView. When I call on setstate after changing the value of a variable named "searchTerm", the anticipated change isn't updated in the tabbarView widgets until I click on any of the tabs.I need the tabviewView to update immediately i call on setstate.Please i need a response as soon as possible See screenshot here

Below is the code

        class  HomeScreen extends StatefulWidget {
    
      @override
      _HomeScreenState createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> with SingleTickerProviderStateMixin {
    
      String searchTerm;
      List <Widget> kTabPages;
      TabController tabController;
    
      final ktabs = <Tab>[Tab(child: Text("All"),),Tab(child: Text("Popular"),),Tab(child: Text("Top"),),];
    
    
    
          @override
          void dispose() {
            tabController.dispose();
            super.dispose();
      }
    
      @override
      void initState() {
      tabController = TabController(length: ktabs.length, vsync: this);
    
        super.initState();
      }
      @override
      Widget build(BuildContext context) {
    
        kTabPages = <Widget>[Center(child: GridWidget(category: "all",searchTerm: searchTerm,),),
          Center(child:  GridWidget(category: "popular",searchTerm: searchTerm,),),
          Center(child: GridWidget(category: "top",searchTerm: searchTerm,),)];
    
        return Scaffold(
          resizeToAvoidBottomInset: false,
          backgroundColor: Color.fromARGB(255, 69, 22, 99),
          body: SafeArea(
            child: DefaultTabController(
              length: ktabs.length,
              child: Column(
                children: [
                  Expanded(child: Container(
                    child: Padding(
                      padding: const EdgeInsets.only(left: 25,right: 16,top: 25,bottom: 10),
                      child: Column(
                        children: [
                          Expanded(
                            flex: 5,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                               Row(
                                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                 children: [
                                   GestureDetector(
                                     child: Icon(
                                       Icons.apps_outlined,
                                       color: Colors.white,
                                     ),
                                  
                                   ),
                                   GestureDetector(
                                     onTap: ()async{
                     //assigns a new value to variable searchName after returning from SearchScreen
                                     var searchName = await Navigator.push(context, 
                                   MaterialPageRoute(builder: (context){
                                         return SearchScreen();
                                       }));
                                     if(searchName != null){
                                   
                                       setState(() {
//Assigns SearchName value to variable searchTerm
                                         searchTerm = searchName;
                                        
                                       });
    
                                     }
                                     },
                                     child: Icon(
                                         Icons.search,
                                       color: Colors.white,
                                     ),
                                   ),
                                 ],
                               ),
                                SizedBox(
                                  height: 15,
                                ),
                                Text("Hi Xeroes️",style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.w700,
                                  fontSize: 25.0
                                ),),
                                SizedBox(
                                  height: 10,
                                ),
                                Text("Today is a good day",style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.w400,
                                    fontSize: 13.0
                                ),),
                                SizedBox(
                                  height: 2,
                                ),
                                Text("To learn something new!",style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.w400,
                                    fontSize: 13.0
                                ),),
    
                              ],
                            ),
                          ),
                          Expanded(
                            flex: 1,
                            child: TabBar(
                              controller: tabController,
                              tabs:ktabs ,
                              labelColor: Colors.white,
                              indicatorColor: Colors.amberAccent,
                              indicatorSize: TabBarIndicatorSize.label,
                              labelStyle: TextStyle(
                                //   fontSize: 16
                              ),),
                          )
                        ],
                      ),
                    ),
                    color: Color.fromARGB(255, 69, 22, 99),
                  ),
                  flex: 4,),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.only(top: 20),
                      child: TabBarView(
                        controller: tabController,
                        children: kTabPages,
                      ),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.only(topLeft: Radius.circular(30),topRight:Radius.circular(30))
                    ),
                  ),
                    flex: 8,)
                ],
              ),
            ),
          ),
        );
      }
    }
  • can you explain more precisely or with the help of images – Aloysius Samuel Aug 09 '21 at 09:51
  • I have included a link to the screenshot in the post. When I click on the search item and go to the search page, it returns back to the homepage with a value which is suppose to update the tabview widget according to the what is being searched for but it doesn't. I have to click on any of the tabs before the tabview widget is updated according to what is being searched for. Do you understand now? – Codes-untold Aug 09 '21 at 11:48

0 Answers0