I want to right justify the TextButton in Flutter - flutter

I want to send the "See More" button in the middle to the right.
Row(
children: <Widget>[
Stack(
children: const <Widget>[
Align(
alignment: Alignment.topLeft,
child: Padding(
padding: EdgeInsets.all(8.0),
child: Text(
'Your Watchlist',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
TextButton(
child: const Text(
'See More >>',
style: TextStyle(color: Colors.yellow, fontSize: 16),
),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => movie_list(),
));
},
),
],
),
Also, how can I pull data from firebase to listview items in the form of cards at the bottom, can you suggest a source on this subject?

You can achieve this by 2 methods
Option1
Add
mainAxisAlignment: MainAxisAlignment.spaceBetween,
To the row widget
Or
Option2
add
Spacer()
Between the see more and the stack widget

You can include Spacer() on middle portion,
Row
- WidgetA
- Spacer()
- WidgetB

Related

Centered Text inside a button with an icon in Flutter

I'm having issues where I want to make a button (any kind to achieve this) that has an icon that is always in the same position at the start of the button, and that the text of the button is always centered compared to the text below it and always centered in the button itself, even if the text is dynamic. This is what I want to achieve:
The button text is centered according to the text below(that stays the same) and it is centered with the above button, but if the button text changes dynamically, then I have these issues:
Or like this one:
Any ideas how to solve this problem?
I tried with the Expanded widget and adding the flex properties but failed. I tried some other stuff too but failed also... Here is the button code:
TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.blue)),
onPressed: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
),
),
Expanded(
flex: 4,
child: Center(
child: Text(
"SOME TEXT",
style: TextStyle(color: Colors.white),
),
),
)
],
),
),
Thanks in advance for your help!
There are a couple of options to do that. I'm going to assume the size of the text could change so you want a flexible solution.
The first option is the most obvious of the two, but will break if the text is too long. The trick is to add an empty sizedBox at the end of the button, so the text can be centered in an expanded widget.
TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: () {},
child: Row(
children: [
Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
size: 24,
),
Expanded(
child: Center(
child: Text(
"SOME TEXT",
style: TextStyle(
color:Colors.white,
),
),
),
),
SizedBox(
width: 24, // width of the icon
),
],
),
);
The other option is using a stack widget. This way, if the text is too long it will overlap with the icon but it's less probable that it will overflow the button.
TextButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: () {},
child: Stack(
children: [
Positioned(
left: 0,
child: Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
size: 24,
),
),
Center(
child: Text(
"SOME TEXT",
style: TextStyle(
color:Colors.white,
),
),
),
],
),
);
Let's discuss about you code :
using row with a mainaxisalignment from start make the element start from the available area. then i suggest to use the SizedBox to make some of spacing between the elements as you want and play with the width of it.
TextButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.blue)),
onPressed: () {},
child: Row(
mainAxisAlignment: MainAxisAlignment.start, // here the issue
children: [
Expanded(
flex: 1,
child: Icon(
Icons.keyboard_arrow_right_sharp,
color: Colors.white,
),
),
SizedBox(width: MediaQuery.of(context).size.width * 0.08, // change this value as you want 0.08 or 0.1 ...etc
),
Expanded(
flex: 4,
child: Center(
child: Text(
"SOME TEXT",
style: TextStyle(color: Colors.white),
),
),
)
],
),
),
for more read this documentation

arranging a scaffold with buttons

i wrote this code in flutter and i wanna know if i can make it better,
i did a lot of "work around" that i feel there's a better way to write it.
especially the buttons with the expanded between them, and the sizedbox.
the screen has text in the middle(getVerse), and and two buttons, one is bottom left and the other is bottom right.
the first expanded separate the text from the buttons, and the second expanded is to separate the two buttons
help is appreciated and thanks for your time.
Scaffold(
appBar: AppBar(
title: Text(
suras.elementAt(widget.index),
textAlign: TextAlign.right,
),
),
body: Column(
children: [
SizedBox(
height: 100,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
getVerse(widget.index + 1, currentVerse),
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24,
fontFamily: 'KFGQPC BAZZI Uthmanic Script',
),
),
),
Expanded(child: Container()),
Row(
children: [
ElevatedButton(
onPressed: () {
setState(() {
currentVerse++;
});
},
child: Text('not sure'),
),
Expanded(child: Container()),
ElevatedButton(
onPressed: null,
child: Text('sure'),
),
],
),
],
),
);
If you want that those 2 buttons to stay at the bottom you can use floatingActionButton in the Scaffold property. You can change the location of those buttons with the floatingActionButtonLocation.
Scaffold(
appBar: AppBar(
title: Text('MyApp'),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {},
child: Text('Not sure'),
),
ElevatedButton(
onPressed: () {},
child: Text('Sure'),
)
],
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'My Text',
textAlign: TextAlign.center,
),
),
),

