flutter-基于选项卡主体中内容长度的选项卡名称中的动态字符串

发布于 2025-01-22 14:14:58 字数 1388 浏览 2 评论 0原文

大家好,我正在尝试更新选项卡名称,其中包含在选项卡内容中的列表中的项目数。该列表在选项卡内容组件中获取。如何将列表长度变量传递给标题标题?

// PARENT WIDGET

    class EventsScreen extends StatefulWidget {
  const EventsScreen({Key? key}) : super(key: key);

  @override
  State<EventsScreen> createState() => _EventsScreenState();
}

class _EventsScreenState extends State<EventsScreen> {
  
  @override
  Widget build(BuildContext context) {
    return Parent(
      child: TabbedHeader(
        tabTitles: [
          'Žiadosti (HERE I WANT TO ADD LENGTH OF REQUESTS LIST)'
        ],
        title: 'Udalosti',
        tabContents: [
          EventRequestTab() // CHILD COMPONENT WITH LIST OF REQUESTS
        ],
      ),
    );
  }
}

// CHILD WIDGET WITH REQUESTS LIST

class EventRequestTab extends StatefulWidget {
  const EventRequestTab({Key? key, this.requests}) : super(key: key);

  @override
  State<EventRequestTab> createState() => _EventRequestState();
}

class _EventRequestState extends State<EventRequestTab> {
  late EventService _eventService;
  late List<EventRequest>? requests;

  @override
  void initState() {
    super.initState();
    _eventService = EventService();
    requests = // FETCHING LIST OF REQUESTS

  }
} ... build method etc ....

在Initstate中的儿童小部件中,我正在获取后来在ListView中显示的请求列表。但是我不知道如何将被提取列表的长度传递给TabTitles属性。 TabBedheader是我的Tabbar小部件的自定义包装器。

Hi all I am trying to update Tab name with the number of items in lists stored in tab content. The list is fetched in tab content component. How can I pass list length variable to tab title ?

// PARENT WIDGET

    class EventsScreen extends StatefulWidget {
  const EventsScreen({Key? key}) : super(key: key);

  @override
  State<EventsScreen> createState() => _EventsScreenState();
}

class _EventsScreenState extends State<EventsScreen> {
  
  @override
  Widget build(BuildContext context) {
    return Parent(
      child: TabbedHeader(
        tabTitles: [
          'Žiadosti (HERE I WANT TO ADD LENGTH OF REQUESTS LIST)'
        ],
        title: 'Udalosti',
        tabContents: [
          EventRequestTab() // CHILD COMPONENT WITH LIST OF REQUESTS
        ],
      ),
    );
  }
}

// CHILD WIDGET WITH REQUESTS LIST

class EventRequestTab extends StatefulWidget {
  const EventRequestTab({Key? key, this.requests}) : super(key: key);

  @override
  State<EventRequestTab> createState() => _EventRequestState();
}

class _EventRequestState extends State<EventRequestTab> {
  late EventService _eventService;
  late List<EventRequest>? requests;

  @override
  void initState() {
    super.initState();
    _eventService = EventService();
    requests = // FETCHING LIST OF REQUESTS

  }
} ... build method etc ....

In child widget in initState I am fetching list of requests which is later displayed in ListView. But I dont know how to pass length of fetched list to tabTitles property of TabbedHeader. TabbedHeader is my custom wrapper for TabBar widget.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫漫岁月 2025-01-29 14:14:58

您可以通过eventRequestTab的状态类中获取的列表,通过将回调传递到EventRequestTab widtget中。
此回调将返回以其参数获取的列表,在eventsscreen中,Tab名称已更新,并使用列表的长度更新

此处,此处是您的代码的更新:


// PARENT WIDGET

class EventsScreen extends StatefulWidget {
  const EventsScreen({Key? key}) : super(key: key);

  @override
  State<EventsScreen> createState() => _EventsScreenState();
}

class _EventsScreenState extends State<EventsScreen> {
  int? lengthOfRequestList; 


  void updateTabName(List<EventRequest> listOfRequests) {
    setState(() {
      lengthOfRequestList = listOfRequests.length;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Parent(
      child: TabbedHeader(
        tabTitles: ['Žiadosti (${int == null ? '...' : int})'],
        title: 'Udalosti',
        tabContents: [
          EventRequestTab(
            onListFetched: updateTabName,
          ) // CHILD COMPONENT WITH LIST OF REQUESTS
        ],
      ),
    );
  }
}

// CHILD WIDGET WITH REQUESTS LIST

class EventRequestTab extends StatefulWidget {
  final Function(List<EventRequest> fetchedList) onListFetched; 
  const EventRequestTab({Key? key, required this.onListFetched}) : super(key: key); 

  @override
  State<EventRequestTab> createState() => _EventRequestState();
}

class _EventRequestState extends State<EventRequestTab> {
  late EventService _eventService;
  late List<EventRequest>? requests;

  @override
  void initState() {
    super.initState();
    _eventService = EventService();
    requests = // FETCHING LIST OF REQUESTS
        widget.onListFetched(requests); 
  }
}

You can pass the list fetched in the initState of your EventRequestTab's state class by passing a callback into the EventRequestTab widget.
This callback will return the list fetched as its parameter and in the EventsScreen, the tab name is updated with the length of the list

Here's an update to your code:


// PARENT WIDGET

class EventsScreen extends StatefulWidget {
  const EventsScreen({Key? key}) : super(key: key);

  @override
  State<EventsScreen> createState() => _EventsScreenState();
}

class _EventsScreenState extends State<EventsScreen> {
  int? lengthOfRequestList; 


  void updateTabName(List<EventRequest> listOfRequests) {
    setState(() {
      lengthOfRequestList = listOfRequests.length;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Parent(
      child: TabbedHeader(
        tabTitles: ['Žiadosti (${int == null ? '...' : int})'],
        title: 'Udalosti',
        tabContents: [
          EventRequestTab(
            onListFetched: updateTabName,
          ) // CHILD COMPONENT WITH LIST OF REQUESTS
        ],
      ),
    );
  }
}

// CHILD WIDGET WITH REQUESTS LIST

class EventRequestTab extends StatefulWidget {
  final Function(List<EventRequest> fetchedList) onListFetched; 
  const EventRequestTab({Key? key, required this.onListFetched}) : super(key: key); 

  @override
  State<EventRequestTab> createState() => _EventRequestState();
}

class _EventRequestState extends State<EventRequestTab> {
  late EventService _eventService;
  late List<EventRequest>? requests;

  @override
  void initState() {
    super.initState();
    _eventService = EventService();
    requests = // FETCHING LIST OF REQUESTS
        widget.onListFetched(requests); 
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文