admin管理员组文章数量:1336644
I encountered an issue when I wrapped my MaterialApp
widget in a BlocBuilder
. I expected the entire widget tree to rebuild whenever a new state was emitted. However, I noticed that some widgets didn’t rebuild as expected.
Here’s a simplified example:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<MyCubit, MyState>(
builder: (context, state) {
return MaterialApp(
theme: state.isDarkMode ? ThemeData.dark() : ThemeData.light(),
home: const MyHomePage(),
);
},
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: const Center(child: Text('Hello, world!')),
);
}
}
In this setup:
- When the
isDarkMode
state changes, theMaterialApp
rebuilds with the new theme. - However,
MyHomePage
and its child widgets (e.g.,Text('Hello, world!')
) don’t rebuild, even though I expected the whole tree to respond to the state change.
What I Tried
To solve the problem, I wrapped the specific widgets that didn’t rebuild (e.g., MyHomePage
) in another BlocBuilder
, like this:
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<MyCubit, MyState>(
builder: (context, state) {
return Scaffold(
appBar: AppBar(title: Text(state.isDarkMode ? 'Dark Mode' : 'Light Mode')),
body: Center(child: Text(state.isDarkMode ? 'Dark Mode On' : 'Light Mode On')),
);
},
);
}
}
This approach worked, but now I’m wondering:
- Is the reason some widgets didn’t rebuild initially because they were marked as
const
? - Or is it because
BlocBuilder
only rebuilds its immediate children, and deeper widgets are unaffected? - Is wrapping widgets in multiple
BlocBuilder
s the best practice, or is there a better way to handle this?
I’d appreciate insights or recommendations!
I encountered an issue when I wrapped my MaterialApp
widget in a BlocBuilder
. I expected the entire widget tree to rebuild whenever a new state was emitted. However, I noticed that some widgets didn’t rebuild as expected.
Here’s a simplified example:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<MyCubit, MyState>(
builder: (context, state) {
return MaterialApp(
theme: state.isDarkMode ? ThemeData.dark() : ThemeData.light(),
home: const MyHomePage(),
);
},
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: const Center(child: Text('Hello, world!')),
);
}
}
In this setup:
- When the
isDarkMode
state changes, theMaterialApp
rebuilds with the new theme. - However,
MyHomePage
and its child widgets (e.g.,Text('Hello, world!')
) don’t rebuild, even though I expected the whole tree to respond to the state change.
What I Tried
To solve the problem, I wrapped the specific widgets that didn’t rebuild (e.g., MyHomePage
) in another BlocBuilder
, like this:
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<MyCubit, MyState>(
builder: (context, state) {
return Scaffold(
appBar: AppBar(title: Text(state.isDarkMode ? 'Dark Mode' : 'Light Mode')),
body: Center(child: Text(state.isDarkMode ? 'Dark Mode On' : 'Light Mode On')),
);
},
);
}
}
This approach worked, but now I’m wondering:
- Is the reason some widgets didn’t rebuild initially because they were marked as
const
? - Or is it because
BlocBuilder
only rebuilds its immediate children, and deeper widgets are unaffected? - Is wrapping widgets in multiple
BlocBuilder
s the best practice, or is there a better way to handle this?
I’d appreciate insights or recommendations!
Share Improve this question asked Nov 19, 2024 at 15:52 DOT NET OLD DEVDOT NET OLD DEV 391 silver badge6 bronze badges1 Answer
Reset to default 1Whatever is below BlocBuilder will be rebuild. Solution:: Wrap whatever items requires rebuild with BlocBuilder. There can be more than one BlocBuilder in the same widget tree.
Also you can can control rebuild using buildWhen parameter in blocbuilder
本文标签: flutterDoes Bloc Rebuild (BlocBuilder) The Whole Widget Tree When New State emittedStack Overflow
版权声明:本文标题:flutter - Does Bloc Rebuild (BlocBuilder) The Whole Widget Tree When New State emitted? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742416734a2470859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论