I have a SliverAppBar and i am trying to position three items in a row:
User Profile Picture
User Account Information
Notification bar widget
But whenever i try to add the User Account Information Widget in the row i either get a render flex error or it gets positions incorrectly and get out of sight.
This is what i've tried and i will give the code snippets my SliverAppBar.
Using a flexibleSpace with a FlexibleSpaceBar() widget and wrapping the title in a row where i then instantiate the 3 widgets.
HomeAccountPicture(),
HomeAccountDetails(),
NotificationsButtonWidget(),
as the child.
I made sure the respective widgets are just wrapped in 'Containers()`
This is my Sliver App Snippet
class HomeView extends GetView<HomeController> {
#override
Widget build(BuildContext context) {
controller.initScrollController();
return WillPopScope(
onWillPop: Helper().onWillPop,
child: Scaffold(
body: RefreshIndicator(
onRefresh: () async {
Get.find<LaravelApiClient>().forceRefresh();
controller.refreshHome(showMessage: true);
Get.find<LaravelApiClient>().unForceRefresh();
},
child: CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
controller: controller.scrollController,
shrinkWrap: false,
slivers: <Widget>[
SliverAppBar(
backgroundColor: Color(0xffFFFFFF),
expandedHeight: MediaQuery.of(context).size.height * 0.25,
elevation: 0.5,
//pinned: true,
floating: false,
iconTheme: IconThemeData(color: Get.theme.primaryColor),
centerTitle: true,
flexibleSpace: FlexibleSpaceBar(
title: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
HomeAccountPicture(),
HomeAccountDetails(),
NotificationsButtonWidget(),
],
),
),
automaticallyImplyLeading: false,
),
SliverToBoxAdapter(
child: Wrap(
children: [
BookingsListWidget(),
],
),
),
],
)),
),
);
}
}
This is my expectation
This is what i get
I have two scaffold widget in the stack for some purposes .. And every scaffold has its own contents .
the second scaffold has transparent background colors so the first scaffold is visible.
Stack(
children: [
Scaffold(
body: GestureDetector(
onTap: () {},
child: myBody(),
),
),
Scaffold(
backgroundColor: Colors.transparent,
body: ListView.builder(
itemBuilder: (context, index) => ...,
),
)
],
),
the GestureDetector in first Scaffolddoes not work and that's because of the Scaffold stack
Note : I can't wrap the second Scaffold with IgnorePointer because it has clickable ListView.bulder which gonna be ignoring any pointer too
How could I solve this ×_O
You can get callback method from second scaffold list item tap. And create a function on level that will be provided on first scaffold GestureDetector.
void main(List<String> args) {
Widget myBody() {
return Container(
color: Colors.cyanAccent.withOpacity(.3),
);
}
void topLevelGesture() {
debugPrint("got hit on GestureDetector");
}
runApp(
MaterialApp(
home: Stack(
children: [
Scaffold(
body: GestureDetector(
onTap: topLevelGesture,
child: myBody(),
),
),
Scaffold(
backgroundColor: Colors.transparent,
body: ListView.builder(
itemBuilder: (context, index) => ListTile(
title: Text("item $index"),
onTap: () {
topLevelGesture();
print("tapped $index");
},
),
),
),
],
),
),
);
}
You can set Gesture as outside of the stack and with this inner list click also works but when you put the first Scaffold body as a clickable it's not working because the second Scaffold overlay that.
GestureDetector(
onTap(){}
child:Stack(
children[
Scaffold(
body: myBody()
)
Scaffold(
backGroundColor: Colors.transparent
body: ListView.bulder(){....}
)
]
))
You need to add clickable widget at the end in your stack widget as below:
Stack(
children[
firstWidget(),
GestureDetector(
onTap(){},
child: secondWidget()
)
]
)
I wanted to show text from a string variable before the Wrap widget. But when I wanted to add something like Text before Wrap widget it is giving me error. How can show some text before the Wrap widget.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Second Route"),
),
body: Center(
child:
Wrap(
children: _buildButtonsWithNames(),
),
)
);
}
You can give column as a child to the center widget like this:
body: Center(
child: Column(
children: [
Text("Your Text"),
Wrap(
children: _buildButtonsWithNames(),
),
],
),
),
Hope it answers the question.
If you want the text to be on top of the Wrap, add your Wrap to a Column and before of it add your Text widget.
If you want the text to be just before the Wrap, add your Wrap to a Row widget and do the same as for Column.
Just found a way right now!!! from another answer.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Second Route"),
),
body: Column(
children: <Widget>[
Text("He"),
Expanded(
child: Center(
child: Wrap(
children: _buildButtonsWithNames(),
),
),
)
],
),
);
}
}
Just use the column widget and pass the text and wrap widgets as children of the column
I got my page layout like this:
return new Scaffold(
appBar: _getAppBarWidget(_currentIndex),
body: return CustomScrollView(slivers: [
SliverPersistentHeader(
delegate: ProfileBar(expandedHeight: 200),
pinned: true,
),
SliverToBoxAdapter(
child: ProfileView()
),
]);,
bottomNavigationBar: BottomBar(),
);
and I want my ProfileView to take the whole rest of the screensize.
ProfileView() looks like this:
return Container(
color: Colors.green;
child: RaisedButton(
child: const Text('Something'),
textColor: Colors.white,
color: Colors.redAccent,
onPressed: () {}
)
);
But I just can't get my Container from ProfileView to take the whole rest of the screenheight that is remaining below the SliverPersistentHeader.
I tried a lot of different things but didn't succeed. (e.g. using Expended) Someone has an advice for this?
Edit: The only thing I succeeded in was hardcoding it. But this is not what I want.
To solve the issue you can use SliverFillRemaining instead of SliverToBoxAdapter
So your updated code will be
return new Scaffold(
appBar: _getAppBarWidget(_currentIndex),
body: return CustomScrollView(slivers: [
SliverPersistentHeader(
delegate: ProfileBar(expandedHeight: 200),
pinned: true,
),
SliverFillRemaining(
child: ProfileView()
),
]);,
bottomNavigationBar: BottomBar(),
);
I'm trying to center the title text in an app bar that has both a leading and trailing actions.
#override
Widget build(BuildContext context) {
final menuButton = new PopupMenuButton<int>(
onSelected: (int i) {},
itemBuilder: (BuildContext ctx) {},
child: new Icon(
Icons.dashboard,
),
);
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that
// was created by the App.build method, and use it to set
// our appbar title.
title: new Text(widget.title, textAlign: TextAlign.center),
leading: new IconButton(
icon: new Icon(Icons.accessibility),
onPressed: () {},
),
actions: [
menuButton,
],
),
body: new Center(
child: new Text(
'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
This works well except the title is aligned on the left as is shown in this picture:
As I try to include the title in the center, it appears that it's too much to the left:
#override
Widget build(BuildContext context) {
final menuButton = new PopupMenuButton<int>(
onSelected: (int i) {},
itemBuilder: (BuildContext ctx) {},
child: new Icon(
Icons.dashboard,
),
);
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that
// was created by the App.build method, and use it to set
// our appbar title.
title: new Center(child: new Text(widget.title, textAlign: TextAlign.center)),
leading: new IconButton(
icon: new Icon(Icons.accessibility),
onPressed: () {},
),
actions: [
menuButton,
],
),
body: new Center(
child: new Text(
'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
),
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
I would love a solution to get the title text centered perfectly between the 2 icons.
Centering the title is the default on iOS. On Android, the AppBar's title defaults to left-aligned, but you can override it by passing centerTitle: true as an argument to the AppBar constructor.
Example:
AppBar(
centerTitle: true, // this is all you need
...
)
I had the same problem and it finally worked when I added the
mainAxisSize: MainAxisSize.min to my Row() widget:
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that
// was created by the App.build method, and use it to set
// our appbar title.
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
widget.title,
),
],
),
leading: new IconButton(
icon: new Icon(Icons.accessibility),
onPressed: () {},
),
actions: [
menuButton,
],
),
);
}
In my case I wanted to have a logo / image centered instead of a text. In this case, centerTitle is not enough (not sure why, I have an svg file, maybe that's the reason... ), so for example, this:
appBar: AppBar(centerTitle: true, title: AppImages.logoSvg)
will not really center the image (plus the image can be too big, etc.). What works well for me is a code like this:
appBar: AppBar(centerTitle: true,
title: ConstrainedBox(
constraints: BoxConstraints(maxHeight: 35, maxWidth: 200),
child: AppImages.logoSvg)),
You can just use the centerTitle property in the appBar section to center your title
using:
centerTitle: true
Here is how I make centerTitle on my appbar:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
centerTitle: true ,
title: new Text("Login"),
),
body: new Container(
padding: EdgeInsets.all(18.0),
key: formkey,
child: ListView(
children: buildInputs() + buildSubmitButton(),
),
)
);
}
Here is a different approach if you want to create a custom app bar title. For example you want an image and a text at the center of app bar then add
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.your_app_icon,
color: Colors.green[500],
),
Container(
padding: const EdgeInsets.all(8.0), child: Text('YourAppTitle'))
],
),
)
Here we have created a Row with MainAxisAlignment.center to center the children. Then we have added two children - An Icon and a Container with text. I wrapped Text widget in the Container to add the necessary padding.
You can center the title of an appBar by using centerTitle parameter.
centerTitle is Boolean Datatype, and default value is False.
centerTitle : true
Example :
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('App Title'),
backgroundColor: Colors.red,
centerTitle: true,
),
),
),
);
}
Yeah but in my case i used centertitle as well as axis alignment then it made it centre , if i am using only onw of it then it is is not making it centre , here is how i am doing it :
import 'package:flutter/material.dart';
import 'package:infintywalls/widgets/widget.dart';
class Home extends StatefulWidget {
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
appName(),
],
),
elevation: 0.0,
centerTitle: true,
),
);
}
}
and yeah btw appName() is my custom widget not a default builtin one.
home this is helpful to you
thanks
appBar has its own bool condition for title center show or not,
so,
if you set true,
appBar: AppBar(
title: Text(
"header Text",
style: TextStyle(color: Colors.black),
),
centerTitle: true,
),
then it will be centre,other then its default left align (in android)
ios set center(in default).
Try the following code:
AppBar(
centerTitle: true,
…
),
It can be done by using Center class.
appBar: AppBar(
title: const Center(
child: Text("I Am Rich"),
),
),
After trying many solutions this helped me centerTitle: true
adding sample code in addition to #Collin Jackson answer
Example
in build(BuildContext context)
do this
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),centerTitle: true
),
appbar:AppBar(
centerTitle: true,
title:Text("HELLO")
)
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
actions: <Widget> [
IconButton(icon: const Icon(Icons.file_upload), onPressed: _pressed),
],
leading: IconButton(icon: const Icon(Icons.list), onPressed: _pressed),
centerTitle: true,
)
body: Text("Content"),
);
}
This can help in making Appbar Title Text in Center.
You can choose to add your desired Styles using Style or comment it if not needed.
appBar: AppBar(
title: const Center(
child: Text(
"App Title",
style: TextStyle( color: Colors.white,fontSize: 20),
),
),
),
On App Display:
It my case this code works:-
appBar: AppBar(
centerTitle: true,
elevation: 2,
title: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text(" TINKLE"),
)
],
),
),
),
Hope this was helpful.
Use Center object
appBar: AppBar(
title: Center(
child: const Text('Title Centered')
)
)