A RenderFlex overflowed by 4.8 pixels on the bottom. The relevant error-causing widget was Column - flutter

I am Getting An error (overflowed by 4.8 pixels on the bottom). I was creating an emailAuthSheet with showModalBottomSheet while debugging the app it shows the error of overflowed. Can u please guide me...And If Possible can you please also guide me on how to make it responsive for all devices as per their device screen size
The following code is below:
emailAuthSheet(BuildContext context) {
return showModalBottomSheet(
context: context,
builder: (context) {
return Container(
child:
Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
Provider.of<LandingService>(context,listen: false).passwordLessSignIn(context),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
color: constantColors.blueColor,
child: Text(
'Log in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {}),
MaterialButton(
color: constantColors.redColor,
child: Text(
'Sign in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {})
],
),
]),
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.0),
topRight: Radius.circular(15.0))));
});
}
Below is the pic of the flutter app and error

Wrap your container with singleChildScrollView
emailAuthSheet(BuildContext context) {
return showModalBottomSheet(
context: context,
builder: (context) {
return SingleChildScrollView(
child: Container(
child:
Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
color: constantColors.whiteColor,
),
),
Provider.of<LandingService>(context,listen: false).passwordLessSignIn(context),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
color: constantColors.blueColor,
child: Text(
'Log in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {}),
MaterialButton(
color: constantColors.redColor,
child: Text(
'Sign in',
style: TextStyle(
color: constantColors.whiteColor,
fontSize: 18.0,
fontWeight: FontWeight.bold),
),
onPressed: () {})
],
),
]),
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: constantColors.blueGreyColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15.0),
topRight: Radius.circular(15.0)))),
);
});
}

Just add 'isScrollControlled' parameter true to showModalBottomSheet.
And changed Column widget to Wrap widget.
(I removed some defines and widget because of leak of data)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState() {
super.initState();
}
emailAuthSheet(BuildContext context) {
return showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (context) {
return Container(
child: Wrap(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 150.0),
child: Divider(
thickness: 4.0,
),
),
Container(height: 450, color: Colors.blue[100]),
// Provider.of<LandingService>(context,listen: false).passwordLessSignIn(context),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
MaterialButton(
child: Text(
'Log in',
style: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.bold),
),
onPressed: () {}),
MaterialButton(
child: Text(
'Sign in',
style: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.bold),
),
onPressed: () {})
],
),
],
),
);
},
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {
emailAuthSheet(context);
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
return Container();
}
}

Related

Flutter: Moving from one dialog to another

How do I move from one dialog to another and close the previous one?
I made two dialogs and when I move to the next one the previous one does not close. I want the previous one, WelcomeDialog to close when I click continue and the next dialog open.
class WelcomeDialog extends StatelessWidget {
const WelcomeDialog({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
),
child: Stack(
alignment: Alignment.topCenter,
children: [
Container(
height: 220,
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 50, 20, 10),
child: Column(
children: [
const Text(
'Welcome!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.red,
),
),
const Text(
'Thank you for joining! Let\'s get you started.',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16),
),
Container(
margin: const EdgeInsets.only(top: 20),
child: OutlinedButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return const CommonDialog();
},
);
},
child: const Text(
'Continue',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
style: OutlinedButton.styleFrom(
side: const BorderSide(
style: BorderStyle.none,
),
backgroundColor: Colors.red,
),
),
),
],
),
),
)
],
),
);
}
}
class CommonDialog extends StatelessWidget {
const CommonDialog({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text(
'Update Information',
),
content: const Text('We need to update your information,'),
actions: [
TextButton(
onPressed: () {},
child: const Text('Continue'),
),
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
],
);
}
}

The named parameter 'body' isn't defined. in flutter code

