Adding some space between TableRows in Flutter - flutter

i'm creating a table in Flutter which contains some TableRows, i just want to add some space between these rows.
Table(
columnWidths: {0: FractionColumnWidth(.4)},
children:[
TableRow(children: [
Text(
'Original Title',
),
Text(
original_title,
),
]),
TableRow(children: [
Text(
'Original Language',
),
Text(
original_language,
),
])
],
);

Here's how i did it.
Create a constant, lets say rowSpacer as
const rowSpacer=TableRow(
children: [
SizedBox(
height: 8,
),
SizedBox(
height: 8,
)
]);
Remember the number of SizedBox widgets to add above should be same as the number of colums in your table. For this case I have 2 colums, hence 2 SizedBoxes.
Now use this constant to give spacing between rows.
Table(
children: [
TableRow(
children: [
Text('Name:'),
Text('Aman kumar')
]),
rowSpacer, //Using the Constant
TableRow(
children: [
Text('Date of Birth:'),
Text('September 27, 1998')
]),
)

Probably not the most efficient way but you can wrap the TableRow in a Padding Class
Padding(
padding: EdgeInsets.all(8.0),
child: const Card(child: Text('Hello World!')),
)
Something along the lines of:
Table(
columnWidths: {0: FractionColumnWidth(.4)},
children:[
TableRow(children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 8.0)
child: Text(
'Original Title',
)),
Padding(
padding: EdgeInsets.symmetric(vertical: 8.0)
child: Text(
original_title,
)),
]),
TableRow(children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 8.0)
child: Text(
'Original Language',
)),
Padding(
padding: EdgeInsets.symmetric(vertical: 8.0)
child: Text(
original_language,
)),
]),
],
);
Padding Class:
https://api.flutter.dev/flutter/widgets/Padding-class.html
EdgeInsets Class:
https://api.flutter.dev/flutter/painting/EdgeInsets-class.html

The easiest thing you can do is to wrap the content of each table cell with a Padding like so:
TableRow (children: [
TableCell(
child:Padding(
padding: const EdgeInsets.all(8.0),
child: Text(EMAIL,
style: TextStyle(
fontWeight: FontWeight.bold,
),),
),
),
TableCell(
child:Padding(
padding: const EdgeInsets.all(8.0),
child: Text(authUser.email),
)
),
])

TableRow(children: [
TableCellPadded(child: const Text('Bla'))
])
class TableCellPadded extends StatelessWidget {
final EdgeInsets? padding;
final Widget child;
final TableCellVerticalAlignment? verticalAlignment;
const TableCellPadded(
{Key? key, required this.child, this.padding, this.verticalAlignment})
: super(key: key);
#override
TableCell build(BuildContext context) => TableCell(
verticalAlignment: verticalAlignment ?? TableCellVerticalAlignment.middle,
child: Padding(padding: padding ?? const EdgeInsets.all(5.0), child: child));
}
Found it here and adjusted it:
https://github.com/flutter/flutter/issues/36816

Related

Flutter dynamic text grid

Does anyone know how to create a grid like this using GridView.builder. Any other alternative will be much appreciated too.
The text in the container should be dynamic as well based on the size of the text.
Thank you
Tried using gridView but the container was always of fixed width and height. The row items was not dynamic as well
You can achieve it by wrapping your list with a Wrap widget. Check the example below:
import 'package:flutter/material.dart';
class NewPage extends StatelessWidget {
const NewPage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
List<String> countries = [
'Canada',
'Germany',
'India',
'Taiwan, Province of China',
'United States',
'United Kingdom',
'Kingdom of Saudi Arabia',
'Bangladesh',
'Vietnam',
'Peru',
'Italy'
];
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
const Text('List of countries',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
const SizedBox(height: 20),
Wrap(
alignment: WrapAlignment.start,
spacing: 10.0,
runSpacing: 10.0,
children: List.generate(
countries.length,
(index) => Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20), color: Colors.grey[400]!),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Text(
countries[index],
style: const TextStyle(fontSize: 16),
),
),
)),
)
],
),
),
);
}
}
Please have a look at Wrap. I recommend that you watch the video first provided on link then read the documentation/example.

Creating a resizeable table that resizes when dragged from corners and sides in FLUTTER

