How to Position Widgets Independently of Other Widgets in flutter? - flutter

I was Trying to Have 3 Widgets in Single page using Column, but for some reason the alignment is not working...
#override
Widget build(BuildContext context) {
return Container(
height: double.infinity,
child: SwipeDetector(
onSwipeUp: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CreditScreen()));
},
onSwipeDown: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ManualScreen()));
},
child: Scaffold(
backgroundColor: DarkBlue,
resizeToAvoidBottomInset: false,
body: new Container(
height: double.infinity,
alignment: Alignment.center,
child: Column(
//mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Align(
alignment: Alignment.topCenter,
child: Image.asset('assets/swipe_down.gif',
scale: 5,
),
),
new Align(
alignment: Alignment.center,
child: Text("Menu Screen",
style: TextStyle(
color: Colors.white,
fontSize: 30.0,
fontWeight: FontWeight.bold,
),
//textAlign: TextAlign.center,
),
),
new Align(
alignment: Alignment.bottomCenter,
child: Image.asset('assets/swipe_up.gif',
scale: 5,
),
),
],
),
),
),
),
);
}
}
According to the above image, I want the swipe up widget at bottom and swipe down at the top while the text in center, I Tried Many wrapping of Widgets, but nothing seems to work...
Any Help...

try this:
Column(
//mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/swipe_down.gif',
scale: 5,
),
Expanded(
child: Text("Menu Screen",
style: TextStyle(
color: Colors.white,
fontSize: 30.0,
fontWeight: FontWeight.bold,
),
//textAlign: TextAlign.center,
),
),
Image.asset('assets/swipe_up.gif',
scale: 5,
),
],
),

You can use Center widget for aligning widgets in center along with padding widget for giving spaces between the widgets.
Scaffold(
backgroundColor: Colors.black,
resizeToAvoidBottomInset: false,
body: new Container(
height: double.infinity,
alignment: Alignment.center,
child:
Center(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top:30),
child: Image.asset('assets/swipe_down.gif'),
),
Padding(
padding: EdgeInsets.only(top:80),
child:
Text("Menu Screen",
style: TextStyle(
color: Colors.white,
fontSize: 30.0,
fontWeight: FontWeight.bold
),
),
),
Padding(
padding: EdgeInsets.only(top:80),
child: Image.asset('assets/swipe_up.gif'),
),
],
),
),
),
),

Related

overflow with expanding listView, and wrong displacement in stack inside column

I'm trying to make a list that when expanded hides under button (stack will do it) and place the button at the bottom of the screen. The problem is whenever the list comes to the bottom edge, it throws an overflow error. The second problem is that when I wrap list and button in stack, they are stacked on each other. Without stack wrapping the Column the layout is fine but the list still overflows. With stack, the displacement is wrong and app throws vertical overflow error.
I'm aiming towards a layout shown on this picture
Layout without usage of stack
\
Layout with usage of stack
Here is task_list_screen.dart:
class TaskListScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: Container(
color: Colors.blueAccent,
child: Text(
'Your tasks chief',
style: GoogleFonts.notoSans(
textStyle: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w500,
color: Colors.black)),
textAlign: TextAlign.center,
),
alignment: Alignment.topCenter,
),
),
Container(
height: 500,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Stack(
children: [
_taskList(),
Align(
child: _AddTaskButton(),
alignment: Alignment.bottomCenter,
),
],
),
],
),
),
],
),
),
),
),
);
}
}
A simple way to archive your layout using floatingActionButton
Result
Demo widget
class TaskListScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(20),
child: Container(
color: Colors.blueAccent,
child: Text(
'Your tasks chief',
// style: GoogleFonts.notoSans(
// textStyle: TextStyle(
// fontSize: 30,
// fontWeight: FontWeight.w500,
// color: Colors.black)),
textAlign: TextAlign.center,
),
alignment: Alignment.topCenter,
),
),
Expanded(
child: ListView(
shrinkWrap: true,
children: [
...List.generate(
20,
(index) => Container(
height: 100,
color: index.isEven ? Colors.red : Colors.pink,
),
),
],
),
),
],
),
),
),
floatingActionButton: Align(
/// alignment: Alignment(0, .95), more controll on dy
alignment: Alignment.bottomCenter,
child: ElevatedButton(
onPressed: () {},
child: Text("asda"),
),
),
),
);
}
}

Dismissible content going outside