I'm practising this code github to add a home screen with cards as pictures shown below. after the get started button clicks it should redirect to the home page with cards. cards are not still designed like the picture shows. the code I used to get cards is not working. can someone know how to fix this? no errors on main. dart. error is in home_page.dart. think I'm missing some codes.
error shows ----> Missing concrete implementation of 'StatefulWidget.createState'.
home_page.dart
import 'package:flutter/material.dart';
import 'package:fashion_app/color_filters.dart';
class HomePage extends StatefulWidget{
#override
_HomePageState createSatate ()=> _HomePageState();
}
class _HomePageState extends State <HomePage> {
final double _borderRadious = 24;
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fashion store'),
),
body: ListView(
padding: EdgeInsets.all(16),
children: [
buildQuoteCard(),
buildRoundedCard(),
buildColoredCard(),
buildImageCard(),
buildImageInteractionCard(),
],
),
)
);
}
Widget buildQuoteCard() =>
Card(
child: Padding(
padding: EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'If life were predictable it would cease to be life,
and be without flavor.',
style: TextStyle(fontSize: 24),
),
const SizedBox(height: 12),
Text(
'Eleanor Roosevelt',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
],
),
),
);
Widget buildRoundedCard() =>
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: Container(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Rounded card',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
'This card is rounded',
style: TextStyle(fontSize: 20),
),
],
),
),
);
Widget buildColoredCard() =>
Card(
shadowColor: Colors.red,
elevation: 8,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.red],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Colored card',
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 4),
Text(
'This card is rounded and has a gradient',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
],
),
),
);
Widget buildImageCard() =>
Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Stack(
alignment: Alignment.center,
children: [
Ink.image(
image: NetworkImage(
'https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1327&q=80',
),
colorFilter: ColorFilters.greyscale,
child: InkWell(
onTap: () {},
),
height: 240,
fit: BoxFit.cover,
),
Text(
'Card With Splash',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24,
),
),
],
),
);
Widget buildImageInteractionCard() =>
Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Column(
children: [
Stack(
children: [
Ink.image(
image: NetworkImage(
'https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1327&q=80',
),
height: 240,
fit: BoxFit.cover,
),
Positioned(
bottom: 16,
right: 16,
left: 16,
child: Text(
'Cats rule the world!',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 24,
),
),
),
],
),
Padding(
padding: EdgeInsets.all(16).copyWith(bottom: 0),
child: Text(
'The cat is the only domesticated species in the family Felidae and is often referred to as the domestic cat to distinguish it from the wild members of the family.',
style: TextStyle(fontSize: 16),
),
),
ButtonBar(
alignment: MainAxisAlignment.start,
children: [
FlatButton(
child: Text('Buy Cat'),
onPressed: () {},
),
FlatButton(
child: Text('Buy Cat Food'),
onPressed: () {},
)
],
)
],
),
);
}
> main. dart
import 'package:flutter/material.dart';
import 'constants.dart';
import 'home_page.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Auth Screen 1',
theme: ThemeData(
brightness: Brightness.light,
primaryColor: kPrimaryColor,
scaffoldBackgroundColor: kBackgroundColor,
textTheme: TextTheme(
headline4: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
button: TextStyle(color: kPrimaryColor),
headline6: TextStyle(color: Colors.white70, fontWeight: FontWeight.normal),
),
inputDecorationTheme: InputDecorationTheme(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.white.withOpacity(.2),
),
),
),
),
home: WelcomeScreen(),
);
}
}
class WelcomeScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0XFFd5ae48),
body: Column(
children: <Widget>[
Expanded(
child: Container(
height: MediaQuery.of(context).size.height*.4,
padding: EdgeInsets.all(10.0),
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/girl.png"),
fit: BoxFit.cover,
),
),
),
flex: 3,
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
RichText(
textAlign: TextAlign.left,
text: TextSpan(
children: [
TextSpan(
text: "Let Your Styles Speaks\n",
style: Theme.of(context).textTheme.headline4,
),
TextSpan(
text: "Discover the latest trends in women fashion and explore your personality\n",
style: Theme.of(context).textTheme.headline6,
)
],
),
),
FittedBox(
child: GestureDetector(
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return HomePage();
},
));
},
child: Container(
margin: EdgeInsets.only(bottom: 25),
padding: EdgeInsets.symmetric(horizontal: 26, vertical: 16),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
color: Colors.black,
),
child: Row(
children: <Widget>[
Text(
"Get started",
style: Theme.of(context).textTheme.button?.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20
),
),
SizedBox(width: 10),
Icon(
Icons.arrow_forward,
color: Colors.white,
)
],
),
),
),
),
],
),
),
],
),
);
}
}
https://api.flutter.dev/flutter/widgets/StatefulWidget/createState.html
Change this,
class HomePage extends StatefulWidget{
#override
_HomePageState createSatate ()=> _HomePageState(); // change createSatate to createState
}
To,
class HomePage extends StatefulWidget{
#override
_HomePageState createState ()=> _HomePageState();
}
class HomePage extends StatefulWidget{
#override
_HomePageState createSatate ()=> _HomePageState();
}
It's because of a typo.
Change createSatate to createState.
Your Homepage class will look like
class HomePage extends StatefulWidget{
#override
_HomePageState createState ()=> _HomePageState();
}

