Why are my values not updating in the Home page? Flutter - flutter

So i was making a weather app , but it does not update itself when i search for a city it just works for the first time and after that if I search any city name the values won't update at all.
Here is the main Homepage code
import 'package:flutter/material.dart';
import 'package:glass_kit/glass_kit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:percent_indicator/percent_indicator.dart';
import '../model/model.dart';
import '../services/remote_services.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
#override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Post? posts;
var isLoaded=true;
var pressure ;
var city = new TextEditingController();
#override
getData(String city) async{
posts=await RemoteService().getPosts(city);
if(posts!=null){
setState((){
isLoaded= true;
print("Weather");
print(posts?.wind.speed);
print(MediaQuery.of(context).size.height);
print(MediaQuery.of(context).size.width);
pressure= posts?.main.pressure;
});
}
}
#override
Widget build(BuildContext context) {
var timezone;
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/Clouds.jpg"),
fit: BoxFit.cover,
),
),
child: SafeArea(
child: SingleChildScrollView(
child: Visibility(
visible: isLoaded,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Row(
children: [
GlassContainer.clearGlass(
borderWidth: 0,
height: 50,
width: MediaQuery.of(context).size.width-70,
borderRadius: BorderRadius.circular(10),
child: TextField(
controller: city,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white
),
decoration: InputDecoration(
hintText: "Enter City",
),
),
),
Expanded(child: SizedBox()),
GlassContainer.clearGlass(
height: 50,
width: 50,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
child: Center(
child: IconButton(
onPressed: (){
getData(city.text);
},
icon: Icon(Icons.search,color: Colors.white,),
),
),
)
],
),
SizedBox(height: 5,),
Row(
children: [
Column(
children: [
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.water, color: Colors.white,),
Text(
posts==null?" ":"${posts?.main.humidity}%",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
)
],
),
),
SizedBox(height: 5,),
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.wind, color: Colors.white,),
Text(
posts==null?" ":"${posts?.wind.speed}",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
),
Text(
"Km/h",
style: TextStyle(
fontSize: 12,
color: Colors.white
),
)
],
),
)
],
),
Expanded(child: SizedBox()),
GlassContainer.clearGlass(
width: MediaQuery.of(context).size.width/1.51102941,
height: MediaQuery.of(context).size.height/3.77731092-10,
borderRadius: BorderRadius.circular(10),
borderWidth: 0,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
posts==null?" ":"${posts?.main.temp} C",
style: TextStyle(
fontSize: 75,
color: Colors.white,
),
),
SizedBox(height: 10,),
Text(
posts==null?"":"Feels like ${posts?.main.feelsLike} C",
style: TextStyle(
fontSize: 18,
color: Colors.white
),
)
],
),
),
),
Expanded(child: SizedBox()),
],
),
SizedBox(height: 7,),
Row(
children: [
GlassContainer.clearGlass(
width: MediaQuery.of(context).size.width/1.51102941,
height: MediaQuery.of(context).size.height/3.77731092-10+12,
borderRadius: BorderRadius.circular(10),
borderWidth: 0,
child: new CircularPercentIndicator(
radius: 90.0,
lineWidth: 13.0,
animation: true,
percent: pressure==null?0:pressure/1084,
center: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon((pressure!=null)?(pressure>1013)?FontAwesomeIcons.arrowUp:FontAwesomeIcons.arrowDown:FontAwesomeIcons.arrowDown, color: Colors.white,),
Text(
"$pressure mB",
style:
new TextStyle(fontWeight: FontWeight.bold, fontSize: 30, color: Colors.white),
),
],
),
circularStrokeCap: CircularStrokeCap.round,
progressColor: Colors.white,
),
),
Expanded(child: SizedBox()),
Column(
children: [
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.temperatureArrowUp, color: Colors.white,),
Text(
"${posts?.main.tempMax} C",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
)
],
),
),
SizedBox(height: 18,),
GlassContainer.clearGlass(
height: MediaQuery.of(context).size.height/8.099,
width: MediaQuery.of(context).size.width/3.990,
borderWidth: 0,
borderRadius: BorderRadius.circular(10),
blur: 10,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FaIcon(FontAwesomeIcons.temperatureArrowDown, color: Colors.white,),
Text(
"${posts?.main.tempMin} C",
style: TextStyle(
color: Colors.white,
fontSize: 24
),
)
],
),
),
],
),
Expanded(child: SizedBox())
],
),
// GlassContainer.clearGlass(height: height, width: width)
],
),
),
),
),
),
),
);
}
}
setState is not working as it should i would be glad if some fixed this as soon as possible
Here is the code for the RemoteService class, it requests from the Uri.
import 'package:http/http.dart' as http;
import '../model/model.dart';
class RemoteService{
Future <Post?> getPosts(String city) async{
var client = http.Client();
var uri = Uri.parse("https://api.openweathermap.org/data/2.5/weather?q=$city&appid=5e749d88f2df02cacc9be6abf8088531&units=metric");
var response = await client.get(uri);
if (response.statusCode==200){
var json=response.body;
return postFromJson(json);
}
}
}