I have a screen with a list of Widgets, each widet is a Dismissible with a ListTile inside, but when I swipe, the content is going outside (as pointed by the red arrow), this may be happening because of the padding around the Dismissible. There is a way to fix it?
You are not giving your code sample in your question so, I have make this type of widget to solve this problem. Please refer the code, (It's may be help to you),
class _MyHomePageState extends State<MyHomePage> {
final itemsList = List<String>.generate(10, (n) => "List item ${n}");
ListView generateItemsList() {
return ListView.builder(
itemCount: itemsList.length,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.symmetric(horizontal: 50, vertical: 10),
child: Dismissible(
key: Key(itemsList[index]),
background: slideRightBackground(),
secondaryBackground: slideLeftBackground(),
child: InkWell(
onTap: () {
print("${itemsList[index]} clicked");
},
child: ListTile(
tileColor: Colors.yellow,
title: Text('${itemsList[index]}'))),
),
);
}
);
}
Widget slideRightBackground() {
return Container(
color: Colors.green,
child: Align(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(
width: 20,
),
Icon(
Icons.edit,
color: Colors.white,
),
Text(
" Edit",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
),
textAlign: TextAlign.left,
),
],
),
alignment: Alignment.centerLeft,
),
);
}
Widget slideLeftBackground() {
return Container(
color: Colors.red,
child: Align(
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Icon(
Icons.delete,
color: Colors.white,
),
Text(
" Delete",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w700,
),
textAlign: TextAlign.right,
),
SizedBox(
width: 20,
),
],
),
alignment: Alignment.centerRight,
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: generateItemsList(),
);
}
}
Output :

How to add a RaisedButton inside the Card with the height same as the card?

I want to add a button in the right side of the card,by default the button's color should be green and while tapping on it, there should be a popup box opened and the color of the button would turn to red.
1-The button's color should be green while the listView willed be plotted.
2-Then when I am tapping on the button, a dialog box should be opened with a input textField.
3-After I am inserting any text and pressing the submit button of the popup box, the entered text will be printed on the console.
4-Then the color of the button would turn to red from green.
As I am new to flutter, please help me to get this.
Here is my Code:
body: ListView.builder(
itemCount: patients.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(9.0),
child: SizedBox(
height: 120,
child: Card(
elevation: 5.0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Row(
children: [
Expanded(
flex: 3,
child: Container(
child: CircleAvatar(
backgroundImage: NetworkImage(patients[index].imgPath),
radius: 50.0,
),
),
),
Expanded(
flex: 5,
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(patients[index].name, style: TextStyle(
fontSize: 23.0,
fontWeight: FontWeight.bold,
color: Colors.black87
),),
SizedBox(
height: 20,
),
Text(patients[index].completedSession.toString() +'/'+ patients[index].totalSession.toString(),
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.black54
),),
],
),
),
),
Expanded(
flex: 2,
child: Container(
child: RaisedButton(
),
),
),
],
),
),
),
),
),
);
},
),
You should use CrossAxisAlignment.stretch for your Row:
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
void main() {
runApp(
MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: MyWidget(),
),
),
);
}
class MyWidget extends HookWidget {
#override
Widget build(BuildContext context) {
final pushed = useState(false);
return AspectRatio(
aspectRatio: 3,
child: Card(
elevation: 5.0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
CircleAvatar(
backgroundImage: NetworkImage(
'https://upload.wikimedia.org/wikipedia/commons/5/54/RowanAtkinsonMar07.jpg',
),
radius: 50.0,
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Mister B.',
style: TextStyle(
fontSize: 23.0,
fontWeight: FontWeight.bold,
color: Colors.black87),
),
SizedBox(
height: 20,
),
Text(
'0/42',
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.black54),
),
],
),
),
RaisedButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
pushed.value ? 'Button unpushed' : 'Button pushed'),
),
);
pushed.value = !pushed.value;
},
color: pushed.value ? Colors.red : Colors.green,
child: Text('OK'),
),
],
),
),
),
);
}
}

How to make carousel like swipe on stacked cards?

