In Flutter, setState triggers a rebuild of the entire widget tree for the widget where setState is called. However, if you only want to rebuild a specific part of the widget tree, like just the AppBar, you can achieve this by restructuring your widget tree and isolating the AppBar into its own stateful widget.
Here's how you can set up your Flutter app to rebuild only the AppBar when necessary:
Create a Stateful Widget for the AppBar
Define a separate stateful widget for the AppBar. This widget will manage its own state and update only when required.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _appBarTitle = "Initial Title"; void _updateAppBarTitle() { setState(() { _appBarTitle = "Updated Title"; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: MyAppBar(title: _appBarTitle, onUpdateTitle: _updateAppBarTitle), body: Center( child: ElevatedButton( onPressed: _updateAppBarTitle, child: Text('Update AppBar Title'), ), ), ); } } class MyAppBar extends StatefulWidget implements PreferredSizeWidget { final String title; final VoidCallback onUpdateTitle; MyAppBar({required this.title, required this.onUpdateTitle}); @override _MyAppBarState createState() => _MyAppBarState(); @override Size get preferredSize => Size.fromHeight(kToolbarHeight); } class _MyAppBarState extends State<MyAppBar> { @override Widget build(BuildContext context) { return AppBar( title: Text(widget.title), actions: [ IconButton( icon: Icon(Icons.refresh), onPressed: widget.onUpdateTitle, ), ], ); } } AppBar and a button to update the AppBar title.AppBar widget that manages its own state. It receives the title and a callback function to update the title from its parent.State Isolation: By placing AppBar in a separate stateful widget (MyAppBar), you ensure that only the AppBar gets rebuilt when its internal state changes.
PreferredSizeWidget: MyAppBar implements PreferredSizeWidget to define the size of the AppBar.
Callback Function: MyAppBar receives a callback (onUpdateTitle) to notify the parent widget (MyHomePage) when a change is needed.
By isolating the AppBar into its own stateful widget, you can rebuild only that part of the widget tree when needed. This approach helps in managing state efficiently and avoiding unnecessary rebuilds of other parts of the widget tree.
How to update only the AppBar in Flutter using setState?
StatefulWidget to encapsulate the AppBar and update it without affecting the entire widget tree.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _title = 'Initial Title'; void _changeTitle() { setState(() { _title = 'Updated Title'; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(_title), ), body: Center( child: ElevatedButton( onPressed: _changeTitle, child: Text('Change Title'), ), ), ); } } How to use a separate widget for the AppBar to optimize rebuilds in Flutter?
AppBar into a separate StatefulWidget to control its rebuild independently.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _title = 'Initial Title'; void _changeTitle() { setState(() { _title = 'Updated Title'; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: CustomAppBar(title: _title), body: Center( child: ElevatedButton( onPressed: _changeTitle, child: Text('Change Title'), ), ), ); } } class CustomAppBar extends StatefulWidget implements PreferredSizeWidget { final String title; CustomAppBar({required this.title}); @override _CustomAppBarState createState() => _CustomAppBarState(); @override Size get preferredSize => Size.fromHeight(kToolbarHeight); } class _CustomAppBarState extends State<CustomAppBar> { @override Widget build(BuildContext context) { return AppBar( title: Text(widget.title), ); } } How to rebuild only the AppBar and not the whole screen in Flutter?
ValueNotifier and ValueListenableBuilder to listen to changes and rebuild only the AppBar.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { final ValueNotifier<String> _titleNotifier = ValueNotifier<String>('Initial Title'); void _changeTitle() { _titleNotifier.value = 'Updated Title'; } @override Widget build(BuildContext context) { return Scaffold( appBar: ValueListenableBuilder<String>( valueListenable: _titleNotifier, builder: (context, title, child) { return AppBar(title: Text(title)); }, ), body: Center( child: ElevatedButton( onPressed: _changeTitle, child: Text('Change Title'), ), ), ); } } How to use StatefulBuilder to rebuild only the AppBar in Flutter?
StatefulBuilder to manage the state of the AppBar independently from the rest of the widget tree.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _title = 'Initial Title'; void _changeTitle() { setState(() { _title = 'Updated Title'; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: StatefulBuilder( builder: (context, setState) { return AppBar( title: Text(_title), ); }, ), body: Center( child: ElevatedButton( onPressed: _changeTitle, child: Text('Change Title'), ), ), ); } } How to update AppBar without affecting the rest of the widget tree in Flutter?
InheritedWidget to provide and rebuild only the AppBar.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: TitleProvider( child: MyHomePage(), ), ); } } class TitleProvider extends InheritedWidget { final String title; final Function(String) updateTitle; TitleProvider({required this.child, required this.title, required this.updateTitle}) : super(child: child); final Widget child; static TitleProvider? of(BuildContext context) { return context.dependOnInheritedWidgetOfExactType<TitleProvider>(); } @override bool updateShouldNotify(TitleProvider oldWidget) { return title != oldWidget.title; } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { final provider = TitleProvider.of(context)!; return Scaffold( appBar: AppBar( title: Text(provider.title), ), body: Center( child: ElevatedButton( onPressed: () => provider.updateTitle('Updated Title'), child: Text('Change Title'), ), ), ); } } How to rebuild AppBar with animations in Flutter using setState?
AppBar with a smooth transition effect using Flutter's AnimatedSwitcher.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _title = 'Initial Title'; void _changeTitle() { setState(() { _title = 'Updated Title'; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: AnimatedSwitcher( duration: Duration(milliseconds: 300), child: Text(_title, key: ValueKey<String>(_title)), ), ), body: Center( child: ElevatedButton( onPressed: _changeTitle, child: Text('Change Title'), ), ), ); } } How to rebuild only the AppBar using Provider in Flutter?
Provider package to manage and rebuild only the AppBar.import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; void main() => runApp( ChangeNotifierProvider( create: (context) => TitleNotifier(), child: MyApp(), ), ); class TitleNotifier extends ChangeNotifier { String _title = 'Initial Title'; String get title => _title; void updateTitle(String newTitle) { _title = newTitle; notifyListeners(); } } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { final titleNotifier = Provider.of<TitleNotifier>(context); return Scaffold( appBar: AppBar( title: Text(titleNotifier.title), ), body: Center( child: ElevatedButton( onPressed: () => titleNotifier.updateTitle('Updated Title'), child: Text('Change Title'), ), ), ); } } How to use StateNotifier to manage AppBar state in Flutter?
StateNotifier from the flutter_riverpod package to manage and rebuild the AppBar.import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; void main() { runApp(ProviderScope(child: MyApp())); } final titleProvider = StateNotifierProvider<TitleNotifier, String>((ref) { return TitleNotifier(); }); class TitleNotifier extends StateNotifier<String> { TitleNotifier() : super('Initial Title'); void updateTitle(String newTitle) { state = newTitle; } } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final title = ref.watch(titleProvider); return Scaffold( appBar: AppBar( title: Text(title), ), body: Center( child: ElevatedButton( onPressed: () { ref.read(titleProvider.notifier).updateTitle('Updated Title'); }, child: Text('Change Title'), ), ), ); } } How to rebuild AppBar with custom animation on state change in Flutter?
AnimatedBuilder to smoothly transition the AppBar title.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { String _title = 'Initial Title'; late AnimationController _controller; late Animation<double> _animation; @override void initState() { super.initState(); _controller = AnimationController( duration: Duration(milliseconds: 300), vsync: this, ); _animation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller); } void _changeTitle() { setState(() { _controller.forward().then((_) { setState(() { _title = 'Updated Title'; _controller.reset(); }); }); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: AnimatedBuilder( animation: _animation, builder: (context, child) { return Opacity( opacity: _animation.value, child: Text(_title), ); }, ), ), body: Center( child: ElevatedButton( onPressed: _changeTitle, child: Text('Change Title'), ), ), ); } @override void dispose() { _controller.dispose(); super.dispose(); } } How to optimize performance by only rebuilding AppBar in Flutter?
AppBar update strategy using a StatefulWidget and setState for minimal rebuild impact.import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _title = 'Initial Title'; void _changeTitle() { setState(() { _title = 'Updated Title'; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(_title), ), body: Center( child: ElevatedButton( onPressed: _changeTitle, child: Text('Change Title'), ), ), ); } } gatling shopping-cart enoent run-script yii2-advanced-app qt5 minikube goland destroy multi-module