Once the data is loaded posts is no more null which doesnt satisfy the condition mentioned in the get data method. Please remove the condition if posts!= null in getdata. Also please remove override mentioned above getdata if you have added it by mistake.

Try updating all parameters in setstate() just like pressure. This will work.

Related

Flutter : Radio button

i want to turn my three buttons to radio button , so When you click on any of them, the border color and background color are changed , like the one in the middle
my button code
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.white,
width: 1,
),
),
height: 65,
width: 350,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('1 WEEK' , style: TextStyle(
fontSize: 18,
color: Colors.white,
),),
Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text('2.99' , style: TextStyle(
fontSize: 18,
color: Colors.white,
),),
Text('/Week' , style: TextStyle(
fontSize: 11,
color: Colors.white60,
),),
],
),
Container(
width: 25,
height: 25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(
color: Colors.white,
width: 1,
),
),
),
],
),
),
Radiobuttonwidget
//this array help us to manage the state of radio button.
var ischecked = [true, false, false];
class MyRadioButton extends StatefulWidget {
const MyRadioButton({Key? key}) : super(key: key);
#override
State<MyRadioButton> createState() => _MyRadioButtonState();
}
class _MyRadioButtonState extends State<MyRadioButton> {
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=true;
ischecked[1]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[0]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[0]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[1]=true;
ischecked[0]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[1]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[1]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=false;
ischecked[2]=true;
ischecked[1]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[2]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[2]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
],
);
}
}
SAmpleCode Dartpad live
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
shrinkWrap: true,
children: [MyRadioButton()],
),
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
var ischecked = [true, false, false];
class MyRadioButton extends StatefulWidget {
const MyRadioButton({Key? key}) : super(key: key);
#override
State<MyRadioButton> createState() => _MyRadioButtonState();
}
class _MyRadioButtonState extends State<MyRadioButton> {
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=true;
ischecked[1]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[0]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[0]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[1]=true;
ischecked[0]=false;
ischecked[2]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[1]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[1]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 16),
child: InkWell(
onTap: (){
setState(() {
ischecked[0]=false;
ischecked[2]=true;
ischecked[1]=false;
});
},
child: Container(
decoration: BoxDecoration(
color: ischecked[2]?Colors.lightGreen:Colors.transparent,
borderRadius: BorderRadius.circular(15),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
height: 65,
// width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
flex: 2,
child: Text(
'1 WEEK',
style: TextStyle(
fontSize: 18,
color: Colors.black,
),
),
),
Expanded(
flex: 2,
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
'2.99',
style: TextStyle(
fontSize: 18,
color: Colors.grey,
),
),
Text(
'/Week',
style: TextStyle(
fontSize: 11,
color: Colors.black26,
),
),
],
),
),
Flexible(
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
color: ischecked[2]?Colors.red:Colors.transparent,
borderRadius: BorderRadius.circular(25),
border: Border.all(
color: Colors.black54,
width: 1,
),
),
),
),
],
),
),
),
),
],
);
}
}

How do I pass data to a screen while using auto_route in flutter?

