Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'dart:math';
- import 'dart:ui';
- void main() => runApp(new MyApp());
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return new MaterialApp(
- title: 'Flutter Demo',
- theme: new ThemeData(
- primarySwatch: Colors.red,
- ),
- home: new MyHomePage(title: 'Flutter Demo Home Page'),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage({Key key, this.title}) : super(key: key);
- final String title;
- @override
- _MyHomePageState createState() => new _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin{
- String batterylevel;
- int percentage = 0;
- int newPercentage;
- AnimationController percentageAnimationController;
- @override
- void initState() {
- super.initState();
- setState(() {
- percentage = 0;
- });
- percentageAnimationController = new AnimationController(
- vsync: this,
- duration: new Duration(milliseconds: 5000)
- )..addListener((){
- setState((){
- percentage = lerpDouble(0.0,newPercentage.toDouble(),percentageAnimationController.value).toInt();
- });
- });
- percentageAnimationController.addStatusListener((state) => print("test: $state"));
- }
- void _fetchdata() {
- setState(() {
- print("before percent : $percentage");
- newPercentage = percentage+10;
- if(newPercentage>=100){
- percentage = 0;
- newPercentage = 0;
- }
- print("percentage : $percentage");
- percentageAnimationController.forward(from: 0.0);
- batterylevel = newPercentage.toString();
- percentage = newPercentage;
- print("battery level : $batterylevel");
- });
- }
- @override
- Widget build(BuildContext context) {
- return new Scaffold(
- appBar: new AppBar(
- title: new Text(widget.title),
- ),
- body: new Center(
- child: new Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- new CustomPaint(
- foregroundPainter: new MyPainter(
- lineColor: Colors.red[50],
- completeColor: Colors.redAccent,
- completePercent: percentage.toDouble(),
- width: 12.0
- ),
- child: new Padding(
- padding: const EdgeInsets.all(32.0),
- child: new Text(
- '$batterylevel',
- ),
- ),
- ),
- ],
- ),
- ),
- floatingActionButton: new FloatingActionButton(
- onPressed: _fetchdata,
- tooltip: 'Refresh',
- child: new Icon(Icons.refresh),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
- }
- }
- class MyPainter extends CustomPainter{
- Color lineColor;
- Color completeColor;
- double completePercent;
- double width;
- MyPainter({this.lineColor,this.completeColor,this.completePercent,this.width});
- @override
- void paint(Canvas canvas, Size size) {
- Paint line = new Paint()
- ..color = lineColor
- ..strokeCap = StrokeCap.round
- ..style = PaintingStyle.stroke
- ..strokeWidth = width;
- Paint complete = new Paint()
- ..color = completeColor
- ..strokeCap = StrokeCap.round
- ..style = PaintingStyle.stroke
- ..strokeWidth = width;
- Offset center = new Offset(size.width/2, size.height/2);
- double radius = min(size.width/2,size.height/2);
- canvas.drawCircle(
- center,
- radius,
- line
- );
- double arcAngle = 2*pi* (completePercent/100);
- canvas.drawArc(
- new Rect.fromCircle(center: center,radius: radius),
- -pi/2,
- arcAngle,
- false,
- complete
- );
- }
- @override
- bool shouldRepaint(CustomPainter oldDelegate) {
- return true;
- }
- }
Add Comment
Please, Sign In to add comment