flutter_bloc library
flutter_bloc library
https://pub.flutter-io.cn/documentation/flutter_bloc/latest/flutter_bloc/BlocBuilder-class.html
Classes
Bloc<Event, State>
{@template bloc} Takes a Stream of Events as input and transforms them into a Stream of States as output. {@endtemplate}
BlocBuilder<C extends Cubit<S>, S>
BlocBuilder handles building a widget in response to new states. BlocBuilder is analogous to StreamBuilder but has simplified API to reduce the amount of boilerplate code needed as well as cubit-specific performance improvements. Please refer to BlocListener if you want to "do" anything in response to state changes such as navigation, showing a dialog, etc... [...]
BlocBuilderBase<C extends Cubit<S>, S>
Base class for widgets that build themselves based on interaction with a specified cubit. [...]
BlocConsumer<C extends Cubit<S>, S>
BlocConsumer exposes a builder and listener in order react to new states. BlocConsumer is analogous to a nested BlocListener and BlocBuilder but reduces the amount of boilerplate needed. BlocConsumer should only be used when it is necessary to both rebuild UI and execute other reactions to state changes in the cubit. [...]
BlocListener<C extends Cubit<S>, S>
Takes a BlocWidgetListener and an optional cubit and invokes the listener in response to state changes in the cubit. It should be used for functionality that needs to occur only in response to a state change such as navigation, showing a SnackBar, showing a Dialog, etc... The listener is guaranteed to only be called once for each state change unlike the builder in BlocBuilder. [...]
BlocListenerBase<C extends Cubit<S>, S>
Base class for widgets that listen to state changes in a specified cubit. [...]
An interface for observing the behavior of Bloc and Cubit instances.
BlocProvider<T extends Cubit<Object>>[[class ThemeCubit extends Cubit<ThemeData>]]
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => ThemeCubit(),
child: BlocBuilder<ThemeCubit, ThemeData>(
builder: (_, theme) {
return MaterialApp(
theme: theme,
home: BlocProvider(
create: (_) => CounterBloc(),
child: CounterPage(),
),
);
},
),
);
}
}
/// {@template brightness_cubit}
/// A simple [Cubit] which manages the [ThemeData] as its state.
/// {@endtemplate}
class ThemeCubit extends Cubit<ThemeData> {
/// {@macro brightness_cubit}
ThemeCubit() : super(_lightTheme);
static final _lightTheme = ThemeData(
floatingActionButtonTheme: const FloatingActionButtonThemeData(
foregroundColor: Colors.white,
),
brightness: Brightness.light,
);
static final _darkTheme = ThemeData(
floatingActionButtonTheme: const FloatingActionButtonThemeData(
foregroundColor: Colors.black,
),
brightness: Brightness.dark,
);
/// Toggles the current brightness between light and dark.
void toggleTheme() {
emit(state.brightness == Brightness.dark ? _lightTheme : _darkTheme);
}
}
Takes a ValueBuilder that is responsible for creating the bloc and a child which will have access to the bloc via BlocProvider.of(context). It is used as a dependency injection (DI) widget so that a single instance of a bloc can be provided to multiple widgets within a subtree. [...]
Change<State>
{@template change} A Change represents the change from one State to another. A Change consists of the currentState and nextState. {@endtemplate}
Cubit<State>
{@template cubit} A Cubit is a subset of Bloc which has no notion of events and relies on methods to emit new states. [...]
Merges multiple BlocListener widgets into one widget tree. [...]
Merges multiple BlocProvider widgets into one widget tree. [...]
Merges multiple RepositoryProvider widgets into one widget tree. [...]
Takes a ValueBuilder that is responsible for creating the repository and a child which will have access to the repository via RepositoryProvider.of(context). It is used as a dependency injection (DI) widget so that a single instance of a repository can be provided to multiple widgets within a subtree. [...]
Transition<Event, State>
{@template transition} Occurs when an event is added after mapEventToState has been called but before the bloc's State has been updated. A Transition consists of the currentState, the event which was added, and the nextState. {@endtemplate}
Extensions
Extends the BuildContext class with the ability to perform a lookup based on a Bloc type.
Extends the BuildContext class with the ability to perform a lookup based on a repository type.
Typedefs
BlocBuilderCondition<S>(S previous, S current) → bool
Signature for the buildWhen function which takes the previous state and the current state and is responsible for returning a bool which determines whether to rebuild BlocBuilder with the current state.
BlocListenerCondition<S>(S previous, S current) → bool
Signature for the listenWhen function which takes the previous state and the current state and is responsible for returning a bool which determines whether or not to call BlocWidgetListener of BlocListener with the current state.
BlocWidgetBuilder<S>(BuildContext context, S state) → Widget
Signature for the builder function which takes the BuildContext and state and is responsible for returning a widget which is to be rendered. This is analogous to the builder function in StreamBuilder.
BlocWidgetListener<S>(BuildContext context, S state) → void
Signature for the listener function which takes the BuildContext along with the state and is responsible for executing in response to state changes.
CreateBloc<T extends Cubit>(BuildContext context) → T
A function that creates a Bloc of type T.
TransitionFunction<Event, State>(Event) → Stream<Transition<Event, State>>
Signature for a mapper function which takes an Event as input and outputs a Stream of Transition objects.
Exceptions / Errors
{@template cubit_unhandled_error_exception} Exception thrown when an unhandled error occurs within a Cubit. [...]
更多推荐



所有评论(0)