How to add divider & Subtitle in drawer in flutter - flutter

I would like to add Subtitle and divider in navigation drawer like below image screen shot.
I have written below code for navigation drawer but not able to find where i can put Subtitle & divider.
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'Drawer Header',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.message),
title: Text('Messages'),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
),

there is a widget for it called Divider
you could simply add a Divider as one of the childrens of the ListView
for the subtitle you could add a Text widget and style it to look how you need it to look using a TextStyle
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const <Widget>[
DrawerHeader(...),
Divider(), //here is a divider
Text("subtitle", style: TextStyle(...)),
ListTile(...),
ListTile(...),
Divider(), //here is a divider
ListTile(...),
//rest of your items
],
),
),

You can do it with Divider wigdet https://api.flutter.dev/flutter/material/Divider-class.html

Just Provide a Divider() within every ListTile where you wanna put that thin line on.

I had the same problem and solved it as follows:
class DividerWithSubheader extends StatelessWidget {
const DividerWithSubheader({Key? key, required this.subHeader,}) : super(key: key);
final String subHeader;
#override
Widget build(BuildContext context) {
return Stack(
children: [
Divider(),
Container(
padding: const EdgeInsets.only(left: 20),
child: Align(
alignment: AlignmentDirectional.centerStart,
child: Text(
subHeader,
style: Theme.of(context).textTheme.caption,
textAlign: TextAlign.start,
),
),
),
],
);
}}
And use it like this:
ListView(children: <Widget>[
DividerWithSubheader(subHeader: 'subheader Test'),
]),

Related

How to align the child of a column on the bottom while keeping other children's position at the center in Flutter?

I want to align the text "2022" on the bottom of the screen while keeping the image and its neighbor text at the center of the screen.
class SplashScreen extends StatelessWidget {
const SplashScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Globalcolors.mainColor,
body: Container(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Image.asset(
'assets/images/splash_logo.png',
color: Colors.white,
),
),
const Padding(
padding: EdgeInsets.all(50),
child: Text(
'Sample Text',
style: TextStyle(
color: Colors.white,
fontSize: 36,
fontFamily: 'MouseMemoirs'),
),
),
const Text(
'2022',
style: TextStyle(color: Colors.white, fontSize: 12),
),
],
),
),
);
}
}
I tried to use this link to solve the problem. I used Expanded:
const Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: Text(
'2022',
style: TextStyle(color: Colors.white, fontSize: 12),
),
),
)
But, The result is not good:
You can user Stack and column widgets like
Stack(
children: [
Column(), // contains the image and title
Align(
alignment: Alignment.bottomCenter,
child: Text("2022"),
),
]
)
You can also use Positioned widget to align the text 2022
you can use the bottom method that comes with the scaffold
Scaffold(
bottom : Text("2020")
)
this will make the text always on the bottom of the screen

Flutter reduce space between widgets

