Search code examples
widgetflutterappbar

How to change AppBar content using children widget?


I am trying to change the AppBar title to "Log In" from LoginWidget level, but I don't know how to do this.

It may looks like IoC pattern, but I think it should still be possible to do.

I am now trying to extend a Scaffold object and add some methods to it, but it seems to not work.

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
            backgroundColor: Colors.amber,
            title: new Text("Application name")
        ),
        body: new LoginWidget()
      )
    );
  }
}

Solution

  • This is impossible

    Widgets cannot modify other widgets in any way. The only thing they can do is submit events for them to potentially react to.

    Only MyApp can change the appbar.