使用PageView构建器的Flutter Bloc Pattern跳过RadioListTile选择

时间:2020-10-27 10:42:21

标签: flutter dart

创建提供建议集的PageView,就像第1集节目一样(香蕉,建议['fruits','vegetables'],第二个Potato提出['vegetables'或可能有所不同])。它是通过向右/向左滑动显示的,在单个屏幕上建议banana,如果向左滑动则显示potato

问题:未选择收音机。如果我来回移动,单选按钮列表将反映所选的值,而不是在选择时显示

Widget



  @override
  Widget build(BuildContext context) {
    final GlobalBloc _globalBloc = Provider.of<GlobalBloc>(context);
    _globalBloc.suggestionBloc.fetchRecords();
    PageController controller = PageController();
    return Scaffold(
      appBar: AppBar(
        titleSpacing: 0,
        title: Text("Songs Suggestion"),
        elevation: 1,
        backgroundColor: Theme.of(context).accentColor,
        actions: <Widget>[
          IconButton(icon: const Icon(Icons.refresh), tooltip: 'Refresh', onPressed: () => {}),
        ],
      ),
      body: StreamBuilder<List<SuggestionTagSong>>(
        stream: _globalBloc.suggestionBloc.recordTagToSong$,
        builder: (BuildContext context, AsyncSnapshot<List<SuggestionTagSong>> snapshot) {
          if (!snapshot.hasData) return LoadingWidget();
          final List<SuggestionTagSong> recordList = snapshot.data;
          if (recordList.length == 0) return EmptyScreenV2();
          return PageView.builder(
            key: PageStorageKey<String>("tags-song"),
            controller: controller,
            pageSnapping: false,
            physics: PageScrollPhysics(),
            itemCount: recordList.length,
            itemBuilder: (context, index) {
              return Container(
                child: Column(
                  children: [
                    Expanded(
                      child: recordList[index].songSuggestion.length == 0
                          ? Container(child: EmptyScreenV2())
                          : ListView.builder(
                              itemCount: recordList[index].songSuggestion.length,
                              itemBuilder: (BuildContext context, int i) {
                                return Container(
                                  child: RadioListTile(
                                    groupValue: _globalBloc.suggestionBloc.selectedSong$.value,
                                    title: Text(recordList[index].songSuggestion[i].name),
                                    activeColor: Theme.of(context).primaryColor,
                                    value: recordList[index].songSuggestion[i].id,
                                    onChanged: (String val) => _globalBloc.suggestionBloc.selectSong(val),
                                  ),
                                );
                              },
                            ),
                    ),
                  ],
                ),
              );
            },
          );
        },
      ),
    );
  }

Sample Bloc Class

class SuggestionBloc {
  BehaviorSubject<String> _selectedSong$;

  BehaviorSubject<String> get selectedSong$ => _selectedSong$;


  selectSong(String recordId) {
    _selectedSong$.add(recordId);
    print("----");
    print(_selectedSong$.value == recordId);
    print(_selectedSong$.value);
    print(recordId);
  }
}

0 个答案:

没有答案