When there are no results loaded on the app, there is a headline and subheadline explaining how to work the application.
However, when results are loaded into the app the Headline and subheadline disappear, and then show the imageArray in place of both
I am unable to group the headline and subheadline together because I am following a specific UI, and thus they require different font styles, but adding them both in a separate child seems to throw an error as the child is already specified.
What's the best way to rectify this?
Container(
margin: EdgeInsets.only(top: 0, left: 0, right: 0, bottom: 0),
height: MediaQuery.of(context).size.height * .8,
child: imageArray.isEmpty
? Center(
child: Text(
‘HEADLINE’,
style: TextStyle(
fontSize: 19,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
child: Text(
‘SUBHEADLINE’,
style: TextStyle(
fontSize: 16,
fontFamily: 'Avenir',
fontWeight: FontWeight.w500,
),
),
)
: GridView.count(
crossAxisCount: 2,
children: List.generate(imageArray.length, (index) {
var img = imageArray[index];
return Container(child: Image.file(img));
})))
]),
)
]));
}
}
According to the official documentation:
All layout widgets have either of the following:
A child property if they take a single child—for example, Center or Container
A children property if they take a list of widgets—for example, Row, Column, ListView, or Stack.
The problem with the code is that Center only takes a single widget as its child. In order to display both of your Text, you'll need to use something such as Column with children property. This should work:
Container(
margin: EdgeInsets.only(top: 0, left: 0, right: 0, bottom: 0),
height: MediaQuery.of(context).size.height * .8,
child: imageArray.isEmpty
? Column( // Use the Column here instead of Center
children: [
Text(
'TITLE',
style: TextStyle(
fontSize: 19,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Text( // Remove the `child:` property
'SUBTITLE',
style: TextStyle(
fontSize: 16,
fontFamily: 'Avenir',
fontWeight: FontWeight.w500,
),
),
],
)
: GridView.count(
crossAxisCount: 2,
children: List.generate(
imageArray.length,
(index) {
var img = imageArray[index];
return Container(child: Image.file(img));
},
),
),
);
Change your Center to a Column this can take a List<Widget> called children as opposed to a singular child
Container(
margin: EdgeInsets.only(top: 0, left: 0, right: 0, bottom: 0),
height: MediaQuery.of(context).size.height * .8,
child: imageArray.isEmpty
? Column(
children: [ Text(
‘HEADLINE’,
style: TextStyle(
fontSize: 19,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Text(
‘SUBHEADLINE’,
style: TextStyle(
fontSize: 16,
fontFamily: 'Avenir',
fontWeight: FontWeight.w500,
),
),
]
)
: GridView.count(
crossAxisCount: 2,
children: List.generate(imageArray.length, (index) {
var img = imageArray[index];
return Container(child: Image.file(img));
})))
]),
)
]));
}
You could also add a Column to the child of your Center if you would prefer that styling.
But Column has the built in functionality to display its children where you want with mainAxisAlignment
Related
In the following code I'd like to align the name 'Jonny Bas' and '#coder' to leftmost
body: Column(
children: [
Center(
child:Text(
'Information',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
),
SizedBox(height: 100),
Text(
'Praveen Kumar',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color:Colors.black,
),
),
Text(
'Coding star- 4',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
],
),
backgroundColor: Colors.grey,
);
}
You can use 'Align' widget or 'positioned' widget if you want to make change in only one or two widget but if want to give all of widgets the same alignment of parent widget column then you can use MainAxisAlignment and CrossAxisAlignment with specified attributes of left/right/etc...
Check the documentation links below for more info on how to use them,
https://api.flutter.dev/flutter/widgets/Align-class.html
https://api.flutter.dev/flutter/widgets/Positioned-class.html
In my text view value comes dynamically from the server some times it has one line when the text is big it takes two lines and overflowed the view like the attached picture.
code:
class ShopsCard extends StatelessWidget {
final UniqueShop uniqueShop;
ShopsCard({this.uniqueShop});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
SearchScreen(
searchArguments: SearchArguments(
searchType: "campaign_shop_click",
campaignShopSlug: uniqueShop.sellerShopSlug,
searchDisplayName: uniqueShop.sellerShopBuisnessName),
)),
);
},
child: Container(
height: 144,
width: 104,
margin: EdgeInsets.only(right: 8.0),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(10)),
padding: EdgeInsets.only(left: 15.0, right: 15.0, top: 10, bottom: 15.0),
child: Column(
children: [
Container(
height: 95.0,
width: 104,
child: uniqueShop.sellerShopImage != null
? populateNetworkImage(imgUrl: uniqueShop.sellerShopImage)
: Icon(
Icons.image,
size: 50,
)),
if (uniqueShop.sellerShopBuisnessName != null)
Text(
uniqueShop.sellerShopBuisnessName,
maxLines: 2,
textAlign: TextAlign.center,
style: commonDmSansTextStyle(
fontSize: 12, fontWeight: FontWeight.w500, color: darkColor),
)
],
),
),
);
}
}
Can anyone tell me what is the problem in my code is and how can I solve it!
Wrap your text with Expanded widget or you can use overflow: TextOverflow.ellipsis,
Expanded(
child: Text(
uniqueShop.sellerShopBuisnessName,
maxLines: 2,
textAlign: TextAlign.center,
style: commonDmSansTextStyle(
fontSize: 12, fontWeight: FontWeight.w500, color: darkColor),
),
)
You could set the maximum line number to one and show an ellipsis if the text overflows
Here is how :
Text(
// ...
maxLines: 1,
overflow: TextOverflow.ellipsis,
);
I have used this solution How Can I generate QR code and Show it on a PDF page in Flutter but still QR Code isn't being printed.
My code:
pdf.BarcodeWidget(
color: PdfColor.fromHex("#000000"),
barcode:pdf.Barcode.qrCode(), data: "My name is Bilal"
),
This is my function:
Future<Uint8List> func_generate_sale_pdf(PdfPageFormat format) {
final doc = pdf.Document();
doc.addPage(pdf.Page(build: (pdf.Context context) {
return pdf.Center(
child: pdf.Column(children: <pdf.Widget>[
pdf.Text(config.app_name.toUpperCase(),
style:
pdf.TextStyle(fontSize: 32, fontWeight: pdf.FontWeight.bold)),
pdf.SizedBox(height: 25),
pdf.Text(
" ${languages.skeleton_language_objects[config.app_language]['for_enquiries_call']} : ${config.app_contacts}",
style: pdf.TextStyle(fontSize: 22)),
pdf.SizedBox(height: 5),
pdf.Text("${config.app_po_box}", style: pdf.TextStyle(fontSize: 22)),
pdf.SizedBox(height: 5),
pdf.Text(
"${languages.skeleton_language_objects[config.app_language]['receipt']} # ${receipt_data['receipt_no']}",
style: pdf.TextStyle(fontSize: 20)),
pdf.SizedBox(height: 5),
pdf.Text("${receipt_data['date_time']}",
style: pdf.TextStyle(fontSize: 20)),
pdf.SizedBox(height: 30),
pdf.Container(
alignment: pdf.Alignment.topLeft,
child: pdf.Text("${receipt_data['items_list'].toString()}",
style: pdf.TextStyle(
fontSize: 22, fontWeight: pdf.FontWeight.normal),
textAlign: pdf.TextAlign.left),
),
pdf.SizedBox(height: 30),
pdf.Container(
child: pdf.Row(children: <pdf.Widget>[
pdf.Expanded(
flex: 1,
child: pdf.Text(
languages.skeleton_language_objects[config.app_language]
['total_amount'],
style: pdf.TextStyle(
fontSize: 20, fontWeight: pdf.FontWeight.bold),
textAlign: pdf.TextAlign.left),
),
pdf.Expanded(
flex: 1,
child: pdf.Text("${receipt_data['total_amount']}",
style: pdf.TextStyle(fontSize: 20),
textAlign: pdf.TextAlign.right),
),
])),
pdf.SizedBox(height: 10),
pdf.Container(
child: pdf.Row(children: <pdf.Widget>[
pdf.Expanded(
flex: 1,
child: pdf.Text(
languages.skeleton_language_objects[config.app_language]
['total_items'],
style: pdf.TextStyle(
fontSize: 20, fontWeight: pdf.FontWeight.bold),
textAlign: pdf.TextAlign.left),
),
pdf.Expanded(
flex: 1,
child: pdf.Text("${receipt_data['total_items']}",
style: pdf.TextStyle(fontSize: 20),
textAlign: pdf.TextAlign.right),
),
])),
pdf.SizedBox(height: 10),
pdf.Container(
height: 35,
child: pdf.Row(children: <pdf.Widget>[
pdf.Expanded(
flex: 1,
child: pdf.Text(
languages.skeleton_language_objects[config.app_language]
['total_tax'],
style: pdf.TextStyle(
fontSize: 20, fontWeight: pdf.FontWeight.bold),
textAlign: pdf.TextAlign.left),
),
pdf.Expanded(
flex: 1,
child: pdf.Text("${receipt_data['total_tax_amount']}",
style: pdf.TextStyle(fontSize: 20),
textAlign: pdf.TextAlign.right),
),
])),
pdf.SizedBox(height: 35),
pdf.Text(
languages.skeleton_language_objects[config.app_language]
['thant_you_and_come_again'],
style: pdf.TextStyle(fontSize: 24)),
//From here on, the compiler doesn't print anything.
pdf.BarcodeWidget(
color: PdfColor.fromHex("#000000"),
barcode:pdf.Barcode.qrCode(), data: "My name is Bilal"
),
pdf.Text("SkillzUPP Technologies: +923058431046",
style: pdf.TextStyle(fontSize: 24),
textAlign: pdf.TextAlign.center),
pdf.SizedBox(height: 10),
]),
); // Center
}));
return doc.save();
}
Everything prints fine except QR Code and at that specific line this error is shown at terminal:
Helvetica-Bold has no Unicode support see https://github.com/DavBfr/dart_pdf/wiki/Fonts-Management
I had a problem showing that line "has no Unicode support..." to fix that i created a folder fonts on my project and added some true type fonts to it, then i created a pdf theme for the document like below:
var myTheme = ThemeData.withFont(
base: Font.ttf(await rootBundle.load("fonts/OpenSans-Regular.ttf")),
bold: Font.ttf(await rootBundle.load("fonts/OpenSans-Bold.ttf")),
italic: Font.ttf(await rootBundle.load("fonts/OpenSans-Italic.ttf")),
boldItalic:
Font.ttf(await rootBundle.load("fonts/OpenSans-BoldItalic.ttf")),
);
then i used the theme in the page of the document and the error was gone, like below:
Page(theme: myTheme,...)
I'm not sure if is the same case, but i think you should try.
Hello i want to ask how to make the text going center.
i have row that contain 2 text.
below is my code.
Container(
child: Padding(
padding: const EdgeInsets.only(bottom: 60.0),
child: Center(
child: Row(
children: [
Text(
"Great",
style: TextStyle(
color: Colors.white,
fontSize: 44.0,
fontWeight: FontWeight.w700,
fontFamily: "Berlin"),
),
Text(
"Ocean",
style: TextStyle(
color: Colors.blue,
fontSize: 44.0,
fontWeight: FontWeight.w700,
fontFamily: "Berlin"),
),
],
),
),
),
),
and output from that code is going like this.
how to make that row going center?
There are a couple of ways to achieve this but first, I would like to refactor your code. You are using Padding inside a Container widget, which is totally unnecessary as Container widget already has a property called padding. So, you should remove the Padding widget.
Also, you have used Center widget inside Container which is also unnecessary as Container has alignment property which you can use to center align child widgets (Alignment.center).
Lastly, in order to center align children widgets in a Row widget, there is mainAxisAlignment & crossAxisAlignment properties which you can use to align widgets.
Setting mainAxisAlignment to MainAxisAlignment.center will center all of your children widgets.
Also, you have used two different Text widgets to create different styled Text widgets, which you can do with a single RichText widget.
Your final code should look like this:
return Container(
padding: const EdgeInsets.only(bottom: 60.0),
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RichText(
text: TextSpan(
style: TextStyle(
color: Colors.white,
fontSize: 44.0,
fontWeight: FontWeight.w700,
fontFamily: "Berlin"),
text: "Great",
children: <TextSpan>[
TextSpan(
text: 'Ocean',
style: TextStyle(
color: Colors.blue,
fontSize: 44.0,
fontWeight: FontWeight.w700,
fontFamily: "Berlin"),
),
],
),
),
],
),
);
I have this UI :UI
And the text is showing this yellow bar, even when I set the maxlines to 3..
I put them in a container and its still like this!
child: Container(
height: 150,
width: 200,
child: Column(children: [
Text(
allRecipes[index].name,
style: TextStyle(color: Colors.black54,
fontSize: 20,),
),
Text(
allRecipes[index].about,
softWrap: true,
maxLines: 3,
style: TextStyle(
fontSize: 14,)
),
Also, I need help from you so I can make more space between the food cards??
Its because your Container Height.
try to increase this and check,
your Container height is 150
increase this 200 or 250. as per your design requirement.
Container(
height: 250,//like this
width: 200,
child: Column(children: [
Text(
allRecipes[index].name,
style: TextStyle(color: Colors.black54,
fontSize: 20,),
),
Text(
allRecipes[index].about,
softWrap: true,
maxLines: 3,
style: TextStyle(
fontSize: 14,)
),
]
)