Why is ElevatedButton getting stretched and becoming a background? - flutter

I'm new on flutter and I don't have an idea why ElevatedButton stretched and become a background. I just want a simple button. How can I solve this problem? Here's the image below:
Here's my code
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: SafeArea(
child: Center(
child: Column(
children: [
Image.asset(
'profile_image.jpg', height: 150, width: 150,),
const SizedBox(height: 15,),
Text("Hello World"),
const SizedBox(height: 15,),
const Text('Go back'),
],
)
)
),
),
)
);
}
}

You are using button on body. that's why full body acting as button.
body: Center(
child: ElevatedButton(
What you seek is just wrapping the Text('Go back')), with ElevatedButton.
return Scaffold(
body: Center(
child: SafeArea(
child: Center(
child: Column(
children: [
Image.asset(
'profile_image.jpg',
height: 150,
width: 150,
),
const SizedBox(
height: 15,
),
Text("Hello World"),
const SizedBox(
height: 15,
),
ElevatedButton(
onPressed: () {
// Navigator.pop(context);
},
child: const Text('Go back')),
],
))),
));

Related

How to stack two bottom sheet in flutter?

I want to stack two bottom sheet each other in flutter as show in photo. The upper one is shown when in error state. In photo, it build with alert dialog. I want is with bottom sheet. How can I get it?
Edit:
Here is my code that I want to do. Lower bottom sheet is with pin field, autoComplete. autoComplete trigger StreamController, and then streamBuilder watch Error state and show dialog.
confirmPasswordModalBottomSheet(
BiometricAuthRegisterBloc biometricAuthRegBloc) {
showMaterialModalBottomSheet(
context: context,
builder: (BuildContext context) {
return StreamBuilder(
stream: biometricAuthRegBloc.biometricAuthRegisterStream,
builder: (context,AsyncSnapshot<ResponseObject>biometricAuthRegSnapShot) {
if (biometricAuthRegSnapShot.hasData) {
if (biometricAuthRegSnapShot.data!.messageState ==
MessageState.requestError) {
showModalBottomSheet(context: context, builder:
(BuildContext context){
return Container(
width: 200,height: 200,
child: Center(child: Text('Helllllllllo'),),);
});
}
}
return SizedBox(
width: 100,
height: 300,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: margin30,
),
Text(CURRENT_PIN_TITLE),
SizedBox(
height: margin30,
),
Padding(
padding: const EdgeInsets.only(
left: margin60, right: margin60),
child: PinCodeField(
pinLength: 6,
onChange: () {},
onComplete: (value) {
biometricAuthRegBloc.biometricAuthRegister(
biometricType:_biometricAuthTypeForApi,
password: value);
},
),
),
SizedBox(
height: margin30,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal:
margin80),
child: AppButton(
onClick: () {},
label: CANCEL_BTN_LABEL,
),
),
Container(
padding: const EdgeInsets.all(8.0),
margin:
EdgeInsets.symmetric(vertical: 8.0,
horizontal: 30),
decoration: BoxDecoration(
color: Colors.grey,
border: Border.all(color: Colors.black),
),
child: const Text(
FINGER_PRINT_DIALOG,
textAlign: TextAlign.center,
),
)
],
),
);
});
},
);
}
When I do like that above, I get setState() or markNeedsBuild() called during build. Error and why? Sorry for my previous incomplete question.
I am bit confused with your question but stacking two bottomsheet is just easy. You just need to call the showModalBottomSheet whenever you want it shown to user. You can check out the following implementation:
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: ElevatedButton(
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 500,
color: Colors.amber,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet 1'),
ElevatedButton(
child: const Text('Show second modal 2'),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.redAccent,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet 2'),
ElevatedButton(
child: const Text('Close BottomSheet'),
onPressed: () => Navigator.pop(context),
),
],
),
),
);
},
);
},
),
],
),
),
);
},
);
},
child: Text('Show bottom sheet 1'),
),
);
}
}
I have solution. All I need to do is, need to add WidgetBinding.insatance.addPostFrameCallback((timeStamp){showModalBottomSheet()}); in the StreamBuilder return.

Error: The getter 'context' isn't defined for the class 'MyApp'

