大佬教程收集整理的这篇文章主要介绍了【Flutter 实战】简约而不简单的计算器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
先看一下效果:
大家学习UI编程语言时喜欢用哪个 App 当作第一个练手的项目呢?,我喜欢使用 计算器 ,可能是习惯了吧,学习 Android 和 React Native 都用此 App 当作练手的项目。
下面我会一步一步的教大家如何实现此项目。
整个项目的 UI 分为两大部分,一部分是顶部显示数字和计算结果,另一部分是底部的输入按钮。
所以整体布局使用 column,在不同分辨率的手机上,规定底部固定大小,剩余空间都由顶部组件填充,所以顶部组件使用 Expanded 扩充,代码如下:
Container(
padding: EdgeInsets.symmetric(horizontal: 18),child: column(
children: <Widget>[
Expanded(
child: Container(
alignment: Alignment.bottomRight,padding: EdgeInsets.only(right: 10),child: Text(
'$_text',maxLines: 1,style: TextStyle(
color: Colors.white,fontSize: 48,fontWeight: FontWeight.w400),),SizedBox(
height: 20,_CalculatorKeyboard(
onValueChange: _onValueChange,SizedBox(
height: 80,)
],)
_CalculatorKeyboard 是底部的输入按钮组件,也是此项目的重点,除了 0 这个按钮外,其余都是圆形按钮,不同之处是 高亮颜色(按住时颜色)、背景颜色、按钮文本、文本颜色不同,因此先实现一个按钮组件,代码如下:
Ink(
decoration: BoxDecoration(
color: Color(0xFF363636),borderRadius: BorderRadius.all(Radius.circular(200))),child: InkWell(
borderRadius: BorderRadius.all(Radius.circular(200)),highLightcolor: Color(0xFF363636),child: Container(
width: 70,height: 70,alignment: Alignment.center,child: Text(
'1',style: TextStyle(color: Colors.white,fontSize: 24),)
而 0 这个按钮的宽度是两个按钮的宽度 + 两个按钮的间隙,所以 0 按钮代码如下:
Ink(
decoration: BoxDecoration(
color: Color(0xFF363636),child: Container(
width: 158,child: Text(
'0',)
将按钮组件进行封装,其中高亮颜色(按住时颜色)、背景颜色、按钮文本、文本颜色属性作为参数,封装如下:
class _CalculatorItem extends StatelessWidget {
final String text;
final Color textColor;
final Color color;
final Color highLightcolor;
final double width;
final ValueChanged<String> onValueChange;
_CalculatorItem(
{this.text,this.textColor,this.color,this.highLightcolor,this.width,this.onValueChangE});
@override
Widget build(BuildContext context) {
return Ink(
decoration: BoxDecoration(
color: color,child: InkWell(
onTap: () {
onValueChange('$text');
},borderRadius: BorderRadius.all(Radius.circular(200)),highLightcolor: highLightcolor ?? color,child: Container(
width: width ?? 70,padding: EdgeInsets.only(left: width == null ? 0 : 25),alignment: width == null ? Alignment.center : Alignment.centerLeft,child: Text(
'$text',style: TextStyle(color: textColor ?? Colors.white,);
}
}
输入按钮的布局使用 Wrap 布局组件,如果没有 0 这个组件也可以使用 GridView组件,按钮的数据:
final List<Map> _keyboardList = [
{
'text': 'AC','textColor': Colors.black,'color': Color(0xFFA5A5A5),'highLightcolor': Color(0xFFD8D8D8)
},{
'text': '+/-',{
'text': '%',{
'text': '÷','color': Color(0xFFE89E28),'highLightcolor': Color(0xFFEDC68F)
},{'text': '7','color': Color(0xFF363636)},{'text': '8',{'text': '9',{
'text': 'x',{'text': '4',{'text': '5',{'text': '6',{
'text': '-',{'text': '1',{'text': '2',{'text': '3',{
'text': '+',{'text': '0','color': Color(0xFF363636),'width': 158.0},{'text': '.',{
'text': '=',];
整个输入按钮组件:
class _CalculatorKeyboard extends StatelessWidget {
final ValueChanged<String> onValueChange;
const _CalculatorKeyboard({Key key,this.onValueChangE}) : super(key: key);
@override
Widget build(BuildContext context) {
return Wrap(
runSpacing: 18,spacing: 18,children: List.generate(_keyboardList.length,(indeX) {
return _CalculatorItem(
text: _keyboardList[index]['text'],textColor: _keyboardList[index]['textColor'],color: _keyboardList[index]['color'],highLightcolor: _keyboardList[index]['highLightcolor'],width: _keyboardList[index]['width'],onValueChange: onValueChange,);
}),);
}
}
onValueChange 是点击按钮的回调,参数是当前按钮的文本,用于计算,下面说下计算逻辑:
这里有4个变量:
AC 按钮表示清空当前输入,显示 0,同时初始化其他变量:
case 'AC':
_text = '0';
_beforeText = '0';
_isResult = false;
break;
+/- 按钮表示对当前数字取反,比如 5->-5:
case '+/-':
if (_text.startsWith('-')) {
_text = _text.subString(1);
} else {
_text = '-$_text';
}
break;
% 按钮表示当前数除以100:
case '%':
double d = _value2Double(_text);
_isResult = true;
_text = '${d / 100.0}';
break;
+、-、x、÷ 按钮,保存当前 操作符号:
case '+':
case '-':
case 'x':
case '÷':
_isResult = false;
_operateText = value;
0-9 和 . 按钮根据是否是计算结果和是否有操作符号进行显示:
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
if (_isResult) {
_text = value;
}
if (_operateText.isnotEmpty && _beforeText.isEmpty) {
_beforeText = _text;
_text = '';
}
_text += value;
if (_text.startsWith('0')) {
_text = _text.subString(1);
}
break;
= 按钮计算结果:
case '=':
double d = _value2Double(_beforeText);
double d1 = _value2Double(_text);
switch (_operateText) {
case '+':
_text = '${d + d1}';
break;
case '-':
_text = '${d - d1}';
break;
case 'x':
_text = '${d * d1}';
break;
case '÷':
_text = '${d / d1}';
break;
}
_beforeText = '';
_isResult = true;
_operateText = '';
break;
double _value2Double(String value) {
if (_text.startsWith('-')) {
String s = value.subString(1);
return double.parse(s) * -1;
} else {
return double.parse(value);
}
}
回过头来,发现代码仅仅只有250多行,当然App也是有不足的地方:
老孟Flutter博客地址(330个控件用法):http://laomengit.com
欢迎加入Flutter交流群(微信:laomENGIt)、关注公众号【老孟Flutter】:
以上是大佬教程为你收集整理的【Flutter 实战】简约而不简单的计算器全部内容,希望文章能够帮你解决【Flutter 实战】简约而不简单的计算器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。