How to make that setState work correctly?

I have a code.There is a floatingActionButton which have SpeedDial.I have two buttons in SpeedDial.
However I have only with delete button.I want that when I click it Text notes it change color.I tried to use setState but it dont work correctly.I dont know why my programm work in this way because all classes are stateful.I tried to check it with printing in console.I console it write that i want but it dont work
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:notev2/scr/func.dart';
import 'colors.dart';
import 'variables.dart';
import 'newnote.dart';
class homepage extends StatefulWidget {
const homepage({Key? key}) : super(key: key);
#override
_homepageState createState() => _homepageState();
}
class _homepageState extends State<homepage> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
toolbarHeight: 0.0,
),
backgroundColor: dark,
body: Column(
children: <Widget>[
Row(
children: <Widget>[
//надпись заметки
Container(
margin: EdgeInsets.only(bottom: 35),
child: Text(
"Notes",
style: TextStyle(
fontSize: 72,
color: delmode==0?grey:coral,
),
),
),
Container(
child: Text(
"Never Settle",
style: TextStyle(color: grey.withOpacity(0.5)),
),
),
SizedBox(
width: 10,
), // лупа
Container(
margin: EdgeInsets.only(top: 0),
child: IconButton(
icon: Icon(Icons.search_rounded),
color: coral,
iconSize: 30,
onPressed: () {},
),
),
Container(
child: IconButton(
icon: Icon(Icons.menu),
color: coral,
iconSize: 30,
onPressed: () {},
),
),
],
),
Expanded(
child: Expanded(
child: ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, index) {
return GestureDetector(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: grey,
width: 1,
),
),
height: 50,
child: Center(
child: Text(
data[index]['title'],
style: TextStyle(
fontSize: 35,
color: grey,
),
),
),
),
onTap: () {
//print(index.toString() + " is taped");
buff = index;
shower(index, data, tContr, dContr);
Navigator.pushNamed(context, 'watchpage');
},
);
/*ListTile(
title:Text(tList[index]),
);*/
},
),
),
),
Spacer(),
],
),
floatingActionButton:
/*FloatingActionButton(
backgroundColor: dark,
shape: RoundedRectangleBorder(
side: BorderSide(
color: Color.fromRGBO(255, 103, 104, 1),
width: 2,
style: BorderStyle.solid),
borderRadius: BorderRadius.circular(50)),
onPressed: () {
buff = data.length + 1;
Navigator.pushNamed(context, "newnote");
},
child: Icon(
Icons.add,
color: coral,
),
),*/
_getFAB(),
);
}
Widget _getFAB() {
return SpeedDial(
overlayOpacity: 0.0,
shape: RoundedRectangleBorder(
side: BorderSide(color: grey, width: 2, style: BorderStyle.solid),
borderRadius: BorderRadius.circular(50)),
animatedIcon: AnimatedIcons.menu_close,
animatedIconTheme: IconThemeData(
size: 22,
),
backgroundColor: light,
visible: true,
curve: Curves.bounceIn,
children: [
// FAB 1
SpeedDialChild(
child: Icon(Icons.add),
backgroundColor: light,
onTap: () {
buff = data.length + 1;
Navigator.pushNamed(context, "newnote");
},
label: 'Button 1',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
color: Colors.white,
fontSize: 16.0),
labelBackgroundColor: light),
// FAB 2
SpeedDialChild(
child: Icon(Icons.delete),
backgroundColor: light,
onTap: () {
setState(() {
print("delmode" + delmode.toString());
delmode = !delmode;
});
},
label: 'Button 2',
labelStyle: TextStyle(
fontWeight: FontWeight.w500,
color: Colors.white,
fontSize: 16.0),
labelBackgroundColor: light)
],
);
}
}

Change Background Color and Text Color of a button onPressed and set back to default when unPressed Flutter

