如何实现Android平台的wrap_content 和match_parent
你可以按照如下方式实现:
1、Width = Wrap_content Height=Wrap_content:
Wrap(
children: <Widget>[your_child])
2、Width = Match_parent Height=Match_parent:
Container(
height: double.infinity,
width: double.infinity,child:your_child)
3、Width = Match_parent ,Height = Wrap_conten:
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[*your_child*],
);
4、Width = Wrap_content ,Height = Match_parent:
Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[your_child],
);
如何避免FutureBuilder频繁执行future方法
错误用法:
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: httpCall(),
builder: (context, snapshot) {
},
);
}
正确用法:
class _ExampleState extends State<Example> {
Future<int> future;
@override
void initState() {
future = Future.value(42);
super.initState();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (context, snapshot) {
},
);
}
}
底部导航切换导致重建问题
在使用底部导航时经常会使用如下写法:
Widget _currentBody;
@override
Widget build(BuildContext context) {
return Scaffold(
body: _currentBody,
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
...
],
onTap: (index) {
_bottomNavigationChange(index);
},
),
);
}
_bottomNavigationChange(int index) {
switch (index) {
case 0:
_currentBody = OnePage();
break;
case 1:
_currentBody = TwoPage();
break;
case 2:
_currentBody = ThreePage();
break;
}
setState(() {});
}
此