ElevatedButton Position Changing method - flutter

I have an Elevated Button which is on of the bottom of the Page and I am a beginner sorry for this silly doubts but i can't figure out how to change the position of the button I dont know how to try positioned widget too. Kindly help me
I tried positioned widget but couldn't do well can anyone help me with this. here is my full code.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(
child: PageView.builder(
itemBuilder: (context, index)=> const OnBoardContent(
image: 'assets/splash-1.png',
description: "All under one roof with different approach"),
),
),
SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),
],
)
),
);
}
}
class OnBoardContent extends StatelessWidget {
const OnBoardContent({
Key? key,
required this.image,
required this.description,
}) : super(key: key);
final String image, description;
#override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(
height: 160,
),
const Text("Naz-Kearn",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold
)),
const Text("A simplify learning App",
style: TextStyle(
fontWeight: FontWeight.normal
),
),
Image.asset(image),
const SizedBox(
height: 50,
),
Text(description,
textAlign: TextAlign.center,
style: const TextStyle(fontWeight: FontWeight.normal),
),
],
);
}
}
Output of the above code

You need your widgets in a stack if you want to use Positioned widget on them :
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack( //wrapped the whole column with a stack so that all the other widgets doesn't get disturbed
children: [
Column(
children: [
Expanded(
child: PageView.builder(
itemBuilder: (context, index)=> const OnBoardContent(
image: 'assets/splash-1.png',
description: "All under one roof with different approach"),
),
),
],
),
Positioned(
top: MediaQuery.of(context).size.height*0.7, //change the 0.7 part to any number you like
child: SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),
),
],
)
),
);
}
}
class OnBoardContent extends StatelessWidget {
const OnBoardContent({
Key? key,
required this.image,
required this.description,
}) : super(key: key);
final String image, description;
#override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(
height: 160,
),
const Text("Naz-Kearn",
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold
)),
const Text("A simplify learning App",
style: TextStyle(
fontWeight: FontWeight.normal
),
),
Image.asset(image),
const SizedBox(
height: 50,
),
Text(description,
textAlign: TextAlign.center,
style: const TextStyle(fontWeight: FontWeight.normal),
),
],
);
}
}
try this code, you can use alignment property of the Stack widget to center everything.
SafeArea(
child: Stack(
alignment: Alignment.center, //do this
children: [

You can wrap your button with Padding widget which helps you to add padding as you like
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(
child: PageView.builder(
itemBuilder: (context, index)=> const OnBoardContent(
image: 'assets/splash-1.png',
description: "All under one roof with different approach"),
),
),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),),
],
)
),
);
}

Firstly please mention what precisely the issue you are facing. If you have a problem with the get started button, what is the expected place for the get started button in the design?
Based on the code given, I'm hoping that the get started button should be at the bottom of the screen with some space below. You have already placed the button at the bottom, but you are not able to give space below.
There are some possible ways, you can use it with the get started button component.
Instead of this,
SizedBox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: (){},
child: const Text("Tap to get started"),
),
),
Option 1
Use container with margin
Container(
height: 30,
width: 200,
margin: EdgeInsets.only(
bottom: 50,
),
child: ElevatedButton(
onPressed: () {},
child: const Text("Tap to get started"),
),
),
Option 2
Wrap existing SizedBox with padding widget
Padding(
padding: EdgeInsets.only(bottom: 50.0),
child: Sizedbox(
height: 30,
width: 200,
child: ElevatedButton(
onPressed: () {},
child: const Text("Tap to get started"),
),
),
),
Even with some more ways to move the button wherever you need, you can try your own with the following widgets Expanded(), Spacer(), SizedBox(), Positioned() and etc.

Related

Container should have the same height

