flutter - Does Bloc Rebuild (BlocBuilder) The Whole Widget Tree When New State emitted? - Stack Overflow

I encountered an issue when I wrapped my MaterialApp widget in a BlocBuilder. I expected the entire wid

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, the MaterialApp 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:

  1. Is the reason some widgets didn’t rebuild initially because they were marked as const?
  2. Or is it because BlocBuilder only rebuilds its immediate children, and deeper widgets are unaffected?
  3. Is wrapping widgets in multiple BlocBuilders 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, the MaterialApp 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:

  1. Is the reason some widgets didn’t rebuild initially because they were marked as const?
  2. Or is it because BlocBuilder only rebuilds its immediate children, and deeper widgets are unaffected?
  3. Is wrapping widgets in multiple BlocBuilders 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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Whatever 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742416735a4439885.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信