Flutter角度计算插件angles的使用
Flutter角度计算插件angles的使用
Dart Angles
这个包提供了一个Angle
类型,旨在消除诸如...deg
或...rad
这样的变量后缀的使用,以及手动的角度单位转换(如x * rad2deg
等)。因此,这个包的存在意义不是为了提供功能,而是为了提高代码的可维护性,从而降低维护成本和错误。
角度可以从以下单位构造:度、弧度、百分度和转。
单位 | 一个完整圆周的数量 |
---|---|
度 | 360° |
弧度 | 2π |
百分度 | 400ᵍ |
转 | 1 |
示例代码
// 各种定义半圈的方法:
a0 = Angle.degrees(180.0);
a1 = Angle.radians(math.pi);
a2 = Angle.turns(0.5);
a0.degrees; // = 180.0
a1.radians; // = 3.1415...
a2.turns; // = 0.5
完整示例Demo
下面是一个完整的示例demo,演示了如何在Flutter项目中使用angles
插件:
import 'package:flutter/material.dart';
import 'package:angles/angles.dart';
import 'dart:math' as math;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Angles Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Angles Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _angleDegrees = 0.0;
double _angleRadians = 0.0;
double _angleTurns = 0.0;
double _angleGradians = 0.0;
void _setAngle(double value, String unit) {
setState(() {
switch (unit) {
case 'degrees':
_angleDegrees = value;
break;
case 'radians':
_angleRadians = value;
break;
case 'turns':
_angleTurns = value;
break;
case 'gradians':
_angleGradians = value;
break;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Enter an angle and select the unit:',
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: TextField(
onChanged: (value) {
if (value.isNotEmpty) {
_setAngle(double.parse(value), 'degrees');
} else {
_setAngle(0.0, 'degrees');
}
},
decoration: InputDecoration(
labelText: 'Degrees',
),
keyboardType: TextInputType.number,
),
),
Expanded(
child: TextField(
onChanged: (value) {
if (value.isNotEmpty) {
_setAngle(double.parse(value), 'radians');
} else {
_setAngle(0.0, 'radians');
}
},
decoration: InputDecoration(
labelText: 'Radians',
),
keyboardType: TextInputType.number,
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: TextField(
onChanged: (value) {
if (value.isNotEmpty) {
_setAngle(double.parse(value), 'turns');
} else {
_setAngle(0.0, 'turns');
}
},
decoration: InputDecoration(
labelText: 'Turns',
),
keyboardType: TextInputType.number,
),
),
Expanded(
child: TextField(
onChanged: (value) {
if (value.isNotEmpty) {
_setAngle(double.parse(value), 'gradians');
} else {
_setAngle(0.0, 'gradians');
}
},
decoration: InputDecoration(
labelText: 'Gradians',
),
keyboardType: TextInputType.number,
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
final angleFromDegrees = Angle.degrees(_angleDegrees);
final angleFromRadians = Angle.radians(_angleRadians);
final angleFromTurns = Angle.turns(_angleTurns);
final angleFromGradians = Angle.gradians(_angleGradians);
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Converted Angles'),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Degrees: ${angleFromDegrees.degrees}'),
Text('Radians: ${angleFromRadians.radians}'),
Text('Turns: ${angleFromTurns.turns}'),
Text('Gradians: ${angleFromGradians.gradians}'),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('Close'),
),
],
),
);
},
child: Text('Convert'),
),
),
],
),
),
);
}
}
文档
构造函数
Angle.degrees(x)
Angle.radians(x)
Angle.gradians(x)
Angle.turns(x)
访问器
.degrees()
.radians()
.gradians()
.turns()
三角函数
.sin()
.cos()
.tan()
Angle.asin(x)
Angle.acos(x)
Angle.atan(x)
Angle.atan2(y, x)
Angle.atanFullTurn(y, x)
Angle.cartesian(x, y)
实用函数
.toString()
.approximately(other, range)
.ratio(other)
.normalized
所有算术和关系运算符都已定义在角度上。
更多关于Flutter角度计算插件angles的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复