The below code is working and showing the progress with percentage.
You can also run this here
https://zapp.run/edit/flutter-z79y06bl79z0?
split=50&lazy=false&entry=lib/main.dart&file=lib/main.dart
copy the above link and you can run the code through this link
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool compressing = false;
bool compressed = false;
double progress = 0.0;
Future<void> compressImages() async{
// If you have 5 images to compress;
for(int i = 1;i<=5;i++){
// this is the compressing time
await Future.delayed(Duration(seconds: 2));
setState(() {
//according to your number of images you can increase the value or percentage here
progress += 0.2;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: compressed
? Text("images compressed 🙂")
: compressing
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// here value can be 0.0 -> 1.0
CircularProgressIndicator(
value: progress,
),
Text("Precentage : ${progress*100}")
],
)
:Text("Want to Compress images"),
),
floatingActionButton: ElevatedButton(
onPressed: () async{
setState(() {
compressing = true;
});
await compressImages();
setState(() {
compressed = true;
});
},
child: Text("Start Compress"),
),
);
}
}
It looks like this
[![Image how it looks][1]][1]
2条答案
按热度按时间jc3wubiy1#
rta7y2nd2#
试试这个: