Search code examples
flutterbloc

Opening a text field causes the parrent widget to rebuild


my app is structured like this:

    class MainPage extends StatefulWidget {
  MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    BlocProvider.of<NavigationBloc>(context).add(const DashboardEvent());
    int _selectedIndex = 0;
    return Scaffold(
      appBar: AppBar(
        elevation: 0,

The Main Page is responsible for navigation via the navigation bloc. Different buttons add different events depending on what page I want to show. Up to this point everything was working normally but all of sudden when I open any TextField in my app, it causes this main page to rebuild, calling the DashboardEvent and the page with the text field disappear.

I know that opening a TextField causes flutter to call the build method, but up to this point, in only caused the page to rebuild, not the parent widget. I have no idea why is it behaving like this all of sudden. Is there any setting I could toggle that I don't remember that could cause this behavior ? Thank you


Solution

  •     class MainPage extends StatefulWidget {
      MainPage({Key? key}) : super(key: key);
    
      @override
      State<MainPage> createState() => _MainPageState();
    }
    
    class _MainPageState extends State<MainPage> {
    @override  
    initState(){
        BlocProvider.of<NavigationBloc>(context).add(const DashboardEvent());
        int _selectedIndex = 0;
        super.initState();
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            elevation: 0,