I want to make an app that contain 4 buttons and it will go to other page if I click the button. But when I use the navigator it shows an error at context.
import 'package:flutter/material.dart';
import 'package:mytestapp/LectureVideo.dart';
import 'package:path/path.dart';
void main() {
runApp( MaterialApp(
debugShowCheckedModeBanner: false, //hide debug sash at top right
title: 'HomePage',
home: MyApp(),
)
);
}
class MyApp extends StatelessWidget{
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width; //getting screen width
return Scaffold(
appBar: AppBar(title: Text('Welcome to EMTeach'),
centerTitle: true,
backgroundColor: Colors.lime[200],
),
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(margin: EdgeInsets.only(bottom: 20.0)),
FirstButton(width),
Container(margin: EdgeInsets.only(bottom: 20.0)),
SecondButton(width),
Container(margin: EdgeInsets.only(bottom: 20.0)),
ThirdButton(width),
Container(margin: EdgeInsets.only(bottom: 20.0)),
FourthButton(width),
Container(margin: EdgeInsets.only(bottom: 20.0)),
],
),
),
);
}
Widget FirstButton(double width) {
return Expanded(
child: SizedBox(
width: width - 20,
child: RaisedButton(
onPressed: () {
Navigator.push(
context, //error
MaterialPageRoute(builder: (context) => LectureVideo()));
},
color: Colors.red,
child: Text(
"Lecture Video",
textScaleFactor: 2.0,
),
),
//fit: BoxFit.cover,
),
);
}
Widget SecondButton(double width) {
return Expanded(
child: SizedBox(
width: width - 20,
child: RaisedButton(
onPressed: () {},
color: Colors.lightGreen,
child: Text(
"Application Video",
textScaleFactor: 2.0,
),
),
//fit: BoxFit.cover,
),
);
}
Widget ThirdButton(double width) {
return Expanded(
child: SizedBox(
width: width - 20,
child: RaisedButton(
onPressed: () {},
color: Colors.lightBlue,
child: Text(
"List of Equation",
textScaleFactor: 2.0,
),
),
//fit: BoxFit.cover,
),
);
}
Widget FourthButton(double width) {
return Expanded(
child: SizedBox(
width: width - 20,
child: RaisedButton(
onPressed: () {},
color: Colors.orange,
child: Text(
"List of Question",
textScaleFactor: 2.0,
),
),
//fit: BoxFit.cover,
),
);
}
}
the problem is you are not passing the context which is the parameter of the build function over to your buttons try this:
Widget FirstButton(double width,BuildContext context ) {
return Expanded(
child: SizedBox(
width: width - 20,
child: RaisedButton(
onPressed: () {
Navigator.push(
context, //error
MaterialPageRoute(builder: (context) => LectureVideo()));
},
color: Colors.red,
child: Text(
"Lecture Video",
textScaleFactor: 2.0,
),
),
//fit: BoxFit.cover,
),
);
}

I am getting findAncestorStateOfType error in flutter . What should I do about this? I am stuck here