I am trying to put stack cards on a center view of flutter container.
Which I am trying to have carousel like swipe or slider only left to right or right to left. On right to left the next card will come up and swipe left to right the prvious card will come back. Something like example link to animation
So far, I have gotten the desired view with Stack and Positioned widgets. Now how do I make the slide or animate look like the example?
So far tried to do it with draggable, which is dragging to all around, which is not the behaviour I am looking for. Then I looked at PageView which will not work with Positioned widget as they are expected to be under Stack. So at the moment I am not sure how to do this? Any guide or example would be really helpful, as I just started flutter a week now.
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
flex: 2,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SvgPicture.asset(
'assets/images/group_6.svg',
fit: BoxFit.scaleDown,
),
SizedBox(width: 10),
Text(
'My cool Header',
style: TextStyle(
color: const Color(0xFF000000),
fontSize: Constants.height / 70,
fontFamily: 'Exo2',
fontWeight: FontWeight.w600),
),
],
),
),
Expanded(
flex: 13,
child: Stack(
overflow: Overflow.visible,
alignment: Alignment.center,
children: <Widget>[
Positioned(
right: (sizingInformation.screenSize.width * 0.09),
left: (sizingInformation.screenSize.width * 0.21),
child: Card(
color: Colors.blue,
elevation: 10.0,
child: Container(
height: (sizingInformation.screenSize.height * 0.5),
),
),
),
Positioned(
right: sizingInformation.screenSize.width * 0.15,
left: sizingInformation.screenSize.width * 0.15,
child: Card(
color: Colors.red,
elevation: 10.0,
child: Container(
height: (sizingInformation.screenSize.height * 0.55),
),
),
),
],
),
),
Expanded(
flex: 2,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Some cool text',
style: TextStyle(
color: const Color(0xFF000000),
fontSize: Constants.height / 70,
fontFamily: 'Exo2',
fontWeight: FontWeight.w600,
),
),
Text(
'Some cool text',
style: TextStyle(
color: const Color(0xFF000000),
fontSize: Constants.height / 70,
fontFamily: 'Exo2',
fontWeight: FontWeight.w600),
),
]),
),
Expanded(
flex: 3,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// Search
IconButton(
icon: SvgPicture.asset(
'assets/images/group_9.svg',
fit: BoxFit.contain,
),
onPressed: () {
widget.comingFromLogin
? Navigator.push(context,
SlideRightRoute(page: SearchFoodPage()))
: _loginPopUp();
}),
// Add Event
IconButton(
icon: SvgPicture.asset(
'assets/images/group_26.svg',
fit: BoxFit.contain,
),
onPressed: () {
widget.comingFromLogin
? Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Events()))
: _loginPopUp();
},
),
// Profile
IconButton(
icon: SvgPicture.asset(
'assets/images/group_8.svg',
fit: BoxFit.contain,
),
onPressed: () {
widget.comingFromLogin
? Navigator.push(context,
SlideLeftRoute(page: ProfileDrawer()))
: _loginPopUp();
}),
],
),
),
]),
),
);
Is it something like this library: flutter_swiper, stack layout
Swiper(
itemBuilder: (BuildContext context, int index) {
return new Image.network(
"http://via.placeholder.com/288x188",
fit: BoxFit.fill,
);
},
itemCount: 10,
itemWidth: 300.0,
layout: SwiperLayout.STACK,
)

Layout in list tile - flutter

Current output:
Expected output:
Code:
SizedBox(
width: 380,
height: 180,
child: Swiper(
itemCount: items.length,
itemBuilder: (context, index) {
return Column(
children: <Widget>[
Expanded(
child: Card(
child: ListTile(
title: Text(
'${items[index].title}',
),
subtitle: Text(
'${items[index].body}',
),
trailing: Column(
children: <Widget>[
CircleAvatar(
backgroundColor: HexColor("#0087a8"),
child: IconButton(
icon: Icon(Icons.add),
],
),
),
),
)
],
);
},
viewportFraction: 0.8,
scale: 0.9,
control: SwiperControl(color: Colors.white),
),
);
I am unable to create the icon button to be that way. As when i put in padding, it says that the pixels overflowed. I need to get the add button on the bottom right hand side.
As already pointed out, you shouldn't use a ListTile for this, as you want to use more than just a title, subtitle for your content.
The reason I've picked a Stack over a Column here, is that it allows you to safely use it in different size screens whilst assuring the view won't overflow. With Column, it would use the button diameter as the whole space to use (vertically), even though it is restrict to the right side of the box.
I believe this is what you're looking for.
class MyListTile extends StatelessWidget {
const MyListTile({
Key key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: 180.0,
color: const Color(0xff0087a8),
child: Container(
padding: const EdgeInsets.all(20.0),
margin: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
color: const Color.fromRGBO(19, 12, 117, 1.0),
),
child: Stack(
children: <Widget>[
Text(
'EUR - USD',
style: Theme.of(context).textTheme.display2.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
Align(
alignment: Alignment.centerLeft,
child: Text(
'Forex',
style: TextStyle(color: Colors.white, fontSize: 20.0),
),
),
Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton(
backgroundColor: const Color(0xff0087a8),
onPressed: () {},
child: Icon(
Icons.add,
color: Colors.white,
size: 50.0,
),
),
)
],
),
),
);
}
}
Try this code
Container(
height: 180,
width: double.infinity,
child: Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'EUR/USD',
style: TextStyle(fontSize: 40),
),
Text('FOREX', style: TextStyle(fontSize: 20)),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.blueAccent,
child: IconButton(
onPressed: () {
print('On Pressed');
},
icon: Icon(Icons.add),
)),
],
)
],
),
),
),
)
Use custom widget for list item OR column from below code for single view. To align left and right you have to put your view in 'Align' as in below -
class _ItemsState extends State<Items> {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: SizedBox(
width: 380,
height: 160,
child: Card(
child: ListView.builder(
itemCount: 1,
itemBuilder: (context, index) {
return
Padding(padding: EdgeInsets.all(10.0),child: Column(children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Column(children: <Widget>[
Text(
'title',
style: TextStyle(fontSize: 20.0 , fontWeight: FontWeight.bold,),
),
Text(
'body',
style: TextStyle(fontSize: 14.0), )
]),
),
Align(
alignment: Alignment.centerRight,
child: CircleAvatar(
maxRadius: 20.0,
backgroundColor: Colors.blueGrey,
child: IconButton(
icon: Icon(Icons.add,
color: Colors.white,),
))
)
]));
}))));
}
}