I want that the red container has the same height as the blue container. The problem is, that the height of the blue container changes depending of the amount of lines. It would also help if the red container starts on the same height as blue container. The goal is to have a functional bullet point in the middle of the first line
My code is:
import 'package:flutter/material.dart';
class InhaltGesetz extends StatelessWidget {
final String text;
const InhaltGesetz({
Key? key,
required this.text,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
child: Container(
height: 70,
color: Colors.red,
child: const Align(
alignment: Alignment(0, -0.99),
child: Text(
'•',
style: TextStyle(fontSize: 25, color: Colors.white),
),
),
),
),
LayoutBuilder(
builder: (context, constraints) {
return Container(
width: 340,
color: Colors.blue,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(4, 8, 8, 8),
child: Text(
text,
style: const TextStyle(
fontSize: 15.5,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
);
},
),
],
);
}
}
I do not know what to do I tried to change the height manually with every container but i ended up with 9000+ lines of code and i will not do it any longer
You can use IntrinsicHeight widget(expensive), Follow this structure.
class InhaltGesetz extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
children: [
IntrinsicHeight(
child: Row(
children: [
Container(
width: 200,
color: Colors.red,
child: const Align(
alignment: Alignment(0, -0.99),
child: Text(
'•',
style: TextStyle(fontSize: 25, color: Colors.white),
),
),
),
Container(
width: 340,
height: 200,
color: Colors.blue,
),
],
),
),
],
);
}
}

Flutter - How 2 Give different width to different container enclosed in a single column?

I'm trying to develop a clone of an App idea from dribble, which can be found here.
Can you please tell me is the code I'm trying to write is correct or not??
As I've tried to wrap a Column with two text Widgets Enclosed in Container ( separate ) and when I'm trying to change the width of one container It changes the margin of the other also.
My Code is :-
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
Container(
margin: const EdgeInsets.only(top: 30, left: 20),
child: const Text(
'Find Intrested Events to Join',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0,
wordSpacing: 2,
),
),
width: 200,
),
// -----------------------------------------------------------------
Container(
margin: EdgeInsets.only(left: 20, top: 20),
width: 220,
child: Text(
'We share all events like Charity, workshop, Blood drive, etc.',
style: TextStyle(wordSpacing: 2, height: 1.4),
),
)
],
),
),
);
Can you tell me How to achieve that stage that I can change the Containers' width without affecting the margin of others.
It will be better to use Stack for this case while it contains background image, Also learn more about Stack, Align, Positioned and LayoutBuilder from flutter.dev
Play with this widget ,
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(child: LayoutBuilder(
builder: (context, constraints) {
return Stack(
children: [
Container(
color: Colors.amber.withOpacity(.4),
), //background image
Positioned(
left: 20,
top: 30,
child: SizedBox(
width: constraints.maxWidth * .5, // 50% of width
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'Find Intrested Events to Join',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30.0,
wordSpacing: 2,
),
),
SizedBox(
height: 20,
), //spaces between text
Text(
'We share all events like Charity, workshop, Blood drive, etc.',
style: TextStyle(wordSpacing: 2, height: 1.4),
),
],
),
),
),
Align(
// also Postisioned can be use
alignment: Alignment.bottomCenter,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("hereh"),
ElevatedButton(onPressed: () {}, child: Text("Button"))
],
),
)
],
);
},
)),
);
}
}

Scrollable Column and ListView.builder

