I am Trying to pass a function to a static callback, i have added the code below.
I am using a plugin like system alert window that is the call back, it has a tag with is pass to button. the same as system Alert window, to draw over other app, the draw over screen has a button(the static call Back function). i am trying to pass a function to this call call back so that when the button is pressed it is able to perform something inside the app
This is the Alert window body code with the button. The button doesnt have an onpresse function, instead it is identified by a string tag.
final header = float.FloatyHeadHeader(
title: float.FloatyHeadText(
text: "Ongoing Ride",
fontSize: 12,
textColor: Colors.black45,
fontWeight: float.FontWeight.normal,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
padding: float.FloatyHeadPadding.setSymmetricPadding(12, 12),
subTitle: float.FloatyHeadText(
text: 'Name $riderName',
fontSize: 16,
fontWeight: float.FontWeight.bold,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
textColor: Colors.black87,
),
decoration: float.FloatyHeadDecoration(startColor: Colors.grey[100]),
button: float.FloatyHeadButton(
text: float.FloatyHeadText(
fontWeight: float.FontWeight.bold,
text: "Personal",
fontSize: 10,
textColor: Colors.black45,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
tag: "personal_btn"),
);
final body = float.FloatyHeadBody(
rows: [
float.EachRow(
columns: [
float.EachColumn(
text: float.FloatyHeadText(
fontWeight: float.FontWeight.bold,
text: "Updated body $pickUpAdd",
fontSize: 12,
textColor: Colors.black45,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
),
],
gravity: float.ContentGravity.center,
),
float.EachRow(columns: [
float.EachColumn(
text: float.FloatyHeadText(
text: "Updated long data of the body",
fontSize: 12,
textColor: Colors.black87,
fontWeight: float.FontWeight.bold,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
padding: float.FloatyHeadPadding.setSymmetricPadding(6, 8),
decoration: float.FloatyHeadDecoration(
startColor: Colors.black12, borderRadius: 25.0),
margin: float.FloatyHeadMargin(top: 4),
),
], gravity: float.ContentGravity.center),
float.EachRow(
columns: [
float.EachColumn(
text: float.FloatyHeadText(
text: "Notes",
fontSize: 10,
textColor: Colors.black45,
fontWeight: float.FontWeight.normal,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
),
],
gravity: float.ContentGravity.left,
margin: float.FloatyHeadMargin(top: 8),
),
float.EachRow(
columns: [
float.EachColumn(
text: float.FloatyHeadText(
text: "Updated random notes.",
fontSize: 13,
textColor: Colors.black54,
fontWeight: float.FontWeight.bold,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
),
],
gravity: float.ContentGravity.left,
),
],
padding: float.FloatyHeadPadding(left: 16, right: 16, bottom: 12, top: 12),
);
final footer = FloatyHeadFooter(
buttons: [
float.FloatyHeadButton(
text: float.FloatyHeadText(
text: "Simple button",
fontSize: 12,
textColor: Color.fromRGBO(250, 139, 97, 1),
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
fontWeight: float.FontWeight.normal,
),
tag: "simple_button",
padding: float.FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
width: 0,
height: float.FloatyHeadButton.WRAP_CONTENT,
decoration: float.FloatyHeadDecoration(
startColor: Colors.white,
endColor: Colors.white,
borderWidth: 0,
borderRadius: 0.0),
),
float.FloatyHeadButton(
text: float.FloatyHeadText(
fontWeight: float.FontWeight.normal,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
text: "Focus button",
fontSize: 12,
textColor: Colors.white,
),
tag: "focus_button",
width: 0,
padding: float.FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
height: float.FloatyHeadButton.WRAP_CONTENT,
decoration: float.FloatyHeadDecoration(
startColor: Color.fromRGBO(250, 139, 97, 1),
endColor: Color.fromRGBO(247, 28, 88, 1),
borderWidth: 0,
borderRadius: 30.0),
)
],
padding: float.FloatyHeadPadding(left: 16, right: 16, bottom: 12),
decoration: float.FloatyHeadDecoration(startColor: Colors.white),
buttonsPosition: float.ButtonPosition.center,
);
Here is the call back containing the String tag for the button, when i click the button it prints out the result but i unable to pass a woking function to the button.
static void floatCallBack(String tag) {
print('CALLBACK FROM FRAGMENT BUILDED: $tag');
switch (tag) {
case "simple_button":
// float.callback;Dispatcher();
print('CALLBACK-FROM Static void: $tag');
break;
case "updated_simple_button":
break;
case "focus_button":
print("Focus button has been called");
break;
default:
print("OnClick event of $tag");
}
}
Here is an example of the function i want to pass to the alert window button.
void openInstalledMapToPickUp(){
double destiLat = widget.rideDetails.pickUpLat;
double destLng = widget.rideDetails.pickUpLng;
MapsSheet.show(
context: context,
onMapTap: (map) {
map.showDirections(
destination: mapLaunch.Coords(
destiLat,
destLng,
),
destinationTitle: 'Drop off Location',
origin: mapLaunch.Coords(
currentPosition.latitude,
currentPosition.longitude,
),
originTitle: "My Current Location",
// waypoints: waypoints,
directionsMode: directionsMode,
);
},
);
// print('pickup $startLat');
// print('pickup $startLng');
print('dropoff lat $destiLat');
print('dropoff lat $destLng ');
}
Here is a full code, on the page i have a Raised button which works as expexted, i would also like to pass to have the alertwindow button do thesame thing the raised button does
class NewRideScreen extends StatefulWidget
{
#override
_NewRideScreenState createState() => _NewRideScreenState();
}
class _NewRideScreenState extends State<NewRideScreen> with TickerProviderStateMixin
{
final float.FloatyHead floatyHead = float.FloatyHead();
final header = float.FloatyHeadHeader(
title: float.FloatyHeadText(
text: "Ongoing Ride",
fontSize: 12,
textColor: Colors.black45,
fontWeight: float.FontWeight.normal,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
padding: float.FloatyHeadPadding.setSymmetricPadding(12, 12),
subTitle: float.FloatyHeadText(
text: 'Name $riderName',
fontSize: 16,
fontWeight: float.FontWeight.bold,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
textColor: Colors.black87,
),
decoration: float.FloatyHeadDecoration(startColor: Colors.grey[100]),
button: float.FloatyHeadButton(
text: float.FloatyHeadText(
fontWeight: float.FontWeight.bold,
text: "Personal",
fontSize: 10,
textColor: Colors.black45,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
tag: "personal_btn"),
);
final body = float.FloatyHeadBody(
rows: [
float.EachRow(
columns: [
float.EachColumn(
text: float.FloatyHeadText(
fontWeight: float.FontWeight.bold,
text: "Updated body $pickUpAdd",
fontSize: 12,
textColor: Colors.black45,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
),
],
gravity: float.ContentGravity.center,
),
float.EachRow(columns: [
float.EachColumn(
text: float.FloatyHeadText(
text: "Updated long data of the body",
fontSize: 12,
textColor: Colors.black87,
fontWeight: float.FontWeight.bold,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
padding: float.FloatyHeadPadding.setSymmetricPadding(6, 8),
decoration: float.FloatyHeadDecoration(
startColor: Colors.black12, borderRadius: 25.0),
margin: float.FloatyHeadMargin(top: 4),
),
], gravity: float.ContentGravity.center),
float.EachRow(
columns: [
float.EachColumn(
text: float.FloatyHeadText(
text: "Notes",
fontSize: 10,
textColor: Colors.black45,
fontWeight: float.FontWeight.normal,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
),
],
gravity: float.ContentGravity.left,
margin: float.FloatyHeadMargin(top: 8),
),
float.EachRow(
columns: [
float.EachColumn(
text: float.FloatyHeadText(
text: "Updated random notes.",
fontSize: 13,
textColor: Colors.black54,
fontWeight: float.FontWeight.bold,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
),
),
],
gravity: float.ContentGravity.left,
),
],
padding: float.FloatyHeadPadding(left: 16, right: 16, bottom: 12, top: 12),
);
final footer = FloatyHeadFooter(
buttons: [
float.FloatyHeadButton(
text: float.FloatyHeadText(
text: "Simple button",
fontSize: 12,
textColor: Color.fromRGBO(250, 139, 97, 1),
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
fontWeight: float.FontWeight.normal,
),
tag: "simple_button",
padding: float.FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
width: 0,
height: float.FloatyHeadButton.WRAP_CONTENT,
decoration: float.FloatyHeadDecoration(
startColor: Colors.white,
endColor: Colors.white,
borderWidth: 0,
borderRadius: 0.0),
),
float.FloatyHeadButton(
text: float.FloatyHeadText(
fontWeight: float.FontWeight.normal,
padding: float.FloatyHeadPadding(
bottom: 4,
left: 5,
right: 5,
top: 5,
),
text: "Focus button",
fontSize: 12,
textColor: Colors.white,
),
tag: "focus_button",
width: 0,
padding: float.FloatyHeadPadding(left: 10, right: 10, bottom: 10, top: 10),
height: float.FloatyHeadButton.WRAP_CONTENT,
decoration: float.FloatyHeadDecoration(
startColor: Color.fromRGBO(250, 139, 97, 1),
endColor: Color.fromRGBO(247, 28, 88, 1),
borderWidth: 0,
borderRadius: 30.0),
)
],
padding: float.FloatyHeadPadding(left: 16, right: 16, bottom: 12),
decoration: float.FloatyHeadDecoration(startColor: Colors.white),
buttonsPosition: float.ButtonPosition.center,
);
String status = "accepted";
String arrivedStatus = 'ContinueTrip';
String onRideStatus = 'ContinueTrip';
String durationRide="";
bool isRequestingDirection = false;
String btnTitle = "Arrived";
String arrivedBtnTitle = "Continue Trip";
String onRideBtnTitle = "Continue Trip";
Color btnColor = Colors.black87;
Color arrivedBtnColor = Colors.green;
Color onRideBtnColor = Colors.green;
Timer timer;
int durationCounter = 0;
bool arrivedStat = false;
bool onRideStat = false;
bool statStat = false;
#override
void initState() {
super.initState();
Wakelock.enable();
float.FloatyHead.registerOnClickListener(floatCallBack);
}
static void floatCallBack(String tag) {
print('CALLBACK FROM FRAGMENT BUILDED: $tag');
switch (tag) {
case "simple_button":
// float.callback;Dispatcher();
print('CALLBACK-FROM Static void: $tag');
break;
case "updated_simple_button":
break;
case "focus_button":
print("Focus button has been called");
break;
default:
print("OnClick event of $tag");
}
}
void openInstalledMapToPickUp(){
double destiLat = widget.rideDetails.pickUpLat;
double destLng = widget.rideDetails.pickUpLng;
MapsSheet.show(
context: context,
onMapTap: (map) {
map.showDirections(
destination: mapLaunch.Coords(
destiLat,
destLng,
),
destinationTitle: 'Drop off Location',
origin: mapLaunch.Coords(
currentPosition.latitude,
currentPosition.longitude,
),
originTitle: "My Current Location",
// waypoints: waypoints,
directionsMode: directionsMode,
);
},
);
// print('pickup $startLat');
// print('pickup $startLng');
print('dropoff lat $destiLat');
print('dropoff lat $destLng ');
}
#override
Widget build(BuildContext context)
{
return new Scaffold(
body: Stack(
children: [
Positioned(
child: SlidingUpPanel(
minHeight: 110,
maxHeight: 400 ,
borderRadius: BorderRadius.only(topLeft: Radius.circular(16.0), topRight: Radius.circular(16.0)),
panel: Container(
height: 300.0,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 15.0, vertical: 18.0),
child: Column(
children: [
Container(
padding: EdgeInsets.only(top: 10, bottom: 10),
decoration: BoxDecoration(color: Colors.cyan[600],
borderRadius: BorderRadius.all(Radius.circular(40.0),),),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"${durationRide} to Destination",
style: TextStyle(fontSize: 15.0, fontFamily: "Brand Bold", color: Colors.white),
),
],
),
),
SizedBox(height: 8.0,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(widget.rideDetails.rider_name, style: TextStyle(fontFamily: "Brand Bold", fontSize: 20.0, fontWeight: FontWeight.w600),),
Text("R ${itemCount.toString()}"),
Column(
children: [
Text("Total Rides", style: TextStyle(fontSize: 12), ),
Text("${widget.rideDetails.totalTrip}", style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600), ),
],
),
],
),
SizedBox(height: 8.0,),
Divider(height: 2.0, thickness: 2.0,),
SizedBox(height: 8.0,),
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: EdgeInsets.all(0.0),
child: MaterialButton(
onPressed: () {
launch(('tel://${widget.rideDetails.phone}'));
},
color: Colors.black,
textColor: Colors.white,
child: Icon(
Icons.call, color: Colors.white, size: 20.0,
),
padding: EdgeInsets.all(12),
shape: CircleBorder(),
),
),
],
),
),
SizedBox(height: 10.0,),
Divider(height: 2.0, thickness: 2.0,),
SizedBox(height: 10.0,),
Row(
children: [
Image.asset("images/pickicon.png", height: 16.0, width: 16.0,),
SizedBox(width: 18.0,),
Expanded(
child: Container(
child: Text(
widget.rideDetails.pickup_address,
style: TextStyle(fontSize: 14.0),
overflow: TextOverflow.visible,
),
),
),
],
),
SizedBox(height: 16.0,),
Row(
children: [
Image.asset("images/desticon.png", height: 16.0, width: 16.0,),
SizedBox(width: 10.0,),
Expanded(
child: Container(
child: Text(
widget.rideDetails.dropoff_address,
style: TextStyle(fontSize: 14.0),
overflow: TextOverflow.ellipsis,
),
),
),
],
),
SizedBox(height: 16.0,),
Visibility(
visible: statStat,
child:RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(24.0),
),
onPressed: () async
{
if(status == "accepted")
{
status = "arrived";
setState(() {
btnTitle = "Start Trip";
btnColor = Colors.cyan[600];
});
}
else if(status == "arrived")
{
status = "onride";
setState(() {
btnTitle = "End Trip";
btnColor = Colors.redAccent;
});
initTimer();
openInstalledMapToPickUp();
}
else if(status == "onride")
{
endTheTrip();
}
},
color: btnColor,
child: Padding(
padding: EdgeInsets.all(17.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(btnTitle, style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold, color: Colors.white),),
Icon(Icons.directions_car, color: Colors.white, size: 26.0,),
],
),
),
),
),
],
),
),
),
),
),
],
),
);
}
endTheTrip() async
void setCustomHeader() {
floatyHead.updateFloatyHeadContent(
header: header,
body: body,
footer: footer,
);
}
void openHead(){
if(openHeads == true){
setCustomHeader();
setCloseIcon();
setIcon();
floatyHead.openBubble();
setNotificationTitle();
setNotificationIcon();
//floatyHead.openBubble();
}
//else floatyHead.isOpen;
}
void closeFloatyHead() {
if (floatyHead.isOpen) {
floatyHead.closeHead();
}
}
Future<void> setNotificationTitle() async {
String result;
try {
result = await floatyHead
.setNotificationTitle("OH MY GOD! THEY KILL KENNY!!!");
} on PlatformException {
result = 'Failed to get icon.';
}
print('result: $result');
if (!mounted) return;
}
Future<void> setNotificationIcon() async {
String result;
String assetPath = "images/notificationIcon.png";
try {
result = await floatyHead.setNotificationIcon(assetPath);
print(result);
} on PlatformException {
result = 'Failed to get icon.';
print("failed: $result");
}
if (!mounted) return;
}
Future<void> setIcon() async {
String result;
String assetPath = "images/headIcon.png";
try {
result = await floatyHead.setIcon(assetPath);
print('result: $result');
} on PlatformException {
return;
//result = 'Failed to get icon.';
}
if (!mounted) return;
}
Future<void> setCloseIcon() async {
String assetPath = "images/closeIcon.png";
try {
await floatyHead.setCloseIcon(assetPath);
} on PlatformException {
return;
}
if (!mounted) return;
}
Future<void> setCloseIconBackground() async {
String assetPath = "assets/closeBg.png";
try {
await floatyHead.setCloseBackgroundIcon(assetPath);
} on PlatformException {
return;
}
if (!mounted) return;
}
}
I want to be able to pass onpressed or onTap function from the alert window button to my app, the plugin name is FLoaty_head github https://github.com/Crdzbird/floaty_chathead
pub.dev https://pub.dev/packages/floaty_head
Never post pictures, post your code, it'll be easier for anybody who is answering to work with your code, instead of having to type it out manually, which ultimately makes your chances of getting an answer slimmer.
Anyway, for your code, copy everything in the on pressed you pointed to, and create a new function, call it myFunction for example, and put it outside your widget build.
void myFunction ()async {
await Sharedpreferences...etc}
And in your onPressed:
onPressed: ()=>myFunction(),
Related
I am currently creating a profile system, in which users can edit certain attributes. I can already GET any and all information that has previously been entered by the user, as well as updating each observation in the database. I am attempting to print the given values to the screen, so the user can see their previously saved input before choosing to change.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
final _formKey = GlobalKey<FormState>();
StorageService storage = StorageService();
userInfo info = userInfo(0, 0,"","", "", "", "");
Future<userInfo> fetchUserInfo() async {
String jwt = await storage.readSecureData("jwt") as String;
Map<String, dynamic> map = Jwt.parseJwt(jwt);
String url = 'http://localhost:8087/getinfo/' + map["id"];
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
print(response.body);
// info = userInfo.fromJson(jsonDecode(response.body));
return userInfo.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load userInfo');
}
}
This is my GET request. It works perfectly and returns an userInfo object.
#override
Widget build(BuildContext context) {
userInfo info = userInfo(0, 0,"","", "", "", "");
final ButtonStyle style =
ElevatedButton.styleFrom(
primary: Colors.green,
fixedSize: Size(200, 50),
textStyle: const TextStyle(fontSize: 20),
);
return Padding(
padding: EdgeInsets.all(0),
child: ListView(
children: <Widget>[
Container(
height: 410,
margin: const EdgeInsets.fromLTRB(20, 20, 20, 20),
padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(18))
),
child: Column(
children: <Widget> [
const Text("Patient Details" , style: TextStyle(fontSize: 40, color: Colors.white)),
Row(
children: [
const Text("Medicare:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
const Text("1234567891" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
]),
Row(
children: [
const Text("Home Address:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
]),
Row(
children: [
Text(info.address , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
]),
Row(
children: [
const Text("Mobile:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
const Text("0447171002" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
]),
Container(
margin: const EdgeInsets.fromLTRB(0, 25, 0, 0),
child: ElevatedButton.icon(
style: style,
icon: const Icon(Icons.edit),
label: const Text('Edit Personal'),
onPressed: () async {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const EditInfoApp()));
},
)
)]),
),
Container(
height: 470,
margin: const EdgeInsets.fromLTRB(20, 20, 20, 20),
padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.all(Radius.circular(18))
),
child: Column(
children: <Widget> [
const Text("Health Information" , style: TextStyle(fontSize: 40, color: Colors.white)),
Row(
children: [
const Text("Health Status: " , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
const Text("average" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
]),
Row(
children: [
const Text("Allergies:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
]),
Row(
children: [
const Text("nuts, Peanuts, fish" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
]),
Row(
children: [
const Text("Diagnosis:" , style: TextStyle(fontSize: 30, height: 2, color: Colors.white)),
]),
Row(
children: [
const Text("Asthma, constipation" , style: TextStyle(fontSize: 30, height: 2, color: Colors.black)),
]),
Container(
margin: const EdgeInsets.fromLTRB(0, 25, 0, 0),
child: ElevatedButton.icon(
style: style,
icon: const Icon(Icons.edit),
label: const Text('Edit Health'),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => const EditHealthApp()));
},
)
)]),
),
]));
}
}
At the moment, I have most fields hard coded. Although, in the home address field, i have tried to enter info.address. This obviously will not work, as I cannot initialise the userInfo object without being inside of an async function, but then I can't access the variables.
I am making a simple weather application, with the OpenWeather API, and it throws me the following error:
"The following _CastError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#9133d): type '_Type' is not a subtype of type 'Weather?' in type cast"
The Code is the following:
Future getForecast(Location location) async {
Forecast forecast;
String apiKey = "117bf0ee07134176cf271f0ad49f3807";
String lat = location.lat;
String lon = location.lon;
var url =
"https://api.openweathermap.org/data/2.5/onecall?lat=$lat&lon=$lon&appid=$apiKey&units=metric";
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
forecast = Forecast.fromJson(jsonDecode(response.body));
}
return _forecast;
}
class _forecast {
}
This is my complete code:
class CurrentWeatherPage extends StatefulWidget {
final List<Location> locations;
final BuildContext context;
const CurrentWeatherPage(this.locations, this.context);
#override
_CurrentWeatherPageState createState() =>
_CurrentWeatherPageState(this.locations, this.context);
}
class _CurrentWeatherPageState extends State<CurrentWeatherPage> {
final List<Location> locations;
Location location;
final BuildContext context;
_CurrentWeatherPageState(List<Location> locations, BuildContext context)
: this.locations = locations,
this.context = context,
this.location = locations[0];
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromRGBO(241, 254, 252, 1),
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Datos ",
style: TextStyle(fontSize: 22),
),
Text(
"Meteorológicos",
style: TextStyle(fontSize: 22, color: Color.fromRGBO(56, 215, 199, 1)),
)
],
),
backgroundColor: Color.fromRGBO(10, 45, 66, 0.9),
),
body: ListView(children: <Widget>[
currentWeatherViews(this.locations, this.location, this.context),
forcastViewsHourly(this.location),
forcastViewsDaily(this.location),
]),
);
}
void _changeLocation(Location newLocation) {
setState(() {
location = newLocation;
});
}
Widget currentWeatherViews(
List<Location> locations, Location location, BuildContext context) {
Weather? _weather;
return FutureBuilder(
builder: (context, snapshot) {
if (snapshot.hasData) {
_weather = snapshot.data as Weather?;
if (_weather == null) {
return Text("Error getting weather");
} else {
return Column(children: [
createAppBar(locations, location, context),
// CityDropDown(locations),
weatherBox(_weather!),
weatherDetailsBox(_weather!),
]);
}
} else {
return Center(child: CircularProgressIndicator());
}
},
future: getCurrentWeather(location),
);
}
Widget forcastViewsHourly(Location location) {
Forecast? _forcast;
return FutureBuilder(
builder: (context, snapshot) {
if (snapshot.hasData) {
_forcast = snapshot.data as Forecast?;
if (_forcast == null) {
return Text("Error getting weather");
} else {
return hourlyBoxes(_forcast!);
}
} else {
return Center(child: CircularProgressIndicator());
}
},
future: getForecast(location),
);
}
Widget forcastViewsDaily(Location location) {
Forecast? _forcast;
return FutureBuilder(
builder: (context, snapshot) {
if (snapshot.hasData) {
_forcast = snapshot.data as Forecast?;
if (_forcast == null) {
return Text("Error getting weather");
} else {
return dailyBoxes(_forcast!);
}
} else {
return Center(child: CircularProgressIndicator());
}
},
future: getForecast(location),
);
}
Widget createAppBar(
List<Location> locations, Location location, BuildContext context) {
// Location dropdownValue = locations.first;
return Container(
padding:
const EdgeInsets.only(left: 20, top: 15, bottom: 15, right: 20),
margin: const EdgeInsets.only(
top: 35, left: 15.0, bottom: 15.0, right: 15.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(60)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.1),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3),
)
]),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
DropdownButton<Location>(
value: location,
icon: Icon(
Icons.keyboard_arrow_down_rounded,
color: Colors.black,
size: 24.0,
semanticLabel: 'Tap to change location',
),
elevation: 16,
underline: Container(
height: 0,
color: Color.fromRGBO(10, 45, 66, 0.9),
),
onChanged: (newLocation) {
// callback(newValue);
// setState(() {
// location = newValue;
// });
var changeLocation = newLocation;
},
items:
locations.map<DropdownMenuItem<Location>>((Location value) {
return DropdownMenuItem<Location>(
value: value,
child: Text.rich(
TextSpan(
children: <TextSpan>[
TextSpan(
text: '${value.city.capitalizeFirstOfEach}, ',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16)),
TextSpan(
text: '${value.country.capitalizeFirstOfEach}',
style: TextStyle(
fontWeight: FontWeight.normal, fontSize: 16)),
],
),
),
);
}).toList(),
),
],
));
}
Widget weatherDetailsBox(Weather _weather) {
return Container(
padding: const EdgeInsets.only(left: 15, top: 25, bottom: 25, right: 15),
margin: const EdgeInsets.only(left: 15, top: 5, bottom: 15, right: 15),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(20)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.1),
spreadRadius: 5,
blurRadius: 7,
offset: Offset(0, 3),
)
]),
child: Row(
children: [
Expanded(
child: Column(
children: [
Container(
child: Text(
"Viento",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 12,
color: Colors.grey),
)),
Container(
child: Text(
"${_weather.wind} km/h",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 15,
color: Colors.black),
))
],
)),
Expanded(
child: Column(
children: [
Container(
child: Text(
"Humedad",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 12,
color: Colors.grey),
)),
Container(
child: Text(
"${_weather.humidity.toInt()}%",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 15,
color: Colors.black),
))
],
)),
Expanded(
child: Column(
children: [
Container(
child: Text(
"Presión atmosférica",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 12,
color: Colors.black45),
)),
Container(
child: Text(
"${_weather.pressure} hPa",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 15,
color: Colors.black),
))
],
))
],
),
);
}
Widget weatherBox(Weather _weather) {
return Stack(children: [
Container(
padding: const EdgeInsets.all(15.0),
margin: const EdgeInsets.all(15.0),
height: 160.0,
decoration: BoxDecoration(
color: Color.fromRGBO(55, 187, 174, 1),
borderRadius: BorderRadius.all(Radius.circular(20))),
),
ClipPath(
clipper: Clipper(),
child: Container(
padding: const EdgeInsets.all(15.0),
margin: const EdgeInsets.all(15.0),
height: 160.0,
decoration: BoxDecoration(
color: Color.fromRGBO(198, 9, 125, 1),
borderRadius: BorderRadius.all(Radius.circular(20))))),
Container(
padding: const EdgeInsets.all(15.0),
margin: const EdgeInsets.all(15.0),
height: 160.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
getWeatherIcon(_weather.icon),
Container(
margin: const EdgeInsets.all(5.0),
child: Text(
"${_weather.description.capitalizeFirstOfEach}",
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 16,
color: Colors.white),
)),
Container(
margin: const EdgeInsets.all(5.0),
child: Text(
"Máxima:${_weather.high.toInt()}° Mínima:${_weather.low.toInt()}°",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 13,
color: Colors.white),
)),
])),
Column(children: <Widget>[
Container(
child: Text(
"${_weather.temp.toInt()}°",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 60,
color: Colors.white),
)),
Container(
margin: const EdgeInsets.all(0),
child: Text(
"Sensación térmica ${_weather.feelsLike.toInt()}°",
textAlign: TextAlign.left,
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 13,
color: Colors.white),
)),
])
],
))
]);
}
Image getWeatherIcon(String _icon) {
String path = 'assets/icons/';
String imageExtension = ".png";
return Image.asset(
path + _icon + imageExtension,
width: 70,
height: 70,
);
}
Image getWeatherIconSmall(String _icon) {
String path = 'assets/icons/';
String imageExtension = ".png";
return Image.asset(
path + _icon + imageExtension,
width: 40,
height: 40,
);
}
Widget hourlyBoxes(Forecast _forecast) {
return Container(
margin: EdgeInsets.symmetric(vertical: 0.0),
height: 150.0,
child: ListView.builder(
padding:
const EdgeInsets.only(left: 8, top: 0, bottom: 0, right: 8),
scrollDirection: Axis.horizontal,
itemCount: _forecast.hourly.length,
itemBuilder: (BuildContext context, int index) {
return Container(
padding: const EdgeInsets.only(
left: 10, top: 15, bottom: 15, right: 10),
margin: const EdgeInsets.all(5),
decoration: BoxDecoration(
color: Color.fromRGBO(1, 68, 109, 1),
borderRadius: BorderRadius.all(Radius.circular(18)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.1),
spreadRadius: 2,
blurRadius: 2,
offset: Offset(0, 1), // changes position of shadow
)
]),
child: Column(children: [
Text(
"${_forecast.hourly[index].temp}°",
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 17,
color: Color.fromRGBO(56, 215, 199, 1)),
),
getWeatherIcon(_forecast.hourly[index].icon),
Text(
"${getTimeFromTimestamp(_forecast.hourly[index].dt)}",
style: TextStyle(
fontWeight: FontWeight.w600,
fontSize: 12,
color: Colors.white),
),
]));
}));
}
String getTimeFromTimestamp(int timestamp) {
var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
var formatter = new DateFormat('h:mm a');
return formatter.format(date);
}
String getDateFromTimestamp(int timestamp) {
var date = new DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
var formatter = new DateFormat('E');
return formatter.format(date);
}
Widget dailyBoxes(Forecast _forcast) {
return Expanded(
child: ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
padding:
const EdgeInsets.only(left: 8, top: 0, bottom: 0, right: 8),
itemCount: _forcast.daily.length,
itemBuilder: (BuildContext context, int index) {
return Container(
decoration: BoxDecoration(
color: Color.fromRGBO(1, 68, 109, 1),
borderRadius: BorderRadius.circular(10),
),
padding: const EdgeInsets.only(
left: 10, top: 5, bottom: 5, right: 10),
margin: const EdgeInsets.all(5),
child: Row(children: [
Expanded(
child: Text(
"${getDateFromTimestamp(_forcast.daily[index].dt)}",
style: TextStyle(fontSize: 14, color: Color.fromRGBO(56, 215, 199, 1)),
)),
Expanded(
child: getWeatherIconSmall(_forcast.daily[index].icon)),
Expanded(
child: Text(
"${_forcast.daily[index].high.toInt()}/${_forcast.daily[index].low.toInt()}",
textAlign: TextAlign.right,
style: TextStyle(fontSize: 14, color: Colors.white),
)),
]));
}));
}
}
class Clipper extends CustomClipper<Path> {
#override
Path getClip(Size size) {
Path path = Path();
path.moveTo(0, size.height - 20);
path.quadraticBezierTo((size.width / 6) * 1, (size.height / 2) + 15,
(size.width / 3) * 1, size.height - 30);
path.quadraticBezierTo((size.width / 2) * 1, (size.height + 0),
(size.width / 3) * 2, (size.height / 4) * 3);
path.quadraticBezierTo((size.width / 6) * 5, (size.height / 2) - 20,
size.width, size.height - 60);
path.lineTo(size.width, size.height - 60);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
path.close();
return path;
}
#override
bool shouldReclip(Clipper oldClipper) => false;
}
Future getCurrentWeather(Location location) async {
Weather weather;
String city = location.city;
String apiKey = "117bf0ee07134176cf271f0ad49f3807";
var url =
"https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey&units=metric";
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
weather = Weather.fromJson(jsonDecode(response.body));
}
return _weather;
}
class _weather {
}
Future getForecast(Location location) async {
Forecast forecast;
String apiKey = "117bf0ee07134176cf271f0ad49f3807";
String lat = location.lat;
String lon = location.lon;
var url =
"https://api.openweathermap.org/data/2.5/onecall?lat=$lat&lon=$lon&appid=$apiKey&units=metric";
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
forecast = Forecast.fromJson(jsonDecode(response.body));
}
return _forecast;
}
class _forecast {
}
I'm new to this, and I've tried many ways, but I can't get it to work. I would be grateful if someone could tell me the error and how to solve it. Thank you!
In your getCurrentWeather method, you assign the return value of the API call to weather, a local variable, but then continue to return _weather, a class not a variable. I don't know why it compiles even, probably because you did not use proper types.
I'll clean up one of the methods, please do the same with all of them where you may have done this. This is how your method should look like:
Future<Weather?> getCurrentWeather(Location location) async {
String city = location.city;
String apiKey = "117bf0ee07134176cf271f0ad49f3807";
var url =
"https://api.openweathermap.org/data/2.5/weather?q=$city&appid=$apiKey&units=metric";
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
return Weather.fromJson(jsonDecode(response.body));
}
return null;
}
Just a few random other remarks: the future should be a state variable, not called recreated every time build is called. This will happen if you put the call int the FutureBuilder directly. You pass a BuildContext through your class hierarchy. You should not need that. Just remove it altogether.
Hi I've been recently shifted from Android Native to Flutter.
What I'm trying to do is to read text from other apps by drawing over other apps.
What I found is flutter_mobile_vision for OCR which works with mobile camera.
https://pub.dev/packages/flutter_mobile_vision
Can anyone guide me to make my app draw on other apps and detect text from other apps.
Though in IOS Floating Panel is not possible due to restriction , but in android you can do so with flutter . Use:
dependencies:
system_alert_window: ^1.0.0
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:system_alert_window/system_alert_window.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
///
/// Whenever a button is clicked, this method will be invoked with a tag (As tag is unique for every button, it helps in identifying the button).
/// You can check for the tag value and perform the relevant action for the button click
///
void callBack(String tag) {
WidgetsFlutterBinding.ensureInitialized();
print(tag);
switch (tag) {
case "simple_button":
case "updated_simple_button":
SystemAlertWindow.closeSystemWindow(prefMode: SystemWindowPrefMode.OVERLAY);
break;
case "focus_button":
print("Focus button has been called");
break;
default:
print("OnClick event of $tag");
}
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
bool _isShowingWindow = false;
bool _isUpdatedWindow = false;
SystemWindowPrefMode prefMode = SystemWindowPrefMode.OVERLAY;
#override
void initState() {
super.initState();
_initPlatformState();
_requestPermissions();
SystemAlertWindow.registerOnClickListener(callBack);
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> _initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = await SystemAlertWindow.platformVersion;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
Future<void> _requestPermissions() async {
await SystemAlertWindow.requestPermissions(prefMode: prefMode);
}
void _showOverlayWindow() {
if (!_isShowingWindow) {
SystemWindowHeader header = SystemWindowHeader(
title: SystemWindowText(text: "Incoming Call", fontSize: 10, textColor: Colors.black45),
padding: SystemWindowPadding.setSymmetricPadding(12, 12),
subTitle: SystemWindowText(text: "9898989899", fontSize: 14, fontWeight: FontWeight.BOLD, textColor: Colors.black87),
decoration: SystemWindowDecoration(startColor: Colors.grey[100]),
button: SystemWindowButton(text: SystemWindowText(text: "Personal", fontSize: 10, textColor: Colors.black45), tag: "personal_btn"),
buttonPosition: ButtonPosition.TRAILING);
SystemWindowBody body = SystemWindowBody(
rows: [
EachRow(
columns: [
EachColumn(
text: SystemWindowText(text: "Some body", fontSize: 12, textColor: Colors.black45),
),
],
gravity: ContentGravity.CENTER,
),
EachRow(columns: [
EachColumn(
text: SystemWindowText(text: "Long data of the body", fontSize: 12, textColor: Colors.black87, fontWeight: FontWeight.BOLD),
padding: SystemWindowPadding.setSymmetricPadding(6, 8),
decoration: SystemWindowDecoration(startColor: Colors.black12, borderRadius: 25.0),
margin: SystemWindowMargin(top: 4)),
], gravity: ContentGravity.CENTER),
EachRow(
columns: [
EachColumn(
text: SystemWindowText(text: "Notes", fontSize: 10, textColor: Colors.black45),
),
],
gravity: ContentGravity.LEFT,
margin: SystemWindowMargin(top: 8),
),
EachRow(
columns: [
EachColumn(
text: SystemWindowText(text: "Some random notes.", fontSize: 13, textColor: Colors.black54, fontWeight: FontWeight.BOLD),
),
],
gravity: ContentGravity.LEFT,
),
],
padding: SystemWindowPadding(left: 16, right: 16, bottom: 12, top: 12),
);
SystemWindowFooter footer = SystemWindowFooter(
buttons: [
SystemWindowButton(
text: SystemWindowText(text: "Simple button", fontSize: 12, textColor: Color.fromRGBO(250, 139, 97, 1)),
tag: "simple_button",
padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
width: 0,
height: SystemWindowButton.WRAP_CONTENT,
decoration: SystemWindowDecoration(startColor: Colors.white, endColor: Colors.white, borderWidth: 0, borderRadius: 0.0),
),
SystemWindowButton(
text: SystemWindowText(text: "Focus button", fontSize: 12, textColor: Colors.white),
tag: "focus_button",
width: 0,
padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
height: SystemWindowButton.WRAP_CONTENT,
decoration: SystemWindowDecoration(
startColor: Color.fromRGBO(250, 139, 97, 1), endColor: Color.fromRGBO(247, 28, 88, 1), borderWidth: 0, borderRadius: 30.0),
)
],
padding: SystemWindowPadding(left: 16, right: 16, bottom: 12),
decoration: SystemWindowDecoration(startColor: Colors.white),
buttonsPosition: ButtonPosition.CENTER);
SystemAlertWindow.showSystemWindow(
height: 230,
header: header,
body: body,
footer: footer,
margin: SystemWindowMargin(left: 8, right: 8, top: 200, bottom: 0),
gravity: SystemWindowGravity.TOP,
notificationTitle: "Incoming Call",
notificationBody: "+1 646 980 4741",
prefMode: prefMode);
setState(() {
_isShowingWindow = true;
});
} else if (!_isUpdatedWindow) {
SystemWindowHeader header = SystemWindowHeader(
title: SystemWindowText(text: "Outgoing Call", fontSize: 10, textColor: Colors.black45),
padding: SystemWindowPadding.setSymmetricPadding(12, 12),
subTitle: SystemWindowText(text: "8989898989", fontSize: 14, fontWeight: FontWeight.BOLD, textColor: Colors.black87),
decoration: SystemWindowDecoration(startColor: Colors.grey[100]),
button: SystemWindowButton(text: SystemWindowText(text: "Personal", fontSize: 10, textColor: Colors.black45), tag: "personal_btn"),
buttonPosition: ButtonPosition.TRAILING);
SystemWindowBody body = SystemWindowBody(
rows: [
EachRow(
columns: [
EachColumn(
text: SystemWindowText(text: "Updated body", fontSize: 12, textColor: Colors.black45),
),
],
gravity: ContentGravity.CENTER,
),
EachRow(columns: [
EachColumn(
text: SystemWindowText(text: "Updated long data of the body", fontSize: 12, textColor: Colors.black87, fontWeight: FontWeight.BOLD),
padding: SystemWindowPadding.setSymmetricPadding(6, 8),
decoration: SystemWindowDecoration(startColor: Colors.black12, borderRadius: 25.0),
margin: SystemWindowMargin(top: 4)),
], gravity: ContentGravity.CENTER),
EachRow(
columns: [
EachColumn(
text: SystemWindowText(text: "Notes", fontSize: 10, textColor: Colors.black45),
),
],
gravity: ContentGravity.LEFT,
margin: SystemWindowMargin(top: 8),
),
EachRow(
columns: [
EachColumn(
text: SystemWindowText(text: "Updated random notes.", fontSize: 13, textColor: Colors.black54, fontWeight: FontWeight.BOLD),
),
],
gravity: ContentGravity.LEFT,
),
],
padding: SystemWindowPadding(left: 16, right: 16, bottom: 12, top: 12),
);
SystemWindowFooter footer = SystemWindowFooter(
buttons: [
SystemWindowButton(
text: SystemWindowText(text: "Updated Simple button", fontSize: 12, textColor: Color.fromRGBO(250, 139, 97, 1)),
tag: "updated_simple_button",
padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
width: 0,
height: SystemWindowButton.WRAP_CONTENT,
decoration: SystemWindowDecoration(startColor: Colors.white, endColor: Colors.white, borderWidth: 0, borderRadius: 0.0),
),
SystemWindowButton(
text: SystemWindowText(text: "Focus button", fontSize: 12, textColor: Colors.white),
tag: "focus_button",
width: 0,
padding: SystemWindowPadding(left: 10, right: 10, bottom: 10, top: 10),
height: SystemWindowButton.WRAP_CONTENT,
decoration: SystemWindowDecoration(
startColor: Color.fromRGBO(250, 139, 97, 1), endColor: Color.fromRGBO(247, 28, 88, 1), borderWidth: 0, borderRadius: 30.0),
)
],
padding: SystemWindowPadding(left: 16, right: 16, bottom: 12),
decoration: SystemWindowDecoration(startColor: Colors.white),
buttonsPosition: ButtonPosition.CENTER);
SystemAlertWindow.updateSystemWindow(
height: 230,
header: header,
body: body,
footer: footer,
margin: SystemWindowMargin(left: 8, right: 8, top: 200, bottom: 0),
gravity: SystemWindowGravity.TOP,
notificationTitle: "Outgoing Call",
notificationBody: "+1 646 980 4741",
prefMode: prefMode);
setState(() {
_isUpdatedWindow = true;
});
} else {
setState(() {
_isShowingWindow = false;
_isUpdatedWindow = false;
});
SystemAlertWindow.closeSystemWindow(prefMode: prefMode);
}
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('System Alert Window Example App'),
),
body: Center(
child: Column(
children: <Widget>[
Text('Running on: $_platformVersion\n'),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: MaterialButton(
onPressed: _showOverlayWindow,
textColor: Colors.white,
child: !_isShowingWindow
? Text("Show system alert window")
: !_isUpdatedWindow
? Text("Update system alert window")
: Text("Close system alert window"),
color: Colors.deepOrange,
padding: const EdgeInsets.symmetric(vertical: 8.0),
),
)
],
),
),
),
);
}
}
With this You can Have the Floating Panel Option . You can Establish your Logic According to your functionality .
thanks
when I am increasing my quantity of one product then all product's quantity is increasing, so how to solve it.
This is my product page:
import 'dart:convert';
import 'package:carousel_pro/carousel_pro.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hospital/Authentication/LoginLogoutScreenPage/login.dart';
import 'package:hospital/CartPage/Cart_Api/cart_api.dart';
import 'package:hospital/CartPage/pages/cartPage.dart';
import 'package:hospital/Drawer/dropdown_menu.dart';
import 'package:hospital/ProductDetailsPage/product_detailPage.dart';
import 'package:hospital/ProductDetailsPage/related_product_page.dart';
import 'package:hospital/SecondSection/Medicine/medicine_page.dart';
import 'package:hospital/constant.dart';
import 'package:hospital/customApiVariable.dart';
import 'package:http/http.dart' as http;
import 'package:line_icons/line_icons.dart';
import 'package:provider/provider.dart';
class DetailPage extends StatefulWidget {
final plistId;
const DetailPage({Key key, this.plistId}) : super(key: key);
#override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
final GlobalKey<FormState> _formKey = GlobalKey();
int quantity = 1;
var response;
var detailPageApi;
#override
void initState() {
super.initState();
fetchData(widget.plistId);
}
fetchData(var consultWithDoctor) async {
var api = Uri.parse(
'$ecommerceBaseUrl/productListApi.php?a2rTokenKey=$a2rTokenKey&plistId=${widget.plistId}');
response = await http.get(
api,
);
print("detailPageApi " + api.toString());
print("detailPageBody " + response.body);
detailPageApi = jsonDecode(response.body);
print("detailPagelist " + detailPageApi.toString());
setState(() {});
}
Future _submit() async {
var errorMessage = 'Authentication Failed';
if (successfully_add_cart_status.toString() == 'false') {
errorMessage = 'Please try again later';
print(errorMessage);
_showerrorDialog(errorMessage);
} else {
errorMessage = 'Product Succesfully Added to Cart';
print(errorMessage);
_showerrorDialog(errorMessage);
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: kGreen,
title: Text(
"Details",
style: TextStyle(fontStyle: FontStyle.italic),
),
actions: [
IconButton(
icon: Icon(Icons.shopping_cart),
// onPressed: () => print("open cart"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Cartpage()),
);
},
),
DropDownMenu(),
],
),
body: Container(
child: response != null
? ListView.builder(
itemCount: detailPageApi.length.clamp(0, 1),
scrollDirection: Axis.vertical,
physics: ScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, index) {
var details = detailPageApi[index];
if (details['num'] == 0) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
),
);
}
return Column(
children: <Widget>[
Hero(
tag: "1",
child: SizedBox(
height: 300.0,
width: 300.0,
child: Carousel(
boxFit: BoxFit.cover,
autoplay: false,
animationCurve: Curves.fastOutSlowIn,
animationDuration: Duration(milliseconds: 800),
dotSize: 6.0,
dotIncreasedColor: Colors.black,
dotBgColor: Colors.transparent,
// dotPosition: DotPosition.topRight,
dotVerticalPadding: 10.0,
showIndicator: true,
indicatorBgPadding: 7.0,
images: [
NetworkImage(details['pImgImg']),
],
),
),
),
SizedBox(
height: 50,
),
Padding(
padding: const EdgeInsets.only(left: 20, right: 20),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Name :",
style: TextStyle(
fontSize: 18,
height: 1.5,
fontWeight: FontWeight.w500),
),
SizedBox(
width: 20,
),
Flexible(
child: Text(
// widget.details,
details['productName'],
style: TextStyle(
fontSize: 17,
height: 1.5,
fontWeight: FontWeight.w500),
),
),
],
),
),
SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.only(left: 20, right: 20),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Details :",
style: TextStyle(
fontSize: 18,
height: 1.5,
fontWeight: FontWeight.w500),
),
SizedBox(
width: 20,
),
Flexible(
child: Text(
// widget.details,
details['productDescription'],
style: TextStyle(
fontSize: 17,
height: 1.5,
fontWeight: FontWeight.w500),
),
),
],
),
),
SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.only(left: 20, right: 20),
child: Row(
children: <Widget>[
Text(
"Price :",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.w500),
),
SizedBox(
width: 20,
),
Row(
children: <Widget>[
Text(
// "Rs " + widget.pPromotionPrice,
"Rs 55.0",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500),
),
SizedBox(
width: 20,
),
Text(
// "Rs " + widget.pPrice,
"Rs 100",
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w500,
// color: warning,
decoration: TextDecoration.lineThrough),
)
],
)
],
),
),
SizedBox(
height: 25,
),
Padding(
padding: const EdgeInsets.only(right: 20, left: 20),
child: Row(
children: <Widget>[
Text(
"Qty :",
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.w500),
),
SizedBox(
width: 20,
),
Row(
children: <Widget>[
InkWell(
onTap: () {
if (quantity > 1) {
setState(() {
quantity = --quantity;
});
}
// minus here
},
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
// border: Border.all(color: primary),
shape: BoxShape.circle),
child: Icon(
LineIcons.minus,
size: 15,
),
),
),
SizedBox(
width: 15,
),
Text(
quantity.toString(),
style: TextStyle(fontSize: 16),
),
SizedBox(
width: 15,
),
InkWell(
onTap: () {
setState(() {
quantity = ++quantity;
});
// minus here
},
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
// border: Border.all(color: primary),
shape: BoxShape.circle),
child: Icon(
LineIcons.plus,
size: 15,
),
),
),
],
)
],
),
),
SizedBox(
height: 50,
),
InkWell(
onTap: () async {
if (var_uid.toString() != null.toString()) {
_submit();
var qty = quantity.toString();
var plistId = widget.plistId;
print('pplistid' + plistId);
var uid = var_uid.toString();
var sid = var_sid.toString();
print('uuid' + uid);
print('ssid' + sid);
var response =
await add_to_cart_fn(qty, plistId, uid, sid);
print("rsp: " + response['msg']);
} else {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LoginPage()));
}
},
// },
child: Padding(
padding: EdgeInsets.only(left: 20, right: 20),
child: Container(
height: 45,
width: double.infinity,
decoration: BoxDecoration(
color: kGreen,
borderRadius: BorderRadius.circular(30)),
child: Center(
child: Text(
"ADD TO CART",
style: TextStyle(
color: kWhite,
fontSize: 20,
),
),
),
),
),
),
SizedBox(height: 20.0),
RelatedProductPage(plistId: widget.plistId)
],
);
})
: Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
),
),
),
);
}
void _showerrorDialog(String message) {
Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: Colors.grey[350],
textColor: Colors.black,
fontSize: 16.0);
}
}
You are using the same quantity variable against each item. Changing the value on one of those will cause all items to update. What you need to do is to keep a Map of quantity selected against product id. That way whenever you increase or decrease the quantity, you update the quantity against that specific product id and the rest will remain unchanged.
EDIT: Here is how this will work
//Create a new map to save the product id and product selected quantity
var productQuantity = new Map<int, int>()
//When setting the values of the product, set the quantity from the map
Padding(
padding: const EdgeInsets.only(right: 20, left: 20),
child: Row(
children: <Widget>[
Text(
"Qty :",
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.w500),
),
SizedBox(width: 20,),
Row(
children: <Widget>[
InkWell(
onTap: () {
if (quantity > 1) {
setState(() {
//Changed here
productQuantity['$productId'] -= 1;
});
}
},
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(shape: BoxShape.circle),
child: Icon(
LineIcons.minus,
size: 15,
),
),
),
SizedBox(width: 15,),
Text(
//Changed here
productQuantity['$productId'].toString(),
style: TextStyle(fontSize: 16),
),
SizedBox(width: 15,),
InkWell(
onTap: () {
setState(() {
//Changed here
productQuantity['$productId'] += 1;
});
},
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
shape: BoxShape.circle),
),
child: Icon(
LineIcons.plus,
size: 15,
),
),
),
],
)
],
),
)
you set the common quantity in all products so change it and set it inside the class.
Please Check the example
import 'package:flutter/material.dart';
class QuantityUpdatePage extends StatefulWidget {
#override
_QuantityUpdatePageState createState() => _QuantityUpdatePageState();
}
class _QuantityUpdatePageState extends State<QuantityUpdatePage> {
List<Product> productArray = [];
#override
void initState() {
super.initState();
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
productArray.clear();
productArray.add(Product(1, "Product 1", 1));
productArray.add(Product(2, "Product 2", 1));
productArray.add(Product(3, "Product 3", 1));
productArray.add(Product(4, "Product 4", 1));
productArray.add(Product(5, "Product 5", 1));
setState(() {});
});
}
#override
void dispose() {
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("QUANTITY UPDATE")),
body: ListView.builder(
shrinkWrap: true,
itemCount: productArray.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(right: 20, left: 20),
child: ListView(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
children: [
SizedBox(
height: 20,
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(productArray[index].productName),
SizedBox(
width: 40,
),
Row(
children: <Widget>[
Text(
"Qty :",
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
),
SizedBox(
width: 20,
),
Row(
children: <Widget>[
InkWell(
onTap: () {
if (productArray[index].quantity > 1) {
setState(() {
productArray[index].quantity = --productArray[index].quantity;
});
}
// minus here
},
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
// border: Border.all(color: primary),
shape: BoxShape.circle),
child: Icon(
Icons.minimize,
size: 15,
),
),
),
SizedBox(
width: 15,
),
Text(
productArray[index].quantity.toString(),
style: TextStyle(fontSize: 16),
),
SizedBox(
width: 15,
),
InkWell(
onTap: () {
setState(() {
productArray[index].quantity = ++productArray[index].quantity;
});
// minus here
},
child: Container(
width: 25,
height: 25,
decoration: BoxDecoration(
// border: Border.all(color: primary),
shape: BoxShape.circle),
child: Icon(
Icons.add,
size: 15,
),
),
),
],
)
],
),
],
),
SizedBox(
height: 20,
),
],
),
);
}));
}
}
class Product {
String productName;
int quantity;
int id;
Product(this.id, this.productName, this.quantity);
}
#Deepak if you don't understand Maps use list instead, with a value being updated for each index. Initialize a list of int types with some large values like 1000000, and update the value for each index. use ListView builder to fetch the data from the API for each index.
List<int> quantity = List.empty(growable: true);
OR
List<int> quantity = List.filled(10000, []);
For updating quantity:-
quantity[index] += 1;
For Grid view thing, refer this example from CodeGrepper:-
GridView.count(
// Create a grid with 2 columns. If you change the scrollDirection to
// horizontal, this produces 2 rows.
crossAxisCount: 2,
// Generate 100 widgets that display their index in the List.
children: List.generate(100, (index) {
return Center(
child: Text(
'Item $index',
style: Theme.of(context).textTheme.headline5,
),
);
}),
);
Here I have a form like this. i want to change the value of the blue button week below when i swipe left or right of Calendar Week. What should i do guys ?. It can only changed when I clicked on the number
Here is the code I'm using:
import 'package:flutter/material.dart';
import 'package:myhumgupdate/App/Config/palette.dart';
import 'package:myhumgupdate/Widgets/dialog_loading.dart';
import 'package:myhumgupdate/giangvien/Screens/XemTKB/TKBTheoTuan/tkbtuan_viewmodel.dart';
import 'package:myhumgupdate/Widgets/calender_week.dart';
import 'package:myhumgupdate/giangvien/models/meeting.dart';
import 'package:myhumgupdate/giangvien/models/meetingdata_source.dart';
import 'package:stacked/stacked.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';
class TKBTuan extends StatefulWidget {
#override
_TKBTuanState createState() => _TKBTuanState();
}
final String _customTimeLabelText = 'Tiết';
class _TKBTuanState extends State<TKBTuan> {
#override
Widget build(BuildContext context) {
return ViewModelBuilder<TKBTuanViewModel>.reactive(
onModelReady: (model) => Future.delayed(Duration.zero,
() => DialogLoading.show(context, model.getTkbTuan(model.timeNow))),
builder: (context, TKBTuanViewModel model, child) => Column(
children: [
Row(
children: [
Expanded(
child: Container(
margin: EdgeInsets.only(
top: 18,
),
child: CalendarWeek(
calendarController: model.calendarController,
press: (DateTime date, _, __) {
model.getTkbTuan(date);
},
),
),
),
Container(
width: 40,
height: 56,
margin: EdgeInsets.only(right: 3),
padding: EdgeInsets.symmetric(horizontal: 4, vertical: 5),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Palette.kPrimaryColor,
),
child: Center(
child: Text(
"Week ${model.week}",
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
),
],
),
Expanded(
child: SfCalendar(
view: CalendarView.week,
firstDayOfWeek: 1,
maxDate: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, 00, 45, 0),
minDate: DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, 00, 45, 0),
headerHeight: 0,
viewHeaderHeight: 0,
dataSource: MeetingDataSource(model.getDataSource),
appointmentTimeTextFormat: 'hh:mm:ss a',
appointmentBuilder: appointmentBuilder,
initialDisplayDate: DateTime(DateTime.now().year,
DateTime.now().month, DateTime.now().day, 00, 45, 0),
monthViewSettings: MonthViewSettings(showAgenda: true),
timeSlotViewSettings: TimeSlotViewSettings(
startHour: 0,
endHour: 16,
timeFormat: _customTimeLabelText + " H",
timeIntervalHeight: 70,
timeTextStyle: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
)),
// selectionDecoration: BoxDecoration(
// color: Colors.transparent,
// border: Border.all(color: Colors.red, width: 1),
// borderRadius: const BorderRadius.all(Radius.circular(4)),
// shape: BoxShape.rectangle,
// ),
),
),
],
),
viewModelBuilder: () => TKBTuanViewModel());
}
}
Widget appointmentBuilder(BuildContext context,
CalendarAppointmentDetails calendarAppointmentDetails) {
final Meeting appointment = calendarAppointmentDetails.appointments.first;
return Container(
width: calendarAppointmentDetails.bounds.width,
height: calendarAppointmentDetails.bounds.height,
// color: appointment.background,
decoration: BoxDecoration(
color: appointment.background,
border: Border.all(
color: Colors.grey,
width: 0.5,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(2.0, 0, 0, 5.0),
child: Text(
appointment.eventName,
// textAlign: TextAlign.center,
style: TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(2.0, 0, 0, 0),
child: Text(
"Phòng: ${appointment.subText}",
style: TextStyle(fontSize: 10, fontStyle: FontStyle.italic),
),
)
],
),
);
}
And if there is no way to change the value like that, what should I do and how can I change that?
In the Flutter Event Calendar, you can programmatically select the date using selectedDate property of the CalendarController.
Inside the state, initialize the calendar controller.
final CalendarController _calendarController= CalendarController();
Using onViewChanged callback of the Flutter event calendar, you can set the first date of visible dates as selected date.
child: SfCalendar(
view: CalendarView.month,
controller: _calendarController,
onViewChanged: viewChanged,
),
void viewChanged(ViewChangedDetails viewChangedDetails) {
SchedulerBinding.instance!.addPostFrameCallback((Duration duration) {
_calendarController.selectedDate = viewChangedDetails.visibleDates[0];
});
}
Wrap your Widget in GestureDetector and use onPanUpdate like this:
GestureDetector(onPanUpdate: (details) {
if (details.delta.dx > 0) {
// swiping in right direction
// update week number
}
});