I need to update the percentage var inside the AlertDialog, I know that you can't update anything inside the showDialog() which I am using here because it is built in another Widget tree, but worth asking, it would really be very helpful.
Thanks in advance. :)
Update
The uploadTask listener redo the function until sth like Navigator pushes to a different screen.
My Code
Future storageupload() async {
await checkInternet();
try {
if (controller == null) {
dialog('Error', 'Please Provide A Video Name', () => {});
} else {
StorageReference ref = FirebaseStorage.instance
.ref()
.child("Khatma 1")
.child("Videos")
.child(controller.text != null ? controller.text : "");
StorageUploadTask uploadTask = ref.putFile(
File(Variables.lastVideoPath),
StorageMetadata(contentType: 'video/mp4'));
uploadTask.events.listen((event) {
if (uploadTask.isComplete) {
AwesomeDialog(
context: context,
headerAnimationLoop: false,
dialogType: DialogType.SUCCES,
animType: AnimType.BOTTOMSLIDE,
title: 'Uploaded',
desc: 'Your Video Was Successfully Uploaded !',
btnOkOnPress: () => {print('success')},
)..show();
} else {
setState(() {
percentage = event.snapshot.bytesTransferred /
event.snapshot.totalByteCount *
100;
});
}
});
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Uploading'),
content: Column(
children: [
Center(
child: Text('Uploading .... Please Be Patient'),
),
SizedBox(
height: height * 0.02,
),
Text(percentage.toString()),
],
),
);
},
);
}
} catch (e) {
print(e);
}
}
code
return StatefulBuilder(
builder: (BuildContext context, void function) => AlertDialog(
title: Text('Uploading'),
content: Column(
children: [
Center(
child: Text('Uploading .... Please Be Patient'),
),
SizedBox(
height: height * 0.02,
),
Text(percentage.toString()),
],
),
),
);
all dialogs are StateLess widgets.... so u can not call setstate to update their data to show on the UI. If u want to do that then u can wrap the dialog inside a StatefullBuilder . which will allow you to call setState.
Related
I have a widget in my settings screen something like this:
Widget autoplay()
{
return ChangeNotifierProvider<AutoplayToggle>(
create: (context) => AutoplayToggle(),
child: Consumer<AutoplayToggle>(
builder: (context, provider, child) {
return Container(
color: provider.isPause ? accent : primary,
width: 45,
child: Switch.adaptive(
value: isPause,
onChanged: (value) async {
setState(() {
isPause= value;
});
await UserPrefs.setAutoplay(isPause);
provider.toggleAutoplay();
},
),
);
},
),
),
}
and this is my class:
class AutoplayToggle with ChangeNotifier{
bool isPause = false;
void toggleAutoplay()
{
isPause = !isPause;
print(isPause);
notifyListeners();
}
}
I printed couple of statements to debug and every time I toggle the switch the function is being called as the values will change from false to true, however, it is not notifying the change. Any idea on whats going wrong?
can you try add "lazy" : false to ChangeNotifierProvider ?
Don't use setState in onChange method
return ChangeNotifierProvider<AutoplayToggle>(
create: (context) => AutoplayToggle(),
child: Consumer<AutoplayToggle>(
builder: (context, provider, child) {
return Container(
color: provider.isPause ? accent : primary,
width: 45,
child: Switch.adaptive(
value: provider.isPause,
onChanged: (value) async {
await UserPrefs.setAutoplay(isPause);
provider.toggleAutoplay();
},
),
);
},
),
);
Hi I'm newbie at coding and Flutter is my first language.
I want to delete Firestore data(Chat List) which is in the ListView from the StreamBuilder when user long tab the 'OK' button in AlertDialog.
(Process Summary:
longPress one item of the ListViewBuilder
show check AlertDialog
tab 'OK'
OK button's Function execute)
But when i longPress the item, then the deleteFunction(OK button's function) is automatically executed before i see the dialog.
How can I make it?
It is really difficult!
Here is my code
StreamBuilder(
stream: chatRoomDB,
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.active &&
snapshot.data != null) {
return ListView(
children: snapshot.data!.docs
.map<Widget>((DocumentSnapshot ds) {
Map<String, dynamic> data =
ds.data()! as Map<String, dynamic>;
receiverProfileImageURL = data['ProfileImageURL'];
receiverUID = data['UserID'];
receiverNickName = data['NickName'];
msgContent = data['MsgContent'];
chatID = data['ChatID'];
return Container(
margin: EdgeInsets.all(3),
child: ListTile(
shape: Border(
bottom:
BorderSide(color: Colors.grey, width: 1)),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ChattingRoom(
chatID: data['ChatID'],
receiverUID: data['UserID'],
receiverImageURL:
data['ProfileImageURL'],
receiverNickName: data['NickName'],
)));
},
onLongPress: () => checkDialog2(
context: context,
msg: 'Delete?',
okButtonString: 'OK',
okFunction: chatDBDeleteFunction(
data['UserID'], data['ChatID']),
noButtonString: 'No'),
leading: Container(
child: CircleAvatar(
foregroundImage: NetworkImage(
'$receiverProfileImageURL',
),
backgroundImage:
NetworkImage('$errorImageURL'),
)),
title: Text('$receiverNickName'),
),
);
}).toList());
}
return Container();
})
checkDialog2({
required context,
required String? msg,
required String? okButtonString,
required Future<dynamic> okFunction,
required String? noButtonString,
}) {
print(1);
okFunctions(okFunc) {
print(3);
okFunction;
Navigator.pop(context);
print(4);
}
print(5);
showDialog(
context: context,
builder: (context) {
print(2);
return AlertDialog(content: Text('$msg'), actions: <Widget>[
TextButton(
onPressed: () => okFunctions(okFunction),
child: Text(
'$okButtonString',
textAlign: TextAlign.end,
),
),
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text(
'$noButtonString',
textAlign: TextAlign.end,
),
),
]);
});
}
Future chatDBDeleteFunction(receiverUID, chatID) async {
await FirebaseFirestore.instance
.collection('ChatMessage')
.doc('${FirebaseAuth.instance.currentUser?.uid}')
.collection(chatID)
.get()
.then((QuerySnapshot qs) => {
qs.docs.forEach((DocumentSnapshot snapshot) {
snapshot.reference.delete();
})
});
Log Order is,
print 1->5>2 and automatically deleted + showDialog.
At that time(list item already disappeared), if i tab the 'OK' button, then print '3' and error(because there is nothing to delete)
(ps. although i change the okfunction to the void Function, it starts automatically. same)
Thanks for your help
I would prefer passing the UserId and ChatId and then call the chatDBDeleteFunction(receiverUID, chatID) Function and pass the values from values
TextButton(
onPressed: () {
Class.chatDBDeleteFunction(receiverUID, chatID)
}
)
The chatDBDeleteFunction(receiverUID, chatID) function can be static
Summarize the Problem:
My application sends information to a server and the server responds with success or failure. I am having trouble updating an AlertDialog with the result of network communication. I am sending multiple items to the server when the user saves their settings and I need to track if all the settings were successfully sent. So when all the settings were successfully sent, I can update the AlertDialog with success. The issue I am seeing with current implementation is it takes me two times to activate the TextButton before I see the correct message. AlertDialog should show the correct message after the first TextButton press labeled as "save". One of the cases I need to solve is if the server is down and the app's connection request times out. Then I need to use something like a CircularProgressIndicator so the user can wait while network communication is being done.
The variable successPrompt is what contains the message with the result of the network transaction. This needs to be updated to the correct message by the time the AlertDialog pops up.
2: What I've tried:
I've tried using FutureBuilder to create the AlertDialog but I got the same result. I need a way to bring up the AlertDialog when I know the result of the network transaction. What happens is the AlertDialog will be brought up but the application is still trying to connect to the server in the background. I want to bring up the widget once this step is done and the socket is closed.
3: Here's the relevant code. Please don't mind the debug prints and commented out code.
import 'package:flutter/material.dart';
import 'dart:io';
import 'globals.dart';
import 'dart:convert' show utf8;
import 'package:local_auth/local_auth.dart';
class SystemsSettingsPage extends StatefulWidget {
final int index;
SystemsSettingsPage({ required this.index});
#override
_SystemsSettingsPage createState() => _SystemsSettingsPage();
}
class _SystemsSettingsPage extends State<SystemsSettingsPage> {
bool tileValTemp = false;
bool tileValDetect = false;
bool tileValCamOff = false;
bool tileValSystem = false;
bool connected = false;
int successCount = 0;
String successPrompt = "";
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.blueAccent,
title: Text("Base Station Settings"),
),
body: Column(
children: <Widget> [
SwitchListTile(value: tileValDetect,
onChanged: (bool val){ setState(() {
tileValDetect = val;
});},
title: Text('Detection notifications', style: TextStyle(color: Colors.white))
),
SwitchListTile(value: tileValTemp,
onChanged: (bool val){ setState(() {
tileValTemp = val;
});},
title: Text('Temperature threshold out of range', style: TextStyle(color: Colors.white))
),
TextButton(
child: const Text("save", style: TextStyle(fontSize: 20.0)),
style: ButtonStyle(foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(10.0)),
backgroundColor: MaterialStateProperty.all<Color>(Colors.blueAccent)),
onPressed: () {
//successPrompt = "Loading.. Wait 5 seconds to update.";
successCount = 0;
Socket.connect(baseStationAddresses[0], baseStationPort,timeout: Duration(seconds: 5)).then(
(socket) {
print('Connected to: '
'${socket.remoteAddress.address}:${socket
.remotePort}');
String command = "SETSYSTEM," + baseStationNames[0] + ",detectMotion," + "$tileValDetect";
socket.write(command);
socket.listen((data) {
String socketData = utf8.decode(data);
if(socketData == "REQUEST_CONFIRMED") {
successCount += 1;
}
},
onDone: () {
socket.destroy();
},
);
},
).catchError((onError) {
print("here 1");
successPrompt = "There was a problem. Please retry.";
});
Socket.connect(baseStationAddresses[0], baseStationPort,timeout: Duration(seconds: 5)).then(
(socket) {
print('Connected to: '
'${socket.remoteAddress.address}:${socket
.remotePort}');
String command = "SETSYSTEM," + baseStationNames[0] + ",tempThreshold," + "$tileValTemp";
socket.write(command);
socket.listen((data) {
String socketData = utf8.decode(data);
if(socketData == "REQUEST_CONFIRMED") {
successCount += 1;
}
},
onDone: () {
print("SuccessCount $successCount");
if(successCount == 2)
{
print("here 2");
successPrompt = "Setting successfully saved.";
}
else
{
print("here 3");
successPrompt = "Couldn't save, please retry.";
}
socket.destroy();
},
);
}
).catchError((onError) {
print("here 4");
successPrompt = "There was a problem. Please retry.";
});
showDialog(context: context, builder: (context) =>
AlertDialog(
title: Text("Save results"),
content: Text(successPrompt),
actions: <Widget>[
TextButton(onPressed: () => Navigator.pop(context),
child: const Text("OK"),
)
]
)
);
/*
FutureBuilder<String>(
future: getSaveStatus(),
builder: (context, snapshot) {
String nonNullableString = snapshot.data ?? 'Error';
if(snapshot.hasData) {
return AlertDialog(
title: Text("Save results"),
content: Text(nonNullableString),
actions: <Widget>[
TextButton(onPressed: () => Navigator.pop(context),
child: const Text("OK"),
)
]
);
}
return Center(child: CircularProgressIndicator());
},
);*/
}
),
Center(
child:ClipRRect(
borderRadius: BorderRadius.circular(4),
child: Stack(
children: <Widget>[
Positioned.fill(
child: Container(
decoration: const BoxDecoration(
color: Colors.red,
),
),
),
TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.all(16.0),
primary: Colors.white,
textStyle: const TextStyle(fontSize: 20),
),
onPressed: () {},
child: const Text('Remove System'),
),
],
),
),
)
],
)
);
}
Future<String> getSaveStatus() async {
return await new Future(() => successPrompt);
}
}
Any suggestion would be helpful.
Wrap the content of the dialog inside of a StatefulBuilder until that your AlertDialog behave as stateless widget Refer:
await showDialog<void>(
context: context,
builder: (BuildContext context) {
int selectedRadio = 0;
return AlertDialog(
content: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Text(successPrompt);
},
),
);
},
);
I try to build cusom confirmation dialog. I tried many different options, but none of them worked for me as it should
Now I have stopped at this one:
Future<bool?> showConfirmationDialog(BuildContext context, String action, {String title = 'Confirmation'}) {
bool result = false;
var confirmDialog = AlertDialog(
title: Text(title),
content: SizedBox(
height: 50,
child: Text(
action,
),
),
actions: [
TextButton(
child: const Text("OK"),
onPressed: () {
Navigator.pop(context, true);
},
),
TextButton(
child: const Text("Cancel"),
onPressed: () {
Navigator.of(context).pop(false);
},
)
],
);
return showDialog<bool>(
context: context,
builder: (BuildContext context) {
return confirmDialog;
},
);
}
but I always get null in result:
confirmDismiss: (DismissDirection direction) async {
var result = await showConfirmationDialog(context, 'Are you sure?');
// here result is null always
if (result == true){
print('yes');
return true;
}
else if(result == false){
print('no');
return false;
}
}
How to make a confirmation dialog box in flutter correctly?
I tried some way but didn't got exact outcomes.
How to implement pull to refresh in re-orderable list view in flutter, after some records in list functionality stopped working.
And also re-orderable list takes more space at the end of list where no list item presents.
code to produce pull to refresh
Widget todoList() {
return StreamBuilder<dynamic>(
stream: toDoBloc.getTodos,
builder: (BuildContext context, AsyncSnapshot<dynamic> snap) {
if (!snap.hasData) {
return Center(
child: SizedBox(
height: 30.0,
width: 30.0,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
),
),
);
} else {
_refreshController.refreshCompleted();
return SmartRefresher(
enablePullDown: true,
controller: _refreshController,
onRefresh: () async {
await Future<dynamic>.delayed(
const Duration(milliseconds: 1000),
);
toDoBloc.toDoList(
prefsObject.getString('circleId'),
);
},
header: ClassicHeader(
key: centerKey,
completeIcon: null,
),
child: listView(snap.data),
);
}
});
}
code to make a list re-orderable
Widget listview(list) {
final List child = map<Widget>(list, (index, i) {
return _singleToDoWidget(list[index], index);
});
void _onReorder(int oldIndex, int newIndex) {
setState(() {
if (newIndex > oldIndex) {
newIndex -= 1;
}
final List oldIndexIds = map<String>(list, (index, i) {
return list[index].id;
});
final dynamic selectedListItem = list[oldIndex];
list.removeAt(oldIndex);
list.insert(newIndex, selectedListItem);
final List newIndexIds = map<String>(list, (index, i) {
return list[index].id;
});
toDoBloc.toDoOrderList(oldIndexIds, newIndexIds);
});
}
return ReorderableListView(
header: null, onReorder: _onReorder, children: child);
}
I implement pull to refresh in re-orderable list view using RefreshIndicator class
Future<dynamic> refreshList() async {
await Future.delayed(const Duration(seconds: 1));
return null; //do some here.
}
Widget todoList() {
return StreamBuilder<dynamic>(
stream: toDoBloc.getTodos,
builder: (BuildContext context, AsyncSnapshot<dynamic> snap) {
if (!snap.hasData) {
return Center(
child: SizedBox(
height: 30.0,
width: 30.0,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
),
),
);
} else {
_refreshController.refreshCompleted();
return RefreshIndicator(
key: refreshKey,
color: pinkColor,
onRefresh: () async {
await refreshList();
},
child: _listView(snap.data),
);
}
});
}