I have a home page with 5 bottomnavbar items (just like instagram) and I need to pass a boolean to one of the pages. I'm using auto_route to navigate.
AutoRoute(
path: '/home',
name: 'HomeRouter',
page: Home,
children: [
AutoRoute(
path: 'timeline',
name: 'TimelineRouter',
page: Timeline,
),
AutoRoute(
path: 'profile',
name: 'ProfileRouter',
page: EmptyRouterPage,
children: [
AutoRoute(
path: '',
page: Profile, <-----
),
AutoRoute(
path: ":currentUserId",
name: "EditProfileRouter",
page: EditProfile,
),
],
),
],
)
I need to pass a boolean to the profile page but I'm not sure how to do that using auto_route. Any hints?
When I passed a boolean, the page received a null value for some reason. What could be the possible reason for getting a null value?
This answer is coming straight from the auto_route API documentation:
AutoRoute automatically detects and handles your page arguments for
you, the generated route object will deliver all the arguments your
page needs including path/query params.
e.g. The following page widget will take an argument of type Book.
class BookDetailsPage extends StatelessWidget {
const BookDetailsPage({required this.book});
final Book book;
...
The generated BookDetailsRoute will deliver the same arguments to it's
corresponding page.
router.push(BookDetailsRoute(book: book));
I used a bool value on the profile page to remove the leading icon while I am calling on the bottom navbar so I am sharing my code I Hope this will work for you.Thanks
////Bottom Navigation page
import 'package:flutter/material.dart';
import 'package:traveling/helpers/AppColors.dart';
import 'package:traveling/screens/Employee/home/Home.dart';
import 'package:traveling/screens/Employee/profile/Profile.dart';
import 'package:traveling/screens/Employee/setting/Setting.dart';
class EmployeeBottomNavBar extends StatefulWidget {
const EmployeeBottomNavBar({Key? key}) : super(key: key);
#override
_EmployeeBottomNavBarState createState() => _EmployeeBottomNavBarState();
}
class _EmployeeBottomNavBarState extends State<EmployeeBottomNavBar> {
int pageIndex = 0;
bool visible = true;
List<Widget> pageList = <Widget>[EmployeeHome(), EmployeeProfile(leadingIcon: false,), Setting()];
#override
Widget build(BuildContext context) {
return Scaffold(
body: pageList[pageIndex],
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.redAccent[400],
currentIndex: pageIndex,
onTap: (value) {
setState(() {
pageIndex = value;
});
},
// type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xff2161c0),
),
child: Icon(
Icons.home,
color: AppColors.white,
),
),
icon: Icon(
Icons.home,
color: Color(0xff2161c0),
),
label: ""),
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xff2161c0),
),
child: Icon(
Icons.person,
color: AppColors.white,
),
),
icon: Icon(
Icons.person,
color: AppColors.baseLightBlueColor,
),
label: ""),
BottomNavigationBarItem(
activeIcon: Container(
height: 40,
width: 80,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: AppColors.baseLightBlueColor,
),
child: Icon(
Icons.settings,
color: AppColors.white,
),
),
icon: Icon(
Icons.settings,
color: AppColors.baseLightBlueColor,
),
label: ""),
]
)
);
}
}
///////Profile page
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:traveling/helpers/AppColors.dart';
import 'package:traveling/helpers/AppStrings.dart';
class EmployeeProfile extends StatefulWidget {
final bool leadingIcon;
EmployeeProfile({Key? key, required this.leadingIcon}) : super(key: key);
#override
_EmployeeProfileState createState() => _EmployeeProfileState();
}
class _EmployeeProfileState extends State<EmployeeProfile> {
bool? visible;
double? _width;
File ?_image;
final picker = ImagePicker();
Future<void>_showChoiceDialog(BuildContext context)
{
return showDialog(context: context,builder: (BuildContext context){
return AlertDialog(
title: Text("Choose option",style: TextStyle(color: AppColors.hotelListDarkBlue),),
content: SingleChildScrollView(
child: ListBody(
children: [
Divider(height: 1,color: AppColors.baseLightBlueColor,),
ListTile(
onTap: (){
Navigator.pop(context,_getImage(ImageSource.gallery));
},
title: Text("Gallery",style: TextStyle(color: AppColors.hotelListDarkBlue),),
leading: Icon(Icons.account_box,color: AppColors.baseLightBlueColor,),
),
Divider(height: 1,color: AppColors.baseLightBlueColor,),
ListTile(
onTap: (){
Navigator.pop(context,_getImage(ImageSource.gallery));
},
title: Text("Camera",style: TextStyle(color: AppColors.hotelListDarkBlue),),
leading: Icon(Icons.camera,color: AppColors.baseLightBlueColor,),
),
],
),
),);
});
}
#override
Widget build(BuildContext context) {
_width = MediaQuery.of(context).size.width;
return Scaffold(
body: ListView(
shrinkWrap: true,
children: [
Container(
width: _width!,
color: AppColors.white,
child: Stack(
children: [
Column(
children: [
Material(
color: AppColors.baseLightBlueColor,
elevation: 15,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.only(bottomRight: Radius.circular(30)),
),
child: Container(
height: 180,
decoration: BoxDecoration(
color: AppColors.baseLightBlueColor,
// AppColors.blue,
borderRadius:
BorderRadius.only(bottomRight: Radius.circular(30)),
),
),
),
Container(
// color: AppColors.green,
width: _width! * 0.90,
height: 140,
margin: EdgeInsets.only(top: 60),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
// crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.personalInformation,
// "Informations personelles",
style: TextStyle(
color: Color(0xff919AAA),
),
)
],
),
Row(
children: [
Container(
height: 100,
width: 50,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
AppStrings.name,
style: TextStyle(
fontSize: 15,
color: AppColors.hotelListDarkBlue,
),
),
Text(
AppStrings.email,
style: TextStyle(
fontSize: 15,
color: AppColors.hotelListDarkBlue,
),
),
Text(
AppStrings.phone,
// "Phone",
style: TextStyle(
fontSize: 15,
color: AppColors.hotelListDarkBlue,
),
),
],
),
),
SizedBox(
height: 100,
width: 30,
),
Container(
height: 100,
width: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Laure Beno",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: AppColors.hotelListDarkBlue,
),
),
Text(
"laure.beno#gmail.com",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: AppColors.hotelListDarkBlue,
),
),
Text("+33 (0)6 45 23 65 77",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: AppColors.hotelListDarkBlue,
)),
],
),
),
],
),
],
),
),
Container(
width: _width! * 0.87,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
AppStrings.myPoetryModes,
style: TextStyle(
color: AppColors.hotelListLightGreyColor,
),
),
Text(
"+ " + AppStrings.add,
style: TextStyle(
color: AppColors.hotelListLightGreyColor,
),
),
],
),
),
SizedBox(
height: 20,
),
Container(
width: _width! * 0.90,
child: atmCard(
cardName: "VISA",
image: "assets/Visa.png",
height: 10)),
Container(
width: _width! * 0.90,
child: atmCard(
cardName: "Master Card",
image: "assets/MasterCard.png",
height: 20)),
],
),
Positioned(
top: 80,
child: _image==null?Stack(
children: [
Container(
height: 150,
width:_width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 120,
width: 120,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(60),
border: Border.all(color: AppColors.white, width: 2.0),
image: DecorationImage(
image: AssetImage("assets/managerPicture.jpeg"),fit: BoxFit.cover,
)
/*gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [
Color(0xff00519E),
AppColors.blue,
Colors.blue.shade900,
],
)*/
),
/* child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Image.asset("assets/manager.jpeg",height: 100,width: 100,)
//Icon(Icons.person, size: 100),
// child: Image.asset("assets/logo/profile.png", fit: BoxFit.fill),
),*/
),
],
),
),
Positioned(
top: 60,
right: 0,
left: 100,
bottom: 0,
child: GestureDetector(
onTap: (){
_showChoiceDialog(context);
},
child: Container(
height: 10,
width: _width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 35,
width: 35,
decoration: BoxDecoration(
color: AppColors.profiePicBackground,
borderRadius: BorderRadius.circular(60),
//border: Border.all(color: Colors.blue.shade900, width: 2.0),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Icon(MdiIcons.squareEditOutline,color: AppColors.popUpBlueColor,size: 20,),
),
),
],
),
),
),)
],
):Stack(
children: [
Container(
height: 150,
width:_width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 120,
width: 120,
decoration: BoxDecoration(
color: Colors.white,
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: FileImage(_image!),
),
border: Border.all(color: AppColors.white, width: 2.0),
),
),
],
),
),
Positioned(
top: 60,
right: 0,
left: 100,
bottom: 0,
child: GestureDetector(
onTap: (){
_showChoiceDialog(context);
},
child: Container(
height: 10,
width: _width,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 35,
width: 35,
decoration: BoxDecoration(
color: AppColors.profiePicBackground,
borderRadius: BorderRadius.circular(60),
//border: Border.all(color: Colors.blue.shade900, width: 2.0),
),
child: ClipRRect(
borderRadius: BorderRadius.circular(60.0),
child: Icon(MdiIcons.squareEditOutline,color: AppColors.popUpBlueColor,size: 20,),
),
),
],
),
),
),)
],
)
),
Positioned(
top: 40,
child: Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.only(left:8,right: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
widget.leadingIcon == true
? Align(
alignment: Alignment.topLeft,
child: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(
MdiIcons.arrowLeft,
color: Colors.white,
)),
)
: Container(),
Align(
alignment: Alignment.topCenter,
child: Text(AppStrings.myAccount,
style: TextStyle(
fontSize: 15,
color: AppColors.white,
fontWeight: FontWeight.bold)),
),
Align(
alignment: Alignment.topRight,
child: GestureDetector(
onTap: () {},
child: Icon(
MdiIcons.pencilOutline,
color: AppColors.white,
)),
),
],
),
)
),
],
),
),
],
),
);
}
//ImageSource: Camera and Gallery.
_getImage(ImageSource imageSource) async
{
PickedFile? imageFile = await picker.getImage(source: imageSource);
//if user doesn't take any image, just return.
if (imageFile == null) return;
setState(
() {
//Rebuild UI with the selected image.
_image = File(imageFile.path);
},
);
}
Widget atmCard({String? image, String? cardName, double? height}) {
return GestureDetector(
onTap: () {
// Navigator.push(
// context, MaterialPageRoute(builder: (context) => Class()));
},
child: Card(
elevation: 2,
margin: EdgeInsets.only(bottom: 15, right: 5, left: 5),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Container(
height: 80,
// width: _width! * 0.90,
padding: EdgeInsets.only(
// top: 5,
left: 20,
// right: 20,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15), color: Colors.white),
child: Row(
children: [
Image.asset(
image.toString(),
height: height,
),
//Icon(Icons.atm,color: AppColors.hotelListDarkBlue,),
SizedBox(
width: 30,
),
Container(
width: MediaQuery.of(context).size.width * 0.62,
height: 50,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
cardName.toString(),
// "MasterCard",
style: TextStyle(
color: AppColors.hotelListDarkBlue,
fontSize: 12,
fontWeight: FontWeight.bold),
),
Text(
"4******12345678",
style: TextStyle(
fontSize: 14,
color: AppColors.hotelListLightGreyColor),
),
],
),
)
],
),
),
),
);
}
}
You can define your path arguments like this in autoRoute class :
AutoRoute(page: TrackAttendancePage, path: 'track-attendance/:id')
Then in your page pass them as argument using #PathParam
class TrackAttendancePage extends StatefulWidget {
final String registerId;
const TrackAttendancePage(#PathParam('id') this.registerId,{Key? key})
: super(key: key);
}

Cant align widgets correctly in Flutter

I have the following code, but i cant figure out, how to align the username to the left (next to the image) and the class-text to the right of the row. MainAxisAlignment.spaceBetween doesnt do the trick. I tried several different alignments on all the rows and columns but nothing is working. the only way i would get space between the two texts is by adding padding to one of the texts but this is not what want because the usernames have different sizes and the class text wouldnt be alligned to the right.
Container(
width: 180,
decoration: BoxDecoration(
color: const Color(0xffe8d9c3),
border: Border.all(color: Colors.transparent),
borderRadius: const BorderRadius.all(
Radius.circular(4),
),
boxShadow: const [
BoxShadow(
color: Colors.black,
blurRadius: 2.0,
spreadRadius: 0.0,
offset: Offset(0, 1),
),
],
),
child: Row(
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const CircleAvatar(
backgroundImage: NetworkImage(
'https://www.woolha.com/media/2020/03/eevee.png',
),
),
const SizedBox(
width: 5,
),
Column(
//mainAxisAlignment: MainAxisAlignment.spaceBetween,
//crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
StreamBuilder<DocumentSnapshot>(
stream: userdoc.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
return Text(
"${snapshot.data?["username"]}",
style: const TextStyle(
fontSize: 10,
color: Colors.black,
),
);
},
),
const Text(
"Class 8",
style: TextStyle(
fontSize: 10,
color: Colors.black,
),
),
],
),
Stack(
clipBehavior: Clip.none,
children: const [
SizedBox(
width: 120,
height: 15,
child: LinearProgressIndicator(
value: 0.3,
backgroundColor: Color(0xff816840),
color: Color(0xffffc518),
),
),
Positioned(
right: 10,
top: 2,
child: Text(
"2918",
style: TextStyle(
fontSize: 10,
color: Colors.white,
),
),
),
],
),
],
),
],
),
)
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Center(
child: Container(
width: 180,
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: const Color(0xffe8d9c3),
border: Border.all(color: Colors.transparent),
borderRadius: const BorderRadius.all(
Radius.circular(4),
),
boxShadow: const [
BoxShadow(
color: Colors.black,
blurRadius: 2.0,
spreadRadius: 0.0,
offset: Offset(0, 1),
),
],
),
child: Row(
// crossAxisAlignment: CrossAxisAlignment.center,
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
const CircleAvatar(
backgroundImage: NetworkImage(
'https://www.woolha.com/media/2020/03/eevee.png',
),
),
const SizedBox(
width: 5,
),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Row(
children: const [
Text(
"username",
style: TextStyle(
fontSize: 10,
color: Colors.black,
),
),
Spacer(),
Text(
"Class 8",
style: TextStyle(
fontSize: 10,
color: Colors.black,
),
),
],
),
Stack(
clipBehavior: Clip.none,
children: const [
SizedBox(
height: 15,
child: LinearProgressIndicator(
value: 0.5,
backgroundColor: Color(0xff816840),
color: Color(0xffffc518),
),
),
Positioned(
right: 10,
top: 2,
child: Text(
"2918",
style: TextStyle(
fontSize: 10,
color: Colors.white,
),
),
),
],
),
],
),
),
],
),
),
)),
);
}
}
you can use Spacer() widget between them

