LocalHistoryEntry class
An entry in the history of a LocalHistoryRoute.
A LocalHistoryEntry represents a "mini" navigation state within a route. It allows widgets or UI components to handle the back button or pop operations locally without affecting the main navigator stack.
It is typically used for widgets such as dialogs, bottom sheets, or inline expandable panels that can be dismissed independently of the surrounding route.
When a local history entry is removed (e.g., via the back button), the onRemove callback is called first. Only after all local history entries have been removed will the route itself be popped.
This sample demonstrates how to use a LocalHistoryEntry to show a panel that can be
dismissed with the back button.
link
To create a local project with this code sample, run:
flutter create --sample=widgets.LocalHistoryEntry.1 mysample
import 'package:flutter/material.dart';
/// Flutter code sample for [LocalHistoryEntry].
void main() {
runApp(PanelDemo());
}
class PanelDemo extends StatefulWidget {
const PanelDemo({super.key});
@override
State<PanelDemo> createState() => _PanelDemoState();
}
class _PanelDemoState extends State<PanelDemo> {
bool _isPanelOpen = false;
LocalHistoryEntry? _entry;
void _openPanel() {
if (_isPanelOpen) {
return;
}
_entry = LocalHistoryEntry(
onRemove: () {
setState(() {
_isPanelOpen = false;
_entry = null;
});
},
);
ModalRoute.of(context)?.addLocalHistoryEntry(_entry!);
setState(() {
_isPanelOpen = true;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('LocalHistoryEntry Example')),
body: Stack(
children: <Widget>[
Center(
child: ElevatedButton(
onPressed: _openPanel,
child: const Text('Open Panel'),
),
),
if (_isPanelOpen)
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 200,
color: Colors.blueAccent,
child: const Center(
child: Text(
'Press back to close this panel',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
),
],
),
),
);
}
}
See also:
- LocalHistoryRoute, which manages a stack of local history entries.
- ModalRoute.addLocalHistoryEntry, which adds an entry to a route.
- showModalBottomSheet, which internally uses local history entries to handle back button behavior.
Constructors
- LocalHistoryEntry({VoidCallback? onRemove, bool impliesAppBarDismissal = true})
- Creates an entry in the history of a LocalHistoryRoute.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- impliesAppBarDismissal → bool
-
Whether an AppBar in the route this entry belongs to should
automatically add a back button or close button.
final
- onRemove → VoidCallback?
-
Called when this entry is removed from the history of its associated LocalHistoryRoute.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
remove(
) → void - Remove this entry from the history of its associated LocalHistoryRoute.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited