Flutter - Why Material elevation not working? - flutter

Material elevation in Column not working.
On this code elevation not working.
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
),
body: Column(
children: [
Material(
elevation: 8,
child: Container(
height: 50,
color: Colors.yellowAccent,
),
),
Container(
height: 50,
color: Colors.white,
)
],
),
);
But just remove Container is working. why?
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
),
body: Column(
children: [
Material(
elevation: 8,
child: Container(
height: 50,
color: Colors.yellowAccent,
),
),
],
),
);
has next Container.
remove next Container.
Include SingleChildScrollView and some content message.
It looks like a little shadow appears in the background, but not in the content.
update simple code:
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
elevation: 0,
),
body: Column(
children: [
Material(
elevation: 8,
child: Container(
alignment: Alignment.center,
height: 50,
color: Colors.yellowAccent,
child: const Text('Some title message'),
),
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
for (var i = 0; i < 20; i++)
Container(
alignment: Alignment.center,
height: 100,
color: Colors.white,
// Divider
margin: const EdgeInsets.only(bottom: 8),
child: const Text('Some content'),
),
],
),
))
],
),
)

I think you can use Stack instead of Column if you want the shadow of Material was visible even if you scroll the list. And add transparent Container on the top of the list.
Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: const Text('Test'),
elevation: 0,
),
// Use stack instead of column
body: Stack(
children: [
SingleChildScrollView(
child: Column(
children: [
// Add transparent container
Container(
alignment: Alignment.center,
height: 60,
color: Colors.transparent,
),
for (var i = 0; i < 20; i++)
Container(
alignment: Alignment.center,
height: 100,
color: Colors.white,
// Divider
margin: const EdgeInsets.only(bottom: 8),
child: const Text('Some content'),
),
],
),
),
Material(
elevation: 8,
child: Container(
alignment: Alignment.center,
height: 50,
color: Colors.yellowAccent,
child: const Text('Some title message'),
),
)
],
),
);

Related

Strange Top Padding on my second AppBar Flutter

Screen of the strange top padding of my app
I have made two classes : the first has a first AppBar and calls the second one that has also a sticky AppBar and display a list of scrollable Items. However, I have a strange top padding in the second AppBar. I have tried a lot of stuff but nothing seems to work.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
extendBodyBehindAppBar: true,
bottomNavigationBar: BottomNavBar(0, _currentAccount),
appBar: PreferredSize(
preferredSize:
Size.fromHeight(MediaQuery.of(context).size.height / 7),
child: Column(
children: [
Container(
width: MediaQuery.of(context).size.width,
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: <Color>[
Color(0xFF894c56),
Color(0xFFa80d0d)
])),
child: Padding(
padding: const EdgeInsets.only(bottom: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.only(top: 50, left: 5),
child: Text(
'Votre évenement ',
style: TextStyle(
fontSize: 15, color: Colors.grey),
)),
Padding(
padding: const EdgeInsets.only(top: 5, left: 5),
child: Text(
event,
style: const TextStyle(
color: Colors.white, fontSize: 17),
),
)
])),
)
],
),
),
body: ListPrestat('Traiteur', _currentAccount)
// ListPrestat('Traiteur', _currentAccount)
));
}
This is the code of my first class that calls the ListPrestat class here under :
#override
Widget build(BuildContext context) {
return SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height ,
// -(MediaQuery.of(context).size.height / 6 + 56),
child: CustomScrollView(
slivers: [
SliverAppBar(
toolbarHeight: 50,
expandedHeight: 0,
backgroundColor: const Color(0xFFDCDCDC),
pinned: true,
leading: IconButton(
icon: const Icon(
Icons.arrow_back,
color: Color(0xFFDC3535),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainScreen(_currentAccount)),
);
},
),
centerTitle: true,
shadowColor: Colors.black,
elevation: 10,
bottom: const PreferredSize(
preferredSize: Size.fromHeight(0),
child: TagList(ListCat: 'brouette')),
title: Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.all(MediaQuery.of(context).size.width / 4),
child: Text(
_currentCat,
style: const TextStyle(
color: Colors.black,
fontSize: 16.0,
fontWeight: FontWeight.w700,
),
)),
),
SliverFixedExtentList(
itemExtent: 110.0,
delegate: SliverChildListDelegate(
[
for (int index = 0; index < 5; index++)
CustomListItem2(
thumbnail: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: AspectRatio(
child: Image.network(
growableList[index],
width: 100.0,
height: 100.0,
fit: BoxFit.fill,
),
aspectRatio: 1 / 1,
)),
title: 'Chez Luigi',
subtitle: 'Les meilleurs pizza hors d\'italie.',
ListCat: 'Food Truck',
),
],
),
)
],
),
);
}
Do you have any idea what can have caused this padding and how to resolve this problem ?
wrap your secound class with scaffold
Scaffold(
body: SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height ,),
...................
)

Flutter set max width for AppBar

I want to set the max width of an AppBar like this:
PreferredSize(
preferredSize: Size(500, 200),
child: AppBar(
flexibleSpace: _AppHeader(),
),
)
But this only sets the height. How is is possible to set the width?
Looks like this is an open issue (https://github.com/flutter/flutter/issues/54784).
As suggested there by #imeDevelopers you can use Stack to "hack" an AppBar with max width:
If you use Stack, you also get to decide if the AppBar will scroll with the content or not.
Use this code to keep the AppBar in place even when scrolling (default AppBar behaviour):
const double appBarWidth = 980;
Scaffold(
body: Stack(
children: [
ListView(
shrinkWrap: true,
children: [
Padding(
padding: const EdgeInsets.only(top: 56),
child: Column(
children: [
...[Colors.green, Colors.indigo, Colors.amber].map(
(c) => Container(
color: Colors.red,
child: Center(
child: Container(
height: 1000,
width: appBarWidth,
color: c,
),
),
),
)
],
),
),
],
),
Container(
width: double.infinity,
height: 56,
color: Colors.lime,
),
Positioned(
left: (MediaQuery.of(context).size.width - appBarWidth) / 2,
child: Container(
width: appBarWidth,
child: AppBar(
elevation: 0,
title: const Text('TITLE'),
actions: [
...[1, 2, 3].map(
(i) => TextButton(
style: TextButton.styleFrom(
primary: Theme.of(context).colorScheme.onPrimary),
onPressed: () {},
child: Text('ACTION $i'),
),
),
],
),
),
)
],
),
);
Use this code if you want the AppBar to scroll with the content:
const double appBarWidth = 980;
Scaffold(
body: ListView(
shrinkWrap: true,
children: [
Stack(
children: [
Container(
width: double.infinity,
height: 56,
color: Colors.lime,
),
Positioned(
left: (MediaQuery.of(context).size.width - appBarWidth) / 2,
child: Container(
width: appBarWidth,
child: AppBar(
elevation: 0,
title: const Text('TITLE'),
actions: [
...[1, 2, 3].map(
(i) => TextButton(
style: TextButton.styleFrom(
primary: Theme.of(context).colorScheme.onPrimary),
onPressed: () {},
child: Text('ACTION $i'),
),
),
],
),
),
)
],
),
Column(
children: [
...[Colors.green, Colors.indigo, Colors.amber].map(
(c) => Container(
color: Colors.red,
child: Center(
child: Container(
height: 1000,
width: appBarWidth,
color: c,
),
),
),
)
],
),
],
),
);

How to wrap text in Positioned child?

I am trying to show an image and there will be a text just next to it but my text is so long that it can't fit the screen. Here is my code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey,
appBar: new AppBar(
backgroundColor: Colors.grey,
title: new Align(
alignment: Alignment.center,
child: Text("Demo"),
),
),
body: Stack(
children: [
Positioned(
top: 50,
left: 25,
child: Image(
image: NetworkImage(
"https://reimg-teknosa-cloud-prod.mncdn.com/mnresize/200/200/productimage/125036758/125036758_0_MC/48543732.jpg"),
height: 300,
width: 200,
fit: BoxFit.fitWidth,
),
),
Container(
child: Positioned(
top: 70,
left: 260,
child: Text(
"Durum: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaB",
style: GoogleFonts.sourceSerifPro(
fontSize: 20,
),
),
),
)
],
),
);
}
}
I tried overflow: TextOverflow.clip, did not work and I searched about Flexible and Expended but I could not place them in my code. I tried to place them instead of Container but that did not work. How can I make my text continue to down line if it won't fit? Thanks in advance.
Change your code like this:
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blueGrey,
appBar: new AppBar(
backgroundColor: Colors.grey,
title: new Align(
alignment: Alignment.center,
child: Text("Demo"),
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: Center(
child: Text(
"Durum: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaB",
style: GoogleFonts.sourceSerifPro(
fontSize: 20,
),
),
),
),
Image(
image: NetworkImage(
"https://reimg-teknosa-cloud-prod.mncdn.com/mnresize/200/200/productimage/125036758/125036758_0_MC/48543732.jpg"),
height: 300,
width: 200,
fit: BoxFit.fitWidth,
),
],
),
);
}
}
You can add this in your Text widget softWrap: true.

How to make a Card lies between Body in flutter

Anybody how to do the layout similar to the image? I am having difficulty on the CardView and it position is lies between the body. I am still consider quite new to Flutter, having some difficulty in UI part. Full code is as below:
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: PreferredSize(
preferredSize: Size.fromHeight(45.0),
child: AppBar(
automaticallyImplyLeading: false,
elevation: 0.0,
centerTitle: true,
backgroundColor: Color(0xFFED2849),
leading: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
margin: const EdgeInsets.only(left: 8.0),
child: GestureDetector(
onTap: (){
Navigator.of(context).pop(null);
},
child: Image.asset('assets/ic_user_title_back.png', width: 12.0, height: 12.0,),
),
),
],
),
title: Center(
child: Container(
child: Text(
'账号中心',
style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 14),
),
),
),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: GestureDetector(
onTap: (){
//logout
},
child: Text(
'退出登陆',
style: TextStyle(color: Color(0xFFFFFFFF), fontSize: 11),
),
),
),
],
),
)
],
)
),
body: SafeArea(
top: false,
child: Material(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
flex: 3,
child: Container(
color: Color(0xFFED2849),
),
),
Expanded(
flex: 7,
child: Container(
color: Color(0xFFF1F1F1),
),
)
],
),
),
)
);
}
You can do this using Stack
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
// fit: StackFit.expand,
children: [
Column(
children: [
Expanded(
flex: 3,
child : Container(color: Colors.red)
),
Expanded(
flex: 7,
child: Container(
color : Colors.white)
)
]
),
Positioned(
top: MediaQuery.of(context).size.height * 0.2,
left: 20,
right: 20,
child:Card(
child: Padding(
padding : const EdgeInsets.all(16),
child: Text("Your Text here", ),
),)
)
],
)
Try using Stack() it will allow you to overlap several children in a simple way.

The 'body' part is not visible after using ' bottomNavigationBar' in Flutter

Whenever i use the bottomNavigationBar: it dose not show the body: part on screen but when i remove the bottomNavigationBar: then it shows the body:
Here is the code
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home', textAlign: TextAlign.center),
actions: <Widget>[],
backgroundColor: Color(0xffd81b60),
),
bottomNavigationBar: _getNavBar(context),
body: ListView(children: <Widget>[
SizedBox(height: 300.0),
Container(
height: 50,
width: 10,
child: Center(
child: RaisedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => mealwisePage()));
},
color: Colors.pink,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Meal Wise',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 20, color: Colors.white),
),), ), ), ), ]),);}
_getNavBar(context) {
return Stack(
children: <Widget>[
Positioned(
bottom: 0,
child: ClipPath(
clipper: NavBarClipper(),
child: Container(
height: 50,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.pink,
Colors.pink.shade800,
])), ),),),],);}
No error is showing just body is invisible on the screen
Any Solution Please?
I fount that it is because of using Stack it overlaps with body so i changed it
from:
return Stack(
children: <Widget>[
Positioned(
bottom: 0,
child: ClipPath(
clipper: NavBarClipper(),
child: Container(),),)],)
To
return Container(
height: 90,
child: Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Positioned(
bottom: 0,
child: ClipPath(
clipper: NavBarClipper(),
child: Container(),),),],),)
bottom navigation is part of scaffold constructor Widget bottomNavigationBar and not the body hence refactor your code as shown below
Scaffold(
appBar: AppBar(
title: const Text('Home', textAlign: TextAlign.center),
backgroundColor: Color(0xffd81b60),
),
resizeToAvoidBottomPadding: false,
body: ListView(children: <Widget>[
Container(
child:Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(15.0, 40.0, 40.0, 25.0),
child: Center(
child: Text(
'Home',
textAlign: TextAlign.center,
),),), ], ),),
SizedBox(height: 200.0),
]),//listview
bottomNavigationBar: _getNavBar(context),
);//scaffold
update edit
child: Container(
height: 50,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
you must give a value to this width: MediaQuery.of(context).size.width,
example width: MediaQuery.of(context).size.width * .98,