I want to have a Column and ListView.builder to be scrollable using the SingleChildScrollView. I tried wrapping both the Column or the SingleChildScrollView with Expanded but I'm still not winning. I just want the ListView.builder and the Container above it to be scrollable. Please help...
I am getting an error that says : RenderFlex children have non-zero flex but incoming height constraints are unbounded.
import 'package:flutter/material.dart';
import 'package:ionicons/ionicons.dart';
import 'package:provider/provider.dart';
import '../widgets/product_item.dart';
import '../providers/products.dart';
class ProductsOverviewScreen extends StatelessWidget {
const ProductsOverviewScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final productsData = Provider.of<Products>(context);
final products = productsData.items;
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {},
icon: const Icon(
Ionicons.notifications_outline,
),
),
title: SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.1,
child: Image.asset(
'assets/logo.png',
fit: BoxFit.contain,
),
),
actions: [
IconButton(
onPressed: () {},
icon: const Icon(
Ionicons.cart_outline,
),
),
],
),
body: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 140,
width: MediaQuery.of(context).size.width * 0.9,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(26.0),
color: Colors.grey[300],
),
),
Row(
children: const [
Padding(
padding: EdgeInsets.only(
left: 15.0,
top: 20.0,
),
child: Text(
'Place Order',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18.0,
),
),
),
],
),
Expanded(
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: products.length,
itemBuilder: (ctx, i) => ProductItem(
products[i].id,
products[i].name,
products[i].size,
products[i].price,
products[i].image,
),
),
)
],
),
),
);
}
}
// The solution to your problem is to make the Listview non using enter code here scrollable and set shrinkwrap to true. Then wrap the first column in SingleChildScrollView. You don't need Expanded or Flexible Widget for this approach.
import 'package:flutter/material.dart';
class ProductsOverviewScreen extends StatelessWidget {
const ProductsOverviewScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final productsData = Provider.of<Products>(context);
final products = productsData.items;
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {},
icon: const Icon(
Icons.info_outline,
),
),
title: SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.1,
child: Image.asset(
'assets/logo.png',
fit: BoxFit.contain,
),
),
actions: [
IconButton(
onPressed: () {},
icon: const Icon(
Icons.shopping_cart_outlined,
),
),
],
),
body: SingleChildScrollView(
child: Column(
// mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 140,
width: MediaQuery.of(context).size.width * 0.9,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(26.0),
color: Colors.grey[300],
),
),
Row(
children: const [
Padding(
padding: EdgeInsets.only(
left: 15.0,
top: 20.0,
),
child: Text(
'Place Order',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18.0,
),
),
),
],
),
ListView.builder(
shrinkWrap: true,
itemCount: 10,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (ctx, i) => ProductItem(
products[i].id,
products[i].name,
products[i].size,
products[i].price,
products[i].image,
),
)
],
),
),
);
}
}
This is one approach among thousands of solutions, it consists of nesting one listview inside another. If you wish you can make the listview.builder horizontal.
import 'package:flutter/material.dart';
import 'package:ionicons/ionicons.dart';
import 'package:provider/provider.dart';
import '../widgets/product_item.dart';
import '../providers/products.dart';
class ProductsOverviewScreen extends StatelessWidget {
const ProductsOverviewScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final productsData = Provider.of<Products>(context);
final products = productsData.items;
return Scaffold(
appBar: AppBar(
leading: IconButton(
onPressed: () {},
icon: const Icon(
Ionicons.notifications_outline,
),
),
title: SizedBox(
width: MediaQuery.of(context).size.width * 0.3,
height: MediaQuery.of(context).size.height * 0.1,
child: Image.asset(
'assets/logo.png',
fit: BoxFit.contain,
),
),
actions: [
IconButton(
onPressed: () {},
icon: const Icon(
Ionicons.cart_outline,
),
),
],
),
body: ListView(
scrollDirection: Axis.vertical,
children: [
Container(
height: 140,
width: MediaQuery.of(context).size.width * 0.9,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(26.0),
color: Colors.grey[300],
),
),
Row(
children: const [
Padding(
padding: EdgeInsets.only(
left: 15.0,
top: 20.0,
),
child: Text(
'Place Order',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18.0,
),
),
),
],
),
ListView.builder(
scrollDirection: Axis.vertical,
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
primary: false,
itemCount: products.length,
itemBuilder: (ctx, i) => ProductItem(
products[i].id,
products[i].name,
products[i].size,
products[i].price,
products[i].image,
),
)
],
),
);
}
}
Looking at your code, you don't need the SingleChildScrollView widget. Just remove it. The listView is built with a scroll behaviour, you dont need to add it.
Please, let me know if you meant something else and I can help.
If you still have the Renderflex error, then wrap your Column widget inside a Flexible widget
Flexible(child: Column(children:[..

Flutter - overflowed by Infinity pixels on the bottom

I have problem with overflowed by Infinity pixels on the bottom. I'm find many solution but again not working.
This is my details page.
class MyWidgetApiDetails extends StatelessWidget {
final Emp emp;
const MyWidgetApiDetails({Key key, #required this.emp}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('DETAILS EMP'),
),
body: Column(
children: [
Hero(
tag: emp.empno,
child: AspectRatio(
aspectRatio: 16 / 9,
child: Image.network(emp.image))
),
Container(
height: 20,
width: 100,
child: PhotoView(
imageProvider: AssetImage("assets/images/map.jpg"),
),
),
PhotoView(
imageProvider: NetworkImage(emp.image),
),
SizedBox(
height: 20,
),
Text(
emp.empno.toString(),
style: TextStyle(fontSize: 15, fontWeight: FontWeight.normal),
),
SizedBox(
height: 20,
),
Text(
emp.ename,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
)
],
),
);
}
}
When put Column in SingleChildScrollView get RenderFlex object was given an infinite size during layout.
You can do by just changing from Column to ListView.
class MyWidgetApiDetails extends StatelessWidget {
final Emp emp;
const MyWidgetApiDetails({Key key, #required this.emp}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('DETAILS EMP'),
),
body: ListView(
children: [
Hero(
tag: emp.empno,
child: AspectRatio(
aspectRatio: 16 / 9,
child: Image.network(emp.image))
),
Container(
height: 20,
width: 100,
child: PhotoView(
imageProvider: AssetImage("assets/images/map.jpg"),
),
),
PhotoView(
imageProvider: NetworkImage(emp.image),
),
SizedBox(
height: 20,
),
Text(
emp.empno.toString(),
style: TextStyle(fontSize: 15, fontWeight: FontWeight.normal),
),
SizedBox(
height: 20,
),
Text(
emp.ename,
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
)
],
),
);
}
}
Put Column in SingleChildScrollView and wrap PhotoView in Container solve my problem.
That is because the SingleChildScrollView wants to be as large as possible, you need to put it into something that has constraints, like a Column, you could do something like this:
//pseudocode, not sure about the exact syntax.
body: Expanded(
child: SingleChildScrollView(
child: Column(...)
)
]
)
And I believe it should work.
Take a look here and here for more information.