I am new to flutter. I have two buttons in a row and I want to change the colour and the text of the first button when it is pressed to something else and if the second button is pressed the first button should go back to the default colour and the second button should go to the new colour. Is there any way I can achieve this by indexing?
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: primaryColor,
onPressed: () {},
child: Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Column(
children: [
Text('Stunning Solo', style: TextStyle(fontSize: 15,color: Colors.white)),
Text('Current Plan', style: TextStyle(fontSize: 12,color: Colors.white)),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("\$$dollars3",style: TextStyle(fontSize: 20,color: Colors.white)),
),
Text(
"Free forever",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 13,
color: whiteColor),
),
],
),
),
),
),
SizedBox(height: 20),
SizedBox(
height: 150,
width: MediaQuery.of(context).size.width * 0.45,
child: RaisedButton(
color: primaryColor,
onPressed:
() {},
child: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Text('Startup Pack', style: TextStyle(fontSize: 15,color: Colors.white)),
),
Text('Introductory Offer', style: TextStyle(fontSize: 12,color: Colors.white)),
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"\$$dollars",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
decoration: TextDecoration.lineThrough),
),
SizedBox(width: 5),
Text(
"\$$dollars2",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: whiteColor),
),
]),
You can use ToggleButton for this.
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<bool> buttonsState = [
true,
false
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: ToggleButtons(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
color: Colors.blue,
fillColor: Colors.blue,
isSelected: buttonsState,
children: <Widget>[
Padding(
padding: EdgeInsets.only(
left: 8,
right:8,
),
child: Text(
"Free forever", style: _getTextStyle(0),
textAlign: TextAlign.center,
),
),
Padding(
padding: EdgeInsets.only(
left: 8,
right: 8,
),
child: Text(
"Startup Pack", style: _getTextStyle(1),
textAlign: TextAlign.center,
),
),
],
onPressed: _updateButtonState,
borderRadius: BorderRadius.all(Radius.circular(8.0)),
),
),
);
}
_updateButtonState(int index) {
setState(() {
for (int buttonIndex = 0; buttonIndex < buttonsState.length; buttonIndex++) {
if (buttonIndex == index) {
buttonsState[buttonIndex] = true;
} else {
buttonsState[buttonIndex] = false;
}
}
});
}
TextStyle _getTextStyle(int index) {
return buttonsState[index]
? TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.white) : TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
color: Colors.blue);
}
}
You can just use TextButton since it provides state type. It is also the replacement of FlatButton.
Example if you want to change foregroundColor which are text or icon color:
TextButton(
onPressed: onPressed,
child: Text("label"),
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith(
(Set<MaterialState> states) {
if (states.contains(MaterialState.pressed)) {
return pressedColor;
} else if (states.contains(MaterialState.selected)) {
return pressedColor;
} else if (states.contains(MaterialState.error)) {
return redPrimary;
} else if (states.contains(MaterialState.disabled)) {
return darkSecondary;
}
return darkSecondary;
},
),
...
),
);

Navigator.push() shows a Black Screen

