在我的应用程序中,我有两个事件,第一个事件是通过bloc提供程序应用的,并且它返回某个状态。.我需要在屏幕上创建一个按钮来调用第二个事件,并且状态会发生变化,以便屏幕显示。 这是我的尝试,但是我得到了……BlocProvider.of()在不包含Bloc的上下文中调用
class Projects extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => AllProjectsBloc()..add(FetchAllProjectsData()),
child: SafeArea(
child: Scaffold(
body: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(width: 120, height: 90, child: TextField()),
IconButton(
icon: Icon(Icons.search),
onPressed: () {
BlocProvider.of<AllProjectsBloc>(context)
.add(FetchFilteredProjectsData());
},
)
],
),
BlocConsumer<AllProjectsBloc, AllProjectsState>(
builder: (context, state) {
if (state is AllProjectsLoadingState) {
return Center(
child: CircularProgressIndicator(),
);
} else if (state is AllProjectsLoadedState) {
return Expanded(
child: ListView.builder(
itemCount: state.allProjectsData.length,
itemBuilder: (context, i) {
return TopViewData(
title: state.allProjectsData[i].title,
imageUrl: state.allProjectsData[i].imageUrl,
id: state.allProjectsData[i].id,
onPress: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailedProject(
state.allProjectsData[i].title)));
},
);
},
));
} else if (state is FilteredProjectsLoadedState) {
return Expanded(
child: ListView.builder(
itemCount: state.filteredProjectsData.length,
itemBuilder: (context, i) {
return TopViewData(
title: state.filteredProjectsData[i].title,
imageUrl: state.filteredProjectsData[i].imageUrl,
id: state.filteredProjectsData[i].id,
onPress: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailedProject(
state.filteredProjectsData[i].title)));
},
);
},
));
}