赞
踩
状态管理 / 路由管理 / 依赖管理
这三部分之间存在联系
建议看官网文章,很详细 ,pub.dev搜索get
pub.dev的文档
状态管理文章相关链接
实现一个计算器,运用GetX去管理它
构建一个计算器界面
flutter_screenutil: ^5.9.0
import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; void main() { runApp(App()); } class App extends StatelessWidget { @override Widget build(BuildContext context) { return ScreenUtilInit( builder: (_, child) { return MaterialApp( debugShowCheckedModeBanner: false, home: child, ); }, child: Main(), ); } } class Main extends StatelessWidget { final Color operatorColor = const Color.fromRGBO(93, 93, 93, 1); final Color numberColor = const Color.fromRGBO(119, 119, 119, 1); final Color operatorColorTwo = const Color.fromRGBO(242, 163, 60, 1); final Color borderColor = const Color.fromRGBO(76, 76, 76, 1); const Main({super.key}); @override Widget build(BuildContext context) { return SafeArea( child: Scaffold( body: Column( children: [ buildUserButton( text: '0', bgColor: operatorColor, width: 1.sw, alignment: Alignment.centerRight, fontSize: 60.r, padding: EdgeInsets.symmetric(horizontal: 25.r), ), Row( children: [ buildUserButton(text: 'AC', bgColor: operatorColor), buildUserButton(text: '±', bgColor: operatorColor), buildUserButton(text: '%', bgColor: operatorColor), buildUserButton(text: '÷', bgColor: operatorColorTwo), ], ), Row( children: [ buildUserButton(text: '7', bgColor: numberColor), buildUserButton(text: '8', bgColor: numberColor), buildUserButton(text: '9', bgColor: numberColor), buildUserButton(text: '×', bgColor: operatorColorTwo), ], ), Row( children: [ buildUserButton(text: '4', bgColor: numberColor), buildUserButton(text: '5', bgColor: numberColor), buildUserButton(text: '6', bgColor: numberColor), buildUserButton(text: '-', bgColor: operatorColorTwo), ], ), Row( children: [ buildUserButton(text: '1', bgColor: numberColor), buildUserButton(text: '2', bgColor: numberColor), buildUserButton(text: '3', bgColor: numberColor), buildUserButton(text: '+', bgColor: operatorColorTwo), ], ), Row( children: [ buildUserButton( text: '0', bgColor: numberColor, width: 1.sw / 2, ), buildUserButton(text: '.', bgColor: numberColor), buildUserButton(text: '=', bgColor: operatorColorTwo), ], ), ], ), ), ); } Widget buildUserButton({ required String text, required Color bgColor, double? width, Alignment? alignment, double? fontSize, EdgeInsetsGeometry? padding, }) { return Container( alignment: alignment ?? Alignment.center, width: width ?? 1.sw / 4, height: 1.sw / 4, decoration: BoxDecoration( color: bgColor, border: Border.all(color: borderColor), ), padding: padding ?? EdgeInsets.all(10.r), child: Text( text, style: TextStyle( color: const Color.fromRGBO(235, 235, 235, 1), fontSize: fontSize ?? 30.r, ), ), ); } }
引入getX
get: ^4.6.6
计算器输入数字
说明:需要跟踪的变量加上后缀.obs。这样的写法,我说明一下。其实是源于dart语言的extension,即扩展。比如:对文本进行扩展,那么文本类型的数据就会有你定义的功能。见下
extension MyStringExtension on String {
String get sayHello {
String words = "$this say hello!!!";
return words;
}
}
void main() {
print("阿笙".sayHello);
/// 阿笙 say hello!!!
}
同样的.obs,就是对各种数据类型进行扩展
显然,不止文本一个,还有很多,这里一一列举/使用,可以参考官网的说明文档
String / bool / double / int 这四个写法类似。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。