Text widget always stays on top and doesn't disappear as I scroll

My text widget "More" always stays on top even though I scroll down, I'm not finding the root of this issue,
I've tried wrapping it in a SingleChildScrollView widget but still nothing has changed, are there any arguments for the Text widget that allows it to scroll as the user scrolls that I don't know of? Or is it something else entirely?
Code:
class MorePage extends StatefulWidget {
#override
_MorePageState createState() => _MorePageState();
}
class _MorePageState extends State<MorePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: white,
body: Stack(
// YESSSS
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center, children: [
SizedBox(height: 45.0),
Padding(
padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin),
child: SizedBox(
height: 25,
child: Text(
"MORE",
style: TextStyle(
color: black,
fontFamily: "Raleway",
fontWeight: FontWeight.w700,
fontSize: 25),
textAlign: TextAlign.center,
),
),),
]),
Container(
// YESSSS
child: getBody(),
),
],
),
);
}
You are using Stack(), Stack will overlay children on top of others.
while using Stack wrap with fixed-Sized Widget, It can be just Container(), also while using Stack, use Aligning widgets as child => Center, Align, Positioned, else use stack fit property,
to know more about Stack Visit https://api.flutter.dev/flutter/widgets/Stack-class.html
import 'package:flutter/material.dart';
class MorePage extends StatefulWidget {
#override
_MorePageState createState() => _MorePageState();
}
class _MorePageState extends State<MorePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Container(
child: Stack(
// YESSSS
children: <Widget>[
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
SizedBox(height: 45.0),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 22),
child: SizedBox(
height: 25,
child: Text(
"MORE",
style: TextStyle(
color: Colors.black,
fontFamily: "Raleway",
fontWeight: FontWeight.w700,
fontSize: 25),
textAlign: TextAlign.center,
),
),
),
]),
Center(
child: Container(
color: Colors.amber,
// YESSSS
height: 1333,
child: Text("Body")
// getBody(),
),
),
],
),
),
),
);
}
}