import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class DemoPage extends StatelessWidget { const DemoPage({super.key}); @override Widget build(BuildContext context) { return BlocProvider( create: (ctx) => DependentCounterCubit(), child: const Scaffold( body: DemoView(), ), ); } } class DemoView extends StatelessWidget { const DemoView({super.key}); @override Widget build(BuildContext context) { print('DemoView build, $hashCode'); final state = context.watch().state; return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const CounterText1(), CounterText2('${state.counterTwo}'), const SizedBox.square(dimension: 20), FilledButton( onPressed: context.read().incrementOne, child: const Text('Increment One'), ), FilledButton( onPressed: context.read().incrementTwo, child: const Text('Increment Two'), ), ], ), ); } } // This class is only created for tagging it in the Widget Performance view class CounterText1 extends StatelessWidget { const CounterText1({super.key}); @override Widget build(BuildContext context) { print('CounterText1 build, $hashCode'); final counterText = context.select( (DependentCounterCubit c) => c.state.counterOne, ); return Text( 'Text1: $counterText', style: Theme.of(context).textTheme.titleLarge, ); } } // This class is only created for tagging it in the Widget Performance view class CounterText2 extends StatelessWidget { const CounterText2(this._text, {super.key}); final String _text; @override Widget build(BuildContext context) { print('CounterText2 build, $hashCode'); return Text( 'Text2: $_text', style: Theme.of(context).textTheme.titleLarge, ); } } // CUBIT SAMPLE typedef DependentCounterState = ({int counterOne, int counterTwo}); class DependentCounterCubit extends Cubit { DependentCounterCubit() : super((counterOne: 0, counterTwo: 0)); void incrementOne() { emit((counterOne: state.counterOne + 1, counterTwo: state.counterTwo)); } void incrementTwo() { emit((counterOne: state.counterOne, counterTwo: state.counterTwo + 1)); } }