How to increment for a specific in Flutter?

enter image description here Like in my sample image, below, I want to increase or decrease the quantity by clicking the button for a single list item. If I increase the counter in setState (), its increment in each item in the list. I need help with this, especially managing a specific list item in Flutter Flutter.
Container(
height: 500.0,
width: double.infinity,
child: GridView.count(
crossAxisCount: 2,
children:
List.generate(userData == null ? 0 : userData.length, (index) {
return Container(
child: SizedBox(
width: 700,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.network(
"${userData[index]["image"]}",
width: 300,
height: 100,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: IconButton(
color: Colors.blue,
icon: const Icon(Icons.remove_circle,
size: 35.0),
onPressed: _decrementCount),
),
Container(
child: CircleAvatar(
radius: 20.0,
backgroundColor: Colors.white,
child: Text(
_counter.toString(),
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
),
),
Container(
child: IconButton(
color: Colors.orange,
icon: const Icon(Icons.add_circle,
size: 35.0),
onPressed: () {
_incrementCount(userData[index]);
},
),
),
],
),
Container(
height: 20.0,
child: Text(
"${userData[index]["libelle"]}",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.orange,
),
),
),
],
))),
);
})),
)
Make a separate Stateful class for your List item.
Like this
List.generate(2, (index) => MyCustomWidget(
user[index]
))
class MyCustomWidget extends StatefulWidget {
final user;
const MyCustomWidget({Key? key,required this.user}) : super(key:key);
#override
_MyCustomWidgetState createState() => _MyCustomWidgetState();
}
class _MyCustomWidgetState extends State<MyCustomWidget> {
#override
Widget build(BuildContext context) {
return Container(
child: SizedBox(
width: 700,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Image.network(
"${widget.user["image"]}",
width: 300,
height: 100,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: IconButton(
color: Colors.blue,
icon: const Icon(Icons.remove_circle,
size: 35.0),
onPressed: _decrementCount),
),
Container(
child: CircleAvatar(
radius: 20.0,
backgroundColor: Colors.white,
child: Text(
_counter.toString(),
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
),
),
Container(
child: IconButton(
color: Colors.orange,
icon: const Icon(Icons.add_circle,
size: 35.0),
onPressed: () {
_incrementCount(userData[index]);
},
),
),
],
),
Container(
height: 20.0,
child: Text(
"${widget.user["libelle"]}",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.orange,
),
),
),
],
))),
);
}
}
set some
conditions ? Image.network(
"${userData[index]["image"]}",
width: 300,
height: 100,
), : Image.network(
"${userData[index]["image"]}",
width: 400,
height: 250,
),
So if condition true then it's display like first one other wise take second one

