带有 TabBar 的 Flutter showSearch 不随 onTap 更新,只有滑动才有效

问题描述

因此,我对 Flutter 和 dart 还很陌生,并且一直在尝试使用 TabBar 设置 showSearch,该 TabBar 在相应的选项卡中返回结果。我已经设法让它在我的 _showSearchResults 小部件中工作,只需要滑动,但不能点击标签。我从支持设置状态的另一个类 (SearchPage) 中的有状态小部件中发送了 tabController 和选择的选项卡。我还创建了一个函数 updateTab 来使用 onTap 更新索引,但它仍然失败。我认为我的问题是在这种情况下的状态管理。有关如何进行的任何建议?这是代码

class SearchPage extends StatefulWidget {

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

class _SearchPageState extends State<SearchPage> with SingleTickerProviderStateMixin {

  final int H = 896;
  final int W = 414;

  TabController _tabController;


  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 8,vsync: this);
    _tabController.addListener(_getActiveTabIndex);
  }

  void _getActiveTabIndex() {

    _selectedTab = _tabController.index;

    setState(() {

    });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  int _selectedTab = 0;

  @override
  Widget build(BuildContext context) {

    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      backgroundColor: Color(0xff121212),appBar: AppBar(
        automaticallyImplyLeading: false,backgroundColor: Color(0xff151515),title: Row(
          children: [
            InkWell(
                onTap: () {
                  showSearch(context: context,delegate:
                  NeamoSearch(tabController: _tabController,selectedTab: _selectedTab)
                  );
                },child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 50),child: Text('Search',style: TextStyle(color: Colors.grey),))),],),);
  }
}



 class NeamoSearch extends SearchDelegate {

  TabController tabController;
   int selectedTab;

  NeamoSearch({this.tabController,this.selectedTab});

  @override
  ThemeData appBarTheme(BuildContext context) {
    return ThemeData(
        textTheme: TextTheme(headline6: TextStyle(color: Colors.grey,fontSize: 22),bodyText2: TextStyle(color: Colors.grey,fontSize: 18)

        ),primaryColor: Color(0xff151515));
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    return [

      IconButton(icon: Icon(Icons.clear,color: Colors.grey),onpressed: (){
        query = '';
      })

    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(icon: Icon(Icons.arrow_back_ios,onpressed: (){
      close(context,null);
    });
  }

  @override
  Widget buildresults(BuildContext context) {

    final int H = 896;
    final int W = 414;

    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return _showSearchResults(height,width,H,W,tabController,selectedTab);
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return Container(
      child: Scaffold(
          backgroundColor: Color(0xff121212),body: Text(query,style: TextStyle(color: Colors.black),)),);
  }

  Widget _showSearchResults(height,selectedTab) {

    return
      Scaffold(
            backgroundColor: Color(0xff121212),body: Column(
             children: [
               Container(

            height: (70/H) * height,color: Colors.white.withOpacity(0.02),child:
              TabBar(
              controller: tabController,indicatorColor: Colors.transparent,labelColor: Color(0xffc49058),onTap: (index) => updateTab(index),isScrollable: true,labelPadding: EdgeInsets.only(left: 20,right: 20),unselectedLabelColor: Colors.grey,tabs: [
                Tab(
                  child: Text('FEATURED',style: TextStyle(
                        fontSize: 13.0,fontWeight: FontWeight.bold
                    ),Tab(
                  child: Text('TRENDING',Tab(
                  child: Text('RECENTLY ADDED',Tab(
                  child: Text('NEAMO 100',Tab(
                  child: Text('ARTISTS',Tab(
                  child: Text('ALBUMS',Tab(
                  child: Text('GENRES',Tab(
                  child: Text('NEAMO SHOWS',Expanded(
            child:
            TabBarView(
                controller: tabController,children: [
                  SearchFeaturedList(),SearchFeaturedList(),Neamo100(),AllArtistsList(),AllAlbumsList(),AllGenresList(),Neamoshows(),]),);

  }



   updateTab(int index) {

    selectedTab = index;

  }


}


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)