How to position linear progress bar at appbar bottom without using the bottom property of the appbar and without inscreasing the appbar height?
like the following image: Material design appbar
For determinate progress indicator we will display a button, pressing on which will show the progress. We will create a variable value whose value is initially 0. double value = 0; We will assign this variable to value property of linear progress indicator.
Firstly declare the AppBar widget that you will use in your Scaffold. Now you can get the height of your appBar using its preferred size: double height = appBar.
You can use BoxDecoration to add border radius and shadow to a Container/DecoratedBox. You can also create a custom widget using below code snippet. In Flutter, you can have a custom shape in the AppBar Widget with shape property. AppBar( title: Text('My App'), shape: RoundedRectangleBorder( borderRadius: BorderRadius.
Why you don't want to use the bottom property? That is the hook the Flutter AppBar
widget provides to add things there. Otherwise you have to create your own version of the AppBar
.
In case it is useful to you, I have created the snippet below that you can use in your appbar like this.
appBar: new AppBar(
title: new Text("Title"),
backgroundColor: Colors.orange,
bottom: MyLinearProgressIndicator(
backgroundColor: Colors.orange,
),
),
MyLinearProgressIndicator
must implement the preferredSize
getter. That is why you need to create your own version.
// Cant't use _kLinearProgressIndicatorHeight 'cause it is private in the
// progress_indicator.dart file
const double _kMyLinearProgressIndicatorHeight = 6.0;
class MyLinearProgressIndicator extends LinearProgressIndicator
implements PreferredSizeWidget {
MyLinearProgressIndicator({
Key key,
double value,
Color backgroundColor,
Animation<Color> valueColor,
}) : super(
key: key,
value: value,
backgroundColor: backgroundColor,
valueColor: valueColor,
) {
preferredSize = Size(double.infinity, _kMyLinearProgressIndicatorHeight);
}
@override
Size preferredSize;
}
And this is the result:
@chemamolins's answer is perfectly valid, but it might be easier just to wrap your widget in a Preferred Size widget. The widget takes a child
and a preferredSize
of type Size
Here's an example from an app I'm working on, wrapping a StreamBuilder:
return Scaffold(
appBar: AppBar(
bottom: PreferredSize(
preferredSize: Size(double.infinity, 1.0),
child: ProgressBar(widget.bloc.isLoading),
),
---snip---
class ProgressBar extends StatelessWidget {
final Stream<bool> _isLoading;
ProgressBar(this._isLoading);
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: _isLoading,
builder: (context, snapshot) {
if (snapshot.hasData && snapshot.data) {
return LinearProgressIndicator();
}
else {
return Container();
}
}
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With