How to solve findAncestorStateOfType error in Flutter? I am not able to navigate to other page using these codes. What is wrong with this code?
The error which I am getting is this
The method 'findAncestorStateOfType' was called on null.
Receiver: null
Tried calling: findAncestorStateOfType()
My code is this:
// entry point for the app,
// the => operator is shorthand for {} when there is only one line of code
void main() {
runApp(MaterialApp(
home: HomeRoute(),
));
}
// the root widget of our application
class HomeRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Color(0xFFFAAC98),
appBar: AppBar(
backgroundColor: Color(0xFFFAAC98),
),
body: myLayoutWidget(),
),
);
}
}
// replace this method with code in the examples below
Widget myLayoutWidget() {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 300,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
color: const Color(0xff89C5CC),
),
child: Center(
child: new Text(
'Gram Panchayat App',
style: TextStyle(
fontSize: 27,
fontWeight: FontWeight.bold,
color: Color(0xFF2F3676)),
),
),
),
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 18.0, top: 18.0),
child: Image.asset(
'assets/images/human1.png',
width: 150,
height: 150,
),
),
Padding(
padding: const EdgeInsets.only(left: 58.0),
child: elevatedButton(),
),
],
),
new Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 28.0),
child: elevatedButton1(),
),
Padding(
padding: const EdgeInsets.only(top: 58.0, left: 68.0),
child: new Image.asset(
'assets/images/human2.png',
width: 200,
height: 170,
),
),
],
),
Padding(
padding: const EdgeInsets.only(left: 60.0),
child: Row(children: [
new Image.asset(
'assets/images/img3.png',
width: 180,
height: 80,
),
]),
)
],
),
);
}
ElevatedButton elevatedButton() => ElevatedButton(
onPressed: () {
BuildContext context;
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
child: Text('Citizen'),
);
ElevatedButton elevatedButton1() =>
ElevatedButton(onPressed: () {}, child: Text('Staff'));
class SecondRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}```
*Please help me I am stuck. I have gone through many sites but couldnt find what is wrong and the solution also.*
make myLayoutWidget() inside your HomeRoute class
and use
Navigator.of(context,rootNavigator:true).pop();
instead of
Navigator.pop(context);

How can I have two buttons in the bottom using align widget inside the center and column widgets?

I am trying to have a grouped button with two individuals and place them in the bottom. It is a login screen so there are other things.
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: Center(
child: Column(
children: <Widget>[
SizedBox(
height: 75.0,
),
Text('Login'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
color: Colors.orange,
onPressed: () {
print('login');
},
),
RaisedButton(
onPressed: () {
print('signup');
},
),
],
),
],
),
),
),
);
}
}
I've been trying with like align bottom and stuffs. But with Row(), it does not work with Center + Column widget. How can I send these two buttons to the bottom of the Center widget?
There are multiple ways to do this. One of them is to use the Spacer widget:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: Center(
child: Column(
children: <Widget>[
SizedBox(
height: 75.0,
),
Spacer(),
Text('Login'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
color: Colors.orange,
onPressed: () {
print('login');
},
),
RaisedButton(
onPressed: () {
print('signup');
},
),
],
),
],
),
),
),
);
Another one would be to reverse the column and start from the bottom, but most probably the side effects are not wanted.
Also, one option would be to divide the whole screen into fixed height partitions with the help of MediaQuery.of(context).size.height value so that your buttons are always at the bottom.
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: Center(
child: Column(
children: <Widget>[
SizedBox(
height: 75.0,
),
Text('Login'),
Expanded(
child:Container
(
alignment: Alignment.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
color: Colors.orange,
onPressed: () {
print('login');
},
),
RaisedButton(
onPressed: () {
print('signup');
},
),
],
),
)
)
],
),
),
),
);
}

How can I use List of Scaffold in Scaffold body in Flutter?

First of all, in my app when the user draws something and and then press save button, the user user should see the same drawing on his/her phone but on a shorter scale, because he/she can draw more drawings and save them, I wanted to show all of them in a singleChildScrollView. I have tried, but I am getting man errors.
I want to show a grid of different screen on one screen so that whenever the user tap on one of the screens then the screen will expand fully on to the screen.
Builder(
builder: (context) => Column(
children: [
Expanded(
child: Container(
padding: EdgeInsets.all(0),
height: height,
color: Colors.white,
child: SingleChildScrollView(
child: Form(
key: formKey,
child: Column(
children: <Widget>[
/////////////////////////////////////////////////////////////////////////////////////////////////////
//This below is the code that I tried.
widget.drawModel != null
? Transform.scale(
scale: 0.5,
child: Scaffold(
body: Container(
constraints: BoxConstraints(
maxHeight:
MediaQuery.of(context).size.height /
2,
maxWidth:
MediaQuery.of(context).size.width / 2,
),
child: CustomPaint(
painter: Draw(
points: widget
.drawModel[
widget.drawModel.length - 1]
.points),
),
),
),
)
: SizedBox(height: 10),
///////////////////////////////////////////////////////////////////////////////////////////////////
images.length != 0
// List view for images
? Column(
children: <Widget>[
for (int i = 0; i < images.length; i++)
Padding(
padding: EdgeInsets.symmetric(
horizontal: ImageColumnPad * width),
child: Dismissible(
key: ObjectKey(images[i]),
onDismissed: (direction) {
var item = images.elementAt(i);
deleteItem(i);
Scaffold.of(context).showSnackBar(
SnackBar(
shape: RoundedRectangleBorder(),
content: Text("Item deleted",
style:
TextStyle(fontSize: 15)),
action: SnackBarAction(
label: "UNDO",
onPressed: () {
undoDeletion(i, item);
},
),
),
);
},
child: GestureDetector(
onTap: () => {
//TODO: Implement delete function here
},
child: Center(
child: Image.file(
images[i],
fit: BoxFit.contain,
),
),
),
),
),
],
)
: SizedBox(height: 2),
...
...
...
You can wrap the body with Column.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Route'),
),
body: Scaffold(
body: Center(
child: RaisedButton(
child: Text('Open route'),
onPressed: () {},
),
),
),
);
}