I am trying to make a fruithero flutter app but while changing my main page to another page, It shows only a black screen on my android emulator in laptop and on my phone, but in https://flutlab.io/ it shows perfectly, What is the problem that the app is not working on local emulator or in my phone but working on online IDE
Showing an Eror like this: -
There are multiple heroes that share the same tag within a subtree.
main.dart
import 'package:flutter/material.dart';
import './detailsPage.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF21BFBD),
body: ListView(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15.0, left: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios),
color: Colors.white,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsPage(),
),
);
},
),
Container(
width: 125.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(Icons.filter_list),
color: Colors.white,
onPressed: () {},
),
IconButton(
icon: Icon(Icons.menu),
color: Colors.white,
onPressed: () {},
),
],
),
),
],
),
),
SizedBox(
height: 25.0,
),
Padding(
padding: EdgeInsets.only(left: 40.0),
child: Row(
children: <Widget>[
Text(
'Healthy',
style: TextStyle(
fontFamily: 'Mont',
color: Colors.white,
fontSize: 25.0,
fontWeight: FontWeight.bold),
),
SizedBox(width: 10.0),
Text(
'Food',
style: TextStyle(
fontFamily: 'Mont', color: Colors.white, fontSize: 25.0),
),
],
),
),
SizedBox(
height: 40.0,
),
Container(
height: MediaQuery.of(context).size.height - 185.0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.only(topLeft: Radius.circular(75.0))),
child: ListView(
primary: false,
padding: EdgeInsets.only(left: 25.0, right: 20.0),
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 45.0),
child: Container(
height: MediaQuery.of(context).size.height / 1.68,
child: ListView(
children: <Widget>[
_buildFoodItem('images/one.png', 'Salmon', '\$24.0'),
_buildFoodItem('images/two.png', 'Spring', '\$22.0'),
_buildFoodItem('images/three.png', 'Sprite', '\$34.0'),
_buildFoodItem('images/one.png', 'Mut', '\$12.0')
],
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
height: 65.0,
width: MediaQuery.of(context).size.width / 6,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
style: BorderStyle.solid,
width: 1.0,
),
borderRadius: BorderRadius.circular(20)),
child: Center(
child: Icon(
Icons.search,
color: Colors.black,
),
),
),
Container(
height: 65.0,
width: MediaQuery.of(context).size.width / 6,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
style: BorderStyle.solid,
width: 1.0,
),
borderRadius: BorderRadius.circular(20)),
child: Center(
child: Icon(
Icons.shopping_cart,
color: Colors.black,
),
),
),
Container(
height: 65.0,
width: MediaQuery.of(context).size.width / 2,
decoration: BoxDecoration(
color: Color(0xff170F1F),
border: Border.all(
color: Colors.grey,
style: BorderStyle.solid,
width: 1.0,
),
borderRadius: BorderRadius.circular(20)),
child: Center(
child: Text(
'Checkout',
style: TextStyle(
fontSize: 18.0,
color: Colors.white,
fontFamily: 'Mont',
),
)),
),
],
),
],
),
),
],
),
);
}
Widget _buildFoodItem(String imgPath, String foodName, String price) {
return Padding(
padding: EdgeInsets.only(left: 10.0, right: 10.0, top: 10.0),
child: InkWell(
onTap: () {
Navigator.push( //Here is the Navigator.push()
context,
MaterialPageRoute(builder: (BuildContext context) {
return DetailsPage();
}),
);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Row(
children: [
Hero(
tag: imgPath,
child: Image(
image: AssetImage(imgPath),
fit: BoxFit.cover,
height: 75.0,
width: 75.0,
),
),
SizedBox(width: 10.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
foodName,
style: TextStyle(
fontFamily: 'Mont',
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
Text(
price,
style: TextStyle(
fontFamily: 'Mont',
fontSize: 15.0,
color: Colors.grey),
)
],
),
],
),
),
IconButton(
icon: Icon(Icons.add),
color: Colors.black,
onPressed: () {},
),
],
),
),
);
}
}
Navigator.push code is like this:
Navigator.push(
context,
MaterialPageRoute(builder: (BuildContext context) {
return DetailsPage();
}),
);
detailsPage.dart
import 'package:flutter/material.dart';
class DetailsPage extends StatefulWidget {
#override
_DetailsPageState createState() => _DetailsPageState();
}
class _DetailsPageState extends State<DetailsPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF7A9BEE),
appBar: AppBar(
leading: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: Icon(Icons.arrow_back_ios),
color: Colors.white,
),
backgroundColor: Colors.transparent,
elevation: 0.0,
title: Text(
'Details',
style: TextStyle(
fontFamily: 'Mont',
fontSize: 18.0,
color: Colors.white,
),
),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.more_horiz),
onPressed: () {},
color: Colors.white,
),
],
),
);
}
}
Here is git Repository link: 1
i dont see the hero widget in the detailsPage.dart, when you set up a Hero widget in main and Navigate to detailsPage, you have to set up a hero widget to receive a hero transition from the main page, and because you have a listView in main you have to give a dynamic tag to each list item because each hero must have a tag associated with it so when it transfer to different layout it knows which hero to transit to it,
you already set up a hero tag in main page and you already give it a tag as tag: imgPath
tag is like an id for that hero, now you need to set the same tag (id) in the detailsPage,
please watch this to understand the logic Hero Flutter Widget
You should use Hero Widgets on both screens.
I couldn't find the Hero widget, nor the tag and not the image that you used on your first screen too (AssetImage(imgPath))
Try and parameterize using the propoer Hero specs.
Here you can find an good example and instructions: Flutter example