【发布时间】:2020-05-27 11:45:24
【问题描述】:
我想知道为什么 Flutter 代码在某些设备中低于 60 fps(在这种情况下,Redmi Go 是分析模式,在 UI 图中显示红色条)。有什么办法可以优化它吗?我昨天开始学习 Flutter,所以我想要一些技巧来帮助我理解有状态的小部件和小部件渲染层次结构。
提前致谢。
文件main.dart
import 'package:project/ui/home.dart';
import 'package:flutter/material.dart';
void main() => runApp(new MaterialApp(
home: BillSplitter(),
));
文件home.dart
import 'package:flutter/material.dart';
class BillSplitter extends StatefulWidget {
@override
_BillSplitterState createState() => _BillSplitterState();
}
class _BillSplitterState extends State<BillSplitter> {
int _tipPercentage = 0;
int _personCounter = 1;
double _billAmount = 0.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"APP Teste 1.0",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.purple.withOpacity(0.5),
),
body: Container(
margin: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.1),
alignment: Alignment.center,
color: Colors.white,
child: ListView(
scrollDirection: Axis.vertical,
padding: EdgeInsets.all(20.5),
children: <Widget>[
Container(
width: 150,
height: 150,
decoration: BoxDecoration(
color: Colors.purple.withOpacity(0.1),
borderRadius: BorderRadius.circular(12.0)),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Total por pessoa",
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.normal,
fontSize: 17.0),
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Text(
"R\$ ${_calculateTotalPerPerson(_billAmount, _personCounter, _tipPercentage)}",
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 35.0),
),
)
],
),
),
),
Container(
margin: EdgeInsets.only(top: 20.0),
padding: EdgeInsets.all(12.0),
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(
color: Colors.blueGrey.shade100,
style: BorderStyle.solid),
borderRadius: BorderRadius.circular(12.0)),
child: Column(
children: <Widget>[
TextField(
keyboardType:
TextInputType.numberWithOptions(decimal: true),
style: TextStyle(color: Colors.purple),
decoration: InputDecoration(
prefixText: "Total da Conta: R\$ ",
prefixIcon: Icon(Icons.attach_money)),
onChanged: (String value) {
try {
_billAmount = double.parse(value);
} catch (e) {
_billAmount = 0.0;
}
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Dividir",
style: TextStyle(color: Colors.grey.shade700),
),
Row(
children: <Widget>[
InkWell(
onTap: () {
setState(() {
if (_personCounter > 1) {
_personCounter--;
}
});
},
child: Container(
width: 40.0,
height: 40.0,
margin: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
color: Colors.purpleAccent.withOpacity(0.1)),
child: Center(
child: Text(
"-",
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0),
)),
),
),
Text(
"$_personCounter",
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0),
),
InkWell(
onTap: () {
setState(() {
_personCounter++;
});
},
child: Container(
width: 40.0,
height: 40.0,
margin: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(7.0),
color: Colors.purpleAccent.withOpacity(0.1)),
child: Center(
child: Text(
"+",
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0),
)),
),
),
],
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Gorjeta",
style: TextStyle(color: Colors.grey.shade700),
),
Padding(
padding: const EdgeInsets.all(18.0),
child: Text(
"R\$ ${(_calculateTotalTip(_billAmount, _personCounter, _tipPercentage)).toStringAsFixed(2)}",
style: TextStyle(
color: Colors.purple,
fontWeight: FontWeight.bold,
fontSize: 17.0),
),
)
],
),
Column(
children: <Widget>[
Text(
"$_tipPercentage%",
style: TextStyle(
color: Colors.purple,
fontSize: 17.0,
fontWeight: FontWeight.bold),
),
Slider(
activeColor: Colors.purple,
inactiveColor: Colors.grey,
min: 0,
max: 100,
divisions: 10,
value: _tipPercentage.toDouble(),
onChanged: (double value) {
setState(() {
_tipPercentage = value.round();
});
})
],
)
],
),
)
],
),
),
);
}
_calculateTotalPerPerson(double billAmount, int splitBy, int tipPercentage) {
double totalPerPerson =
(billAmount + _calculateTotalTip(billAmount, splitBy, tipPercentage)) /
splitBy;
return totalPerPerson.toStringAsFixed(2);
}
_calculateTotalTip(double billAmount, int splitBy, int tipPercentage) {
double totalTip = 0.0;
if (billAmount < 0 || billAmount.toString().isEmpty || billAmount == null) {
} else {
totalTip = (billAmount * tipPercentage) / 100;
}
return totalTip;
}
}
【问题讨论】:
-
您需要在发布模式下运行应用程序才能看到真正的影响。执行
flutter run --release或通过flutter build apk构建发布apk(这将产生flat arm64 和32 包含的应用程序)[flutter.dev/docs/deployment/android]. -
如何在发布模式下调试/显示帧率?
-
你可以使用 Dart DevTools。
-
DevTools 是否在发布模式下工作?
标签: android performance flutter mobile dart