Here is what i want
Hello, I made a table with Flutter, I need help resizing my table INSIDE the app by dragging its corners(like in the image) and not by changing sizes in the code. I found a question like this but with an image, and I couldn't integrate that into my code correctly. Can someone help me? I'll link the question here.
Creating Resizable View that resizes when pinch or drag from corners and sides in FLUTTER
Update: I also found a question like that with a table but it has private packages which I can't use, I will link it here too just in case` Create dynamic table rows in PDF document using the pdf package
And I'll put my code here.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget{
const MyApp({super.key});
#override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget{
const HomePage({super.key});
#override
Widget build(BuildContext context) {
return Scaffold(
//backgroundColor: Colors.blue[100], //background color of scaffold
appBar: AppBar(
//title:const Text("Flutter Table"), //title of app
backgroundColor: Colors.white, //background color of app bar
),
body: Container(
padding: const EdgeInsets.all(15),
child:Table(
border: TableBorder.all(width:1, color:Colors.black45), //table border
children: const [
TableRow(
children: [
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("S/N")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Name")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Address")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Nation")
)
)
]
),
TableRow(
children: [
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("1.")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Krishna Karki")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Nepal, Kathmandu")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Nepal")
)
)
]
),
TableRow(
children: [
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("2.")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("John Wick")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("New York, USA")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("USA")
)
)
]
),
TableRow(
children: [
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("3.")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Fedrick May")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Berlin, Germany")
)
),
TableCell(
child: Padding(
padding: EdgeInsets.all(10),
child:Text("Germany")
)
)
]
),
],)
)
);
}
}
Here is how it looks`

Flutter, Overflow in Drawer's DrawerHeader

The problem may not be just for the Drawer or DrawerHeader widgets, but on how Expanded or Flexible may work inside a DrawerHeader widget combined with Column and Row widgets.
My code with my Drawer is simply like this:
import 'package:flutter/material.dart';
class PlanningDrawer extends StatefulWidget{
const PlanningDrawer({Key? key}) : super(key: key);
#override
State<PlanningDrawer> createState() => _PlanningDrawerState();
}
class _PlanningDrawerState extends State<PlanningDrawer> {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
DrawerHeader(
decoration: const BoxDecoration(color: Colors.blue),
child: Padding(
padding: const EdgeInsets.only(bottom: 28),
child: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.calendar_month_outlined, size: 48,),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("mPlanning", style: TextStyle(fontSize: 20),),
Text("Application description which may span over many lines")
],
),
)
]
),
Divider(color: Colors.black),
Text("Additional info comes here, which may span over many lines")
],
),
)
)
],
)
);
}
}
And it looks like this when compiling to test on Google Chrome:
As you see I have two overflows.
The upper right one is linked to the long text describing my app, which I want to wrap into multiple lines, it may span many lines. I have tried to wrap it into Flexible, Expanded and some other suggested widgets, but found no luck yet.
The lower I am not sure which it is bound to, since it may be secondary (the long text there wraps nicely though, (not so well seen underneath the warning)).
Could someone please give me a working hint how to get both long texts wrapping ?
Use ListTile Widget instead of Row Widget Please check the below example
class PlanningDrawer extends StatefulWidget {
const PlanningDrawer({Key? key}) : super(key: key);
#override
State<PlanningDrawer> createState() => _PlanningDrawerState();
}
class _PlanningDrawerState extends State<PlanningDrawer> {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
DrawerHeader(
decoration: const BoxDecoration(color: Colors.blue),
child: Column(
children: const [
ListTile(
title: Text(
"mPlanning",
style: TextStyle(fontSize: 20),
),
subtitle: Text(
"Application description which may span over many
lines"),
leading: Icon(
Icons.calendar_month_outlined,
size: 48,
),
),
Divider(color: Colors.black),
Text(
"Additional info comes here, which may span over many lines ")
],
),
)
],
),
);
}
}
If you need more height, you might replace the DrawerHeader with a Column widget.
class PlanningDrawer extends StatefulWidget {
const PlanningDrawer({Key? key}) : super(key: key);
#override
State<PlanningDrawer> createState() => _PlanningDrawerState();
}
class _PlanningDrawerState extends State<PlanningDrawer> {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
children: [
Expanded(
child: Container(
color: Colors.blue,
padding: EdgeInsets.all(8),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Icons.calendar_month_outlined,
size: 48,
),
SizedBox(width: 10),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"mPlanning",
style: TextStyle(fontSize: 20),
),
Text(
"Application description which may span over many linesApplication description which may span over many linesApplication description which may span over many linesApplication description which may span over many linesApplication description which may span over many lines",
softWrap: true,
)
],
),
),
//
],
),
),
Divider(color: Colors.black),
Text(
"Additional info comes here, which may span ov info comes here, which may span ov info comes here, which may span ov info comes here, which may span ov info comes here, which may span ov info comes here, which may span over many lines")
],
),
),
),
],
),
);
}
}
Try using SizedBox to have some space.
The default one with DrawerHeader Also, it provides default padding.
If it is ok to have like this max 2line description with title
You can follow this
class _PlanningDrawerState extends State<PlanningDrawer> {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
DrawerHeader(
margin: EdgeInsets.zero,
decoration: const BoxDecoration(color: Colors.blue),
child: Column(
children: [
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Icon(
Icons.calendar_month_outlined,
size: 48,
),
// sizedBOx if needed
SizedBox(
width: 10,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"mPlanning",
style: TextStyle(fontSize: 20),
),
Expanded(
child: Text(
"Application which may span over many lines",
softWrap: true,
maxLines: 5,
),
)
],
),
)
],
),
),
Divider(color: Colors.black),
Text(
"Additional info comes here, which may span over many lines"),
SizedBox(
height: 28,
)
],
),
),
],
));
}
}
After some more time, and of course both Muhammad Mohsin and Yeasin Sheikh's answers above I came upon a slight change in my code which solved both overflows.
In my fairly simple mind, I thought that DrawerHeader adjusted its height when more widgets where added, but no, you need to expand it manually or make some tweak to do just that.
For the width overflow, I need to wrap the Column which the "mPlanning" and "Application Description..." is placed, in an Expanded widget. Additional you need to put the DrawerHeader inside a SizedBox and constrain its height to some number (200 in this case). Now the DrawerHeader seems to prepare for constraints both in width and height as expected.
import 'package:flutter/material.dart';
class PlanningDrawer extends StatefulWidget{
const PlanningDrawer({Key? key}) : super(key: key);
#override
State<PlanningDrawer> createState() => _PlanningDrawerState();
}
class _PlanningDrawerState extends State<PlanningDrawer> {
#override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
SizedBox( //<-- Added
height: 200, //<-- Fixed height
child: DrawerHeader(
decoration: const BoxDecoration(color: Colors.blue),
child: Padding(
padding: const EdgeInsets.only(bottom: 28),
child: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(Icons.calendar_month_outlined, size: 48, color: Colors.white,),
),
Expanded( //<-- Added to let texts in here wrap if necessary.
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("mPlanning", style: TextStyle(fontSize: 20, color: Colors.white)),
Text("Application description which may span over many lines", style: TextStyle(color: Colors.white),)
],
),
),
)
]
),
Divider(color: Colors.white),
Text("Additional info comes here, which may span over many lines", style: TextStyle(color: Colors.white),)
],
),
)
),
)
],
)
);
}
}
And the result is now:

Why does this row don't wrap in flutter

What the problem is I have to render some texts which are in a row and also it should wrap when it doesn't have enough space but the row does not wrap.
I have used wrapping it Expanded, Wrap, and tried some stackoverflow answers but none of them work
Below is my code for it
Widget xyz(List _list) {
return Padding(
padding: const EdgeInsets.fromLTRB(50, 10, 40, 10),
child: InkWell(
onTap: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Title : ',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13),
),
Wrap(children: [
Row(children: [
for (int i = 0; i < _list.length; i++)
Container(
padding: const EdgeInsets.only(right: 8),
child: Text(
'${_list[i]}',
style: TextStyle(fontSize: 13),
),
),
]),
]),
],
),
),
);
}
class _Page3State extends State<Page3> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
xyz([
'Text1',
'Text2',
'Text3',
'Text4',
'Text5',
'Text6',
'Text7',
]),
//other widgets
],
),
);
}
}
And this is the output I am getting
I want the 'Text6' & 'Text7' Text widgets to go on the next line.
This is a working example:
The issues are incorrect Flexible (Expanded) placing, and your Wrap had a Row child instead of directly placed children.
Widget xyz(List _list) {
return Padding(
padding: const EdgeInsets.fromLTRB(50, 10, 40, 10),
child: InkWell(
onTap: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'Title : ',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13),
),
Flexible(
child: Wrap(
children: [
for (int i = 0; i < _list.length; i++)
Container(
padding: const EdgeInsets.only(right: 8),
child: Text(
'${_list[i]}',
style: TextStyle(fontSize: 13),
),
),
],
),
),
],
),
),
);
}
class _Page3State extends State<Page3> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
xyz([
'Text1',
'Text2',
'Text3',
'Text4',
'Text5',
'Text6',
'Text7',
]),
//other widgets
],
),
);
}
}

Text Wrapping in Flutter

I am writing a widget in flutter for a project which should look like this, I am new to flutter and I am trying to imitate the card below
So far this is my code for the widget
import 'package:flutter/material.dart';
import 'package:smooth_star_rating/smooth_star_rating.dart';
class TrailCard extends StatelessWidget {
const TrailCard({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: InkWell(
onTap: () {},
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.asset(
'assets/images/cocoa.jpg',
height: 100,
width: 90,
fit: BoxFit.fitHeight,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Boring Cocoa",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold),
),
Text('Rajhamundry, AP'),
Text(
'Feel the authentic flavours of life while you visit the farms around Rajahmundry',
overflow: TextOverflow.ellipsis,
)
],
),
),
],
)),
)),
);
}
}
I tried using flexible and expanded to try and make the content look the way but I am getting overflow error
You can wrap the Padding widget with a Flexible widget so it takes only the amount of space needed:
NOTE: you should also set the maximum amount of lines the Text widget should take before showing the ellipsis.
Flexible(
// new line
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Boring Cocoa",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Text('Rajhamundry, AP'),
Text(
'Feel the authentic flavours of life while you visit the farms around Rajahmundry more text to cause overflow',
// set maximum number of lines the text should take
maxLines: 3, // new line
overflow: TextOverflow.ellipsis,
)
],
),
),
);
RESULT:
Wrap Padding with Expanded
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Boring Cocoa",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold),
),
Text('Rajhamundry, AP'),
Text(
'Feel the authentic flavours of life while you visit the farms around Rajahmundry',
overflow: TextOverflow.ellipsis,
)
],
),
),
)