How to center AlertDialog FlatButton in Flutter

How can I style the FlatButton to be aligned on center without having to create a custom AlertDialog?
AlertDialog(
title: Text(
'Alert Dialog'.toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28.0),
),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Scrollable AlertDialog' * 100),
],
),
),
actions: <Widget>[
FlatButton(
child: Text(
'Accept'.toUpperCase(),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
This is how it looks like
I tried removing 'actions' and adding the FlatButton inside a row in 'content', but the scroll stopped working and the content overflowed.
content: Column(
children: <Widget>[
SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Scrollable AlertDialog.' * 50),
],
),
),
Row(
children: <Widget>[
FlatButton(
child: Text(
'Accept'.toUpperCase(),
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
fontSize: 16.0
),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
],
),
Google's material design guidelines don't want you to place that in center, which is why Flutter team used ButtonBar for actions and you have no control over ButtonBar's alignment inside actions property, so if you really want to center your single button, there are 2 simple workarounds:
Make changes in source code, in dialog.dart's AlertDialog's build method, you'll see a ButtonBar, widget, add alignment to it.
ButtonBar(
alignment: MainAxisAlignment.center, // add this line
// ...
)
You can use dummy widgets like SizedBox and provide some width to it.
actions: [
SizedBox(width: space),
FlatButton(
onPressed: () {},
child: Text('Button'),
),
SizedBox(width: space),
]
Screenshot:
Try to play around with actionsPadding which is one of the property of AlertDialog.
final horizontalPadding=50;
horizontal padding is the one that is transparent to the left and right of the dialog.
actionsPadding: EdgeInsets.only(right: MediaQuery.of(context).size.width/2-horizontalPadding*2),
Also, go through How to style AlertDialog Actions in Flutter . You can change and play around within the library as mentioned here. But, this may not work for your colleagues who are sharing the git repo. They will have to update their libraries as well in their local machines.
actions: <Widget>[
Align(
alignment: Alignment.center,
child: ElevatedButton(
onPressed: () {
//your logic
Navigator.pop(context);
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Close'),
),
),
),
],
Just use actionsAlignment property in AlertDialog Widget.
AlertDialog(
actions: ...,
actionsAlignment: MainAxisAlignment.spaceBetween
)

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

Flutter: Horizontal auto Scrolling

I would like some help with getting the artist names to automatically scroll from right to left (or left to right if that's easier for you) in flutter, but I am not sure how to go about that and would appreciate someone's help :) I have looked into Horizontal ListViews and all but I was getting errors so I didn't move forward with that.
#override
Widget build(BuildContext context) {
return Scaffold(body: Stack(
children: <Widget>[
Container(decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/danceClub.jpg"),
fit: BoxFit.cover,
),
),
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
child: Container(decoration: BoxDecoration(color: Colors.black.withOpacity(0.2))))
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(child: Container(child: Text('Now Playing', style: TextStyle(fontSize: 30, color: Colors.black),),)),
_imageURLValue != null ? Image.network(_imageURLValue, height: 300) : Container(),
Row(mainAxisSize: MainAxisSize.max, children: <Widget>[Padding(padding: EdgeInsets.only(left: 30.0)), Text('${_songName}', style: TextStyle(fontSize: 25, color: Colors.amber)), Text(' - ', style: TextStyle(fontSize: 25, color: Colors.black)),
Expanded(child: ListView.builder(shrinkWrap: true, itemCount: _artistsNames == null ? 0 : _artistsNames.length,
itemBuilder: (context, i) { return Text(_artistsNames[i], style: TextStyle(fontSize: 25, color: Colors.blue),);}))]),
Container(child: RaisedButton(
onPressed: () => Navigator.pop(context),
child: Text('Exit to menu'),
)),
Container(child: RaisedButton(
onPressed: getData,
child: Text('Get data from http!', style: TextStyle(color: Colors.white, fontStyle: FontStyle.italic, fontSize: 20.0)),
))
],
)
]
)
);
}
}
There is a Flutter plugin that seems to match exactly your need:
marquee:
https://pub.dev/packages/marquee
A Flutter widget that scrolls text infinitely. Provides many customizations including custom scroll directions, durations, curves as well as pauses after every round.
The most simple solution is to use carousel_slider library