handling application layout in flutter app

I have Entry page to my app like below, I am getting bottom overflow , image size issue , text font issue, when I run on the phone with less than 5 inches,
If I run the same app on over 5 inches I am getting like below
Can anyone who have worked on developing flutter apps help me on how I can adjust according to the screen?
also How to adjust text size, image size every other things as per the screen size?
below is my code:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Color colorTheme;
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => new _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
TextEditingController controllerEmail = TextEditingController();
TextEditingController controllerPassword = TextEditingController();
String username, password;
Widget loginButtonChild = const Text(
"Log in",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white, fontFamily: "OpenSans-Regular", fontSize: 16),
);
Widget loginButtonWithCircle = Row(
children: <Widget>[
const Text(
"Log in",
style: TextStyle(
color: Colors.white,
fontFamily: "OpenSans-Regular",
),
),
CircularProgressIndicator(),
],
);
#override
Widget build(context) {
double maxHeight = MediaQuery.of(context).size.height;
// ScreenUtil.instance = ScreenUtil.getInstance()..init(context);
return Scaffold(
resizeToAvoidBottomPadding: true,
body: SingleChildScrollView(
child: LimitedBox(
maxHeight: maxHeight * 1,
child: Stack(
//fit: StackFit.expand,
children: <Widget>[
Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.05,
left: MediaQuery.of(context).size.width * 0.05),
child: Image.asset('assets/Heat Map.png',
width: 100, height: 20, fit: BoxFit.fill),
)
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.all(Radius.circular(15))),
padding: EdgeInsets.all(
// MediaQuery.of(context).size.width * 0.09,
MediaQuery.of(context).size.width * 0.1,
),
child: Image.asset(
'assets/layer_1_3.png',
// color: Color(0xFFe31735),
// width: 300,
width: MediaQuery.of(context).size.width * 0.72,
// height: 300,
height: MediaQuery.of(context).size.height * 0.5,
fit: BoxFit.contain,
),
),
Positioned(
left: 78.0,
bottom: 10.0,
child: RichText(
text: TextSpan(
text: 'APP',
style: TextStyle(
fontWeight: FontWeight.bold, color: Colors.black),
children: <TextSpan>[
TextSpan(
text: '\nDevelopment',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.normal,
),
),
TextSpan(
text: '\nFlutter',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.normal,
),
),
],
),
maxLines: 3,
overflow: TextOverflow.ellipsis,
softWrap: true,
),
),
],
),
SingleChildScrollView(
child: new Container(
padding: const EdgeInsets.fromLTRB(20.0, 0.0, 40.0, 40.0),
child: new Form(
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 20),
child: Icon(Icons.person_outline,
size: 38, color: Colors.black),
),
SizedBox(width: 10.0),
new Expanded(
child: new TextFormField(
style: TextStyle(color: Colors.black),
controller: controllerEmail,
//cursorColor: , make it yellow later TODO
decoration: new InputDecoration(
labelText: "Username",
enabledBorder: new UnderlineInputBorder(
borderSide: new BorderSide(
color: Colors.red,
))),
keyboardType: TextInputType.text,
),
),
],
),
Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 20),
child: Icon(
Icons.lock_outline,
size: 38,
color: Colors.black,
),
),
SizedBox(width: 10.0),
new Expanded(
child: new TextFormField(
controller: controllerPassword,
style: TextStyle(color: Colors.red),
decoration: new InputDecoration(
labelText: "Password",
enabledBorder: new UnderlineInputBorder(
borderSide: new BorderSide(
color: Colors.red,
))),
obscureText: true,
keyboardType: TextInputType.text,
),
),
],
),
Padding(
padding: EdgeInsets.only(
top: 0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {
},
child: Text(
"Forgot password?",
style: TextStyle(
color: Colors.grey,
fontFamily: "OpenSans-Regular",
),
),
)
],
),
),
SizedBox(
height: MediaQuery.of(context).size.width * 0.04,
),
RaisedButton(
shape: RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(25.0),
side: BorderSide(color: Colors.red)),
color: Colors.red,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 48),
child: loginButtonChild,
),
const SizedBox(
width: 45.0,
height: 45.0,
),
Icon(Icons.arrow_forward,
color: Colors.white),
],
),
onPressed: () {
}),
],
),
),
),
),
],
),
],
),
),
),
);
}
}
You can use SingleChildScrollView to avoid overflow error just wrap your widget with SingleChildScrollView
Example:-
SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height,
child:YourWidget();
)
)