I try to display a ExpansionTile with some text in it followed by an Image. Referring to the screenshot you can see that there is some space between both widgets. Does someone know how I can reduce this space?
class Test extends StatelessWidget {
const Test({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: backGround,
appBar: AppBar(
title: const Text('Aromatics'),
backgroundColor: appbarColor,
),
body: Column(
children: [
ExpansionTile(
iconColor: titleColor,
title: const Text('Tst', style: TextStyle(color: titleColor)),
subtitle: const Text('Test subtitle',
style: TextStyle(color: Colors.white)),
children: [
Container(
margin: const EdgeInsets.only(left: 20),
child: Column(
children: const [
Text('Test text',
style:
TextStyle(color: Colors.white, fontSize: 13)),
],
),
),
]),
const SizedBox(
height: 1,
),
Expanded(
child: Image.asset('assets/images/test.webp'),
),
],
));
}
}
As per the ExpansionTile Doc, you can use childrenPadding to manage the padding for the children widget
Specifies padding for children.
If this property is null then ExpansionTileThemeData.childrenPadding is used. If that is also null then the value of childrenPadding is EdgeInsets.zero.
Won't removing Expanded suit your goals?
...
const SizedBox(
height: 1,
),
Image.asset('assets/images/test.webp'),
...
Just remove the Expanded widget around the image because it is taking all the remaning space in the Column for the image.
Current Code:
Column(
children: [
...
Expanded(
child: Image.asset('assets/images/test.webp'),
),
...
],
),
New Code:
Column(
children: [
...
Image.asset('assets/images/test.webp'),
...
],
),```

How to make ExpansionTile scrollable when end of screen is reached?

In the project I'm currently working on, I have a Scaffold that contains a SinlgeChildScrollView. Within this SingleChildScrollView the actual content is being displayed, allowing for the possibility of scrolling if the content leaves the screen.
While this makes sense for ~90% of my screens, however I have one screen in which I display 2 ExpansionTiles. Both of these could possibly contain many entries, making them very big when expanded.
The problem right now is, that I'd like the ExpansionTile to stop expanding at latest when it reaches the bottom of the screen and make the content within the ExpansionTile (i.e. the ListTiles) scrollable.
Currently the screen looks like this when there are too many entries:
As you can clearly see, the ExpansionTile leaves the screen, forcing the user to scroll the actual screen, which would lead to the headers of both ExpansionTiles disappearing out of the screen given there are enought entries in the list. Even removing the SingleChildScrollView from the Scaffold doesn't solve the problem but just leads to a RenderOverflow.
The code used for generating the Scaffold and its contents is the following:
class MembershipScreen extends StatefulWidget {
#override
State<StatefulWidget> createState() => _MembershipScreenState();
}
class _MembershipScreenState extends State<MembershipScreen> {
String _fontFamily = 'OpenSans';
Widget _buildMyClubs() {
return Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Color(0xFFD2D2D2),
width: 2
),
borderRadius: BorderRadius.circular(25)
),
child: Theme(
data: ThemeData().copyWith(dividerColor: Colors.transparent),
child: ExpansionTile(
title: Text("My Clubs"),
trailing: Icon(Icons.add),
children: getSearchResults(),
),
)
);
}
Widget _buildAllClubs() {
return Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Color(0xFFD2D2D2),
width: 2
),
borderRadius: BorderRadius.circular(25)
),
child: Theme(
data: ThemeData().copyWith(dividerColor: Colors.transparent),
child: SingleChildScrollView(
child: ExpansionTile(
title: Text("All Clubs"),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.add)
],
),
children: getSearchResults(),
),
)
)
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
extendBody: true,
body: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light,
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
gradient: kGradient //just some gradient
),
),
Center(
child: Container(
height: double.infinity,
constraints: BoxConstraints(maxWidth: 500),
child: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
padding: EdgeInsets.symmetric(horizontal: 40.0, vertical: 20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Clubs',
style: TextStyle(
fontSize: 30.0,
color: Colors.white,
fontFamily: _fontFamily,
fontWeight: FontWeight.bold),
),
SizedBox(
height: 20,
),
_buildMyClubs(),
SizedBox(height: 20,),
_buildAllClubs()
],
),
),
),
),
],
),
)
),
);
}
List<Widget> getSearchResults() {
return [
ListTile(
title: Text("Test1"),
onTap: () => print("Test1"),
),
ListTile(
title: Text("Test2"),
onTap: () => print("Test2"),
), //etc..
];
}
}
I hope I didn't break the code by removing irrelevant parts of it in order to reduce size before posting it here. Hopefully, there is someone who knows how to achieve what I intend to do here and who can help me with the solution for this.
EDIT
As it might not be easy to understand what I try to achieve, I tried to come up with a visualization for the desired behaviour:
Thereby, the items that are surrounded with dashed lines are contained with the list, however cannot be displayed because they would exceed the viewport's boundaries. Hence the ExpansionTile that is containing the item needs to provide a scroll bar for the user to scroll down WITHIN the list. Thereby, both ExpansionTiles are visible at all times.
Try below code hope its help to you. Add your ExpansionTile() Widget inside Column() and Column() wrap in SingleChildScrollView()
Refer SingleChildScrollView here
Refer Column here
You can refer my answer here also for ExpansionPanel
Refer Lists here
Refer ListView.builder() here
your List:
List<Widget> getSearchResults = [
ListTile(
title: Text("Test1"),
onTap: () => print("Test1"),
),
ListTile(
title: Text("Test2"),
onTap: () => print("Test2"),
), //etc..
];
Your Widget using ListView.builder():
SingleChildScrollView(
padding: EdgeInsets.all(20),
child: Column(
children: [
Card(
child: ExpansionTile(
title: Text(
"My Clubs",
),
trailing: Icon(
Icons.add,
),
children: [
ListView.builder(
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Column(
children: getSearchResults,
);
},
itemCount: getSearchResults.length, // try 50 length just testing
),
],
),
),
],
),
),
Your Simple Widget :
SingleChildScrollView(
padding: EdgeInsets.all(20),
child: Column(
children: [
Card(
child: ExpansionTile(
title: Text(
"My Clubs",
),
trailing: Icon(
Icons.add,
),
children:getSearchResults
),
),
],
),
),
Your result screen ->

Adding multiple rows or column in body

Hi trying to figure out why my hamburger is getting misaligned when i add another Expanded() code within the body.
I tried to follow this url to develop a hamburger menu and responsive for all platforms at once.
Flutter Blog
Below is the code that i tried where i added Expanded() below the Row()
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
#override
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
return Scaffold(
key: _scaffoldKey,
drawer: AppDrawer(),
body: Row(
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.all(16),
child: IconButton(
icon: Icon(Icons.menu, size: 30),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
}),
),
],
),
Expanded(
child: Container(
width: width,
child: Column(
children: [
Expanded(
child: Lottie.network(
'https://assets2.lottiefiles.com/datafiles/6WfDdm3ooQTEs1L/data.json'),
),
Expanded(
child: Column(
children: [
Flexible(
child: Text(
"Multivariate Testing",
style: GoogleFonts.montserrat(
fontSize: 50,
),
),
),
SizedBox(
height: 10,
),
Flexible(
child: Text(
'Run experiments to make key flows more effective.',
style: GoogleFonts.montserrat(
fontSize: 25,
),
),
),
SizedBox(
height: 35,
),
// ignore: deprecated_member_use
TextButton(
onPressed: () {},
child: Text(
'Create Experiment',
style: GoogleFonts.montserrat(
fontSize: 20,
),
),
)
],
),
),
],
),
),
)
],
),
);
}
}
and the output that i am seeing from mobile view. Appreciate any help. Sorry for too big screenshot!
This is because of the default functionality of Row.
So change your first child for your top level Row to this,
Scaffold(
key: _scaffoldKey,
drawer: AppDrawer(),
body: Row(
children: [
Align(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.all(16),
child: IconButton(
icon: Icon(Icons.menu, size: 30),
onPressed: () {
_scaffoldKey.currentState.openDrawer();
})),
),
Expanded( ...... rest of your code
Here, we are using the Align widget with alignment: Alignment.topCenter.
Align widget takes the entire available space in the parent and then depending on the alignment property, aligns it't child inside it.
So, saying Alignment.topCenter will place it at the top vertically and middle horizontally.
Using an AppBar will unnecessarily complicate your situation since now, the AppBar is going to take up space a the top, but in your case, you want your Lottie view to be visible at the top, so it will overlap.
Because Expanded fills up the whole space and that's why your hamburger is getting misaligned.
Why don't you try using appbar for the hamburger?

renderflex over flow by 7px on the right in flutter app

Please help me with this error in the flutter app. I did not know what i am during wrong this is my code below: This is the code below.Pls help me
import 'package:flutter/material.dart';
class CartProducts extends StatefulWidget {
#override
_CartProductsState createState() => _CartProductsState();
}
class _CartProductsState extends State<CartProducts> {
var productsOnTheCart=[
{
"name":"Blazer",
"picture":"images/products/blazer1.jpeg",
"price":85,
"size":"M",
"color":"Black",
"quantity":1,
},
{
"name":"M Pant",
"picture":"images/products/pants2.jpeg",
"price": 80,
"size":"8",
"color":"Black",
"quantity":1,
},
{
"name":"Red Dress",
"picture":"images/products/dress1.jpeg",
"price":50,
"size":"7",
"color":"Red",
"quantity":2,
}
];
#override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: productsOnTheCart.length,
itemBuilder: (context, index){
return new SingleCartProduct(
cartProdName:productsOnTheCart[index]['name'],
cartProdColor:productsOnTheCart[index]['color'],
cartProdQty:productsOnTheCart[index]['quantity'],
cartProdSize:productsOnTheCart[index]['size'],
cartProdPrice:productsOnTheCart[index]['price'],
cartProdPicture:productsOnTheCart[index]['picture'],
);
}
);
}
}
class SingleCartProduct extends StatelessWidget {
final cartProdName;
final cartProdPicture;
final cartProdPrice;
final cartProdSize;
final cartProdColor;
final cartProdQty;
SingleCartProduct({
this.cartProdName,
this.cartProdPicture,
this.cartProdPrice,
this.cartProdSize,
this.cartProdColor,
this.cartProdQty});
#override
Widget build(BuildContext context) {
return Card(
child: ListTile(
//leading section image section here
leading: new Image.asset(cartProdPicture, height:80.0,width: 80.0,),
//title section here
title: new Text(cartProdName),
//subtitle section
subtitle: new Column(
children: <Widget>[
//Row Inside Column
new Row(
children: <Widget>[
//this section is for the size of the products
new Padding(padding:const EdgeInsets.all(4.0),
child: new Text("Size:"),
),
new Padding(padding: const EdgeInsets.all(4.0),
child: new Text(cartProdSize, style: TextStyle(color: Colors.red),),
),
//This Section is for Prodcut Color
new Padding(padding:const EdgeInsets.fromLTRB(20.0, 8.0, 8.0, 8.0),
child: new Text("Color:"),),
new Padding(padding:const EdgeInsets.all(4.0),
child: new Text(cartProdColor, style: TextStyle(color: Colors.red),),
),
],
),
new Container(
alignment: Alignment.topLeft,
child: new Text("\$${cartProdPrice}",
style: TextStyle(
fontSize: 17.0, fontWeight: FontWeight.bold, color: Colors.red
),
),
)
//This Section is for the product price
],
),
/* trailing:new Column(
children: <Widget>[
new IconButton(
icon: Icon(
Icons.arrow_drop_up,color: Colors.red),
iconSize: 38,onPressed: () {}),
new IconButton(
icon: Icon(
Icons.arrow_drop_down,color: Colors.red,),
iconSize: 38, onPressed: () {}),
],
)*/
trailing: FittedBox(
fit: BoxFit.fill,
child: Column(
children: <Widget>[
new IconButton(icon: Icon(Icons.arrow_drop_up), onPressed: (){}),
new Text("$cartProdQty", style: TextStyle(fontWeight: FontWeight.bold),),
new IconButton(icon: Icon(Icons.arrow_drop_down), onPressed: (){}),
],
),
),
),
);
}
}
If I remember currectly, applying shrinkWrap: true in the ListView.Builder
is supposed to force the views to fit inside it.
My guess is that the views inside the listview cause the overflow..
Edit:
The overflow could be caused by the column widget inside the single cart widget.
Maybe switching it to a Listview.builder instead may help?
I am guessing overflow is happening in the row widget.
Here is a fix. Use Flexible widget or Expanded Widget based on the requirement
Padding(
padding: const EdgeInsets.symmetric(horizontal: 22.0, vertical: 8.0),
child: Row(
children: <Widget>[
Flexible(
fit: FlexFit.loose, // It means that the child widget can expand to maximum size available thus taking up all space else it can shrink if there is no space available // Refer to the link for more info
child: Text(
'This is my first label:',
style: Theme.of(context).textTheme.subtitle,
),
),
SizedBox(width: 5,), // You can simply put some sized boxes like this to have some fixed space instead of wrapping every child in a padding widget
Flexible(
fit: FlexFit.loose,
child: Text(
'This is my first value',
style: Theme.of(context).textTheme.body1,
),
),
Flexible(
fit: FlexFit.loose,
child: Text(
'This is my second label:',
style: Theme.of(context).textTheme.subtitle,
),
),
SizedBox(width: 5,),
Flexible(
fit: FlexFit.loose,
child: Text(
'This is my second value',
style: Theme.of(context).textTheme.body1,
),
),
],
),
),
Okay so here is the result.
See how texts take multiple lines when enough space is not available. You can avoid overflow in smaller screens this way.
Hope this solves your problem :D