输入框(TextField)去边框

  1. TextField(
  2. decoration: InputDecoration(
  3. border: InputBorder.none
  4. )

超出的文字显示省略号

  1. Text(
  2. item.title,
  3. maxLines: 2,
  4. overflow: TextOverflow.ellipsis,
  5. style: ...
  6. ),

图片加占位符和圆角

  1. Container(
  2. padding: const EdgeInsets.all(20.0),
  3. width: double.infinity,
  4. height: 150,
  5. child: ClipRRect(
  6. borderRadius: BorderRadius.circular(6.0),
  7. child: FadeInImage.assetNetwork(
  8. placeholder: "images/default.jpg",
  9. image: 'https://via.placeholder.com/650x200',
  10. fit: BoxFit.cover,
  11. ),
  12. ),
  13. )

设置Container全屏宽度

  1. Container(
  2. width: MediaQuery.of(context).size.width,
  3. )

设置部分圆角

  1. borderRadius: const BorderRadius.only(topLeft: const Radius.circular(10.0), topRight: const Radius.circular(10.0))

点击空白区域收回软键盘

  1. GestureDetector(
  2. behavior: HitTestBehavior.translucent,
  3. onTap: () {
  4. // 触摸收起键盘
  5. FocusScope.of(context).requestFocus(FocusNode());
  6. },
  7. child: *******
  8. }

禁止 ScrollView 滑动

  1. physics: const NeverScrollableScrollPhysics()

showDialog() 或 showBottomSheet() 更改状态

因这两个方法是生成了新的路由了,因此没办法用当前页面的setState来做状态更新。
这里使用 StatefulBuilder 组件更新 dialog 内的状态

  1. StatefulBuilder(
  2. builder: (context, state) {
  3. //当前使用新的方法 state 来更新状态
  4. state((){});
  5. });

路由重定向

  1. Navigator.of(context).pushAndRemoveUntil(
  2. new MaterialPageRoute(builder: (context) => new MyApp() ),
  3. (route) => route == null
  4. );

顶部固定剩余部分滑动

  1. // 方式一
  2. LayoutBuilder(
  3. builder: (context, constrains) {
  4. return Column(
  5. children: <Widget>[
  6. sizeBox(
  7. height: 50,
  8. child: ...
  9. ),
  10. SizedBox(
  11. height: constrains.maxHeight-50,
  12. child: ...
  13. )
  14. ]
  15. )
  16. }
  17. );
  18. // 方式二
  19. SizeBox(
  20. width: 375,
  21. height: 800,
  22. child: Column(
  23. children: [
  24. sizeBox(
  25. height: 50,
  26. child: ...
  27. ),
  28. Expanded(
  29. child: ...
  30. )
  31. ]
  32. )
  33. )

widget列表中异步循环渲染组件

  1. List renderData = [...];
  2. Iterable<Widget> get widgetList sync* {
  3. for (int i = 0; i < renderData.length; i++) {
  4. yield Container(...);
  5. }
  6. }
  7. // 在`widget`列表中使用(Row, Column, ListView...)
  8. Row(
  9. ...
  10. children: widgetList.toList()
  11. )

设置为宽度为最大宽度

  1. MediaQuery.of(context).size.width

设置渐变

  1. BoxDecoration(
  2. ...
  3. gradient: LinearGradient(
  4. begin: Alignment(0, 0),
  5. end: Alignment(1, 1),
  6. colors: [Color(0xff1eccfe), Color(0xff0191fa)]
  7. )
  8. )

统一Android和IOS头部

  1. Scaffold(
  2. appBar: AppBar(
  3. title: Text('标题', style: TextStyle(color: Color(0xff333333), fontSize: 22)),
  4. leading: new IconButton(
  5. onPressed: () {
  6. Navigator.pop(context);
  7. },
  8. icon: Icon(Icons.chevron_left, color: Color(0xff333333), size: 22),
  9. ),
  10. elevation: 0,
  11. ),
  12. body: ...
  13. )

自定义tab

  1. class Orders extends StatefulWidget {
  2. final dynamic option;
  3. Orders(this.option);
  4. OrdersState createState () => OrdersState();
  5. }
  6. class OrdersState extends State<Orders> {
  7. final List<Tab> myTabs = <Tab>[
  8. new Tab(text: '全部'),
  9. new Tab(text: '已完成'),
  10. new Tab(text: '未完成'),
  11. ];
  12. TabController _controller;
  13. @override
  14. Widget build(BuildContext context) {
  15. return DefaultTabController(
  16. length: myTabs.length,
  17. child: Scaffold(
  18. appBar: AppBar(
  19. title: Text('订单管理', style: TextStyle(color: kLingyiText500, fontSize: ScreenUtil.getInstance().setSp(32))),
  20. leading: new IconButton(
  21. onPressed: () {
  22. Navigator.pop(context);
  23. },
  24. icon: Icon(Icons.chevron_left, color: kLingyiText500, size: ScreenUtil.getInstance().setSize(42)),
  25. ),
  26. bottom: TabBar(
  27. tabs: myTabs,
  28. controller: _controller,
  29. indicatorColor: Colors.red,
  30. indicatorSize: TabBarIndicatorSize.tab,
  31. isScrollable: false,
  32. labelColor: Colors.red,
  33. unselectedLabelColor: Colors.black,
  34. indicatorWeight: 5.0,
  35. labelStyle: TextStyle(height: 1),
  36. ),
  37. ),
  38. body:TabBarView(
  39. children: myTabs.map((Tab tab) {
  40. // var tabIndex = myTabs.indexOf(tab);
  41. // return OrderBody(tabIndex);
  42. ...
  43. }).toList(),
  44. )
  45. )
  46. );
  47. }
  48. }

webView的使用

  1. import 'package:flutter/material.dart';
  2. import 'package:webview_flutter/webview_flutter.dart';
  3. class Webview extends StatelessWidget {
  4. final dynamic option;
  5. Webview(this.option);
  6. @override
  7. Widget build(BuildContext context) {
  8. String url = option.query['url'];
  9. String title = option.query['title'];
  10. return Scaffold(
  11. appBar: AppBar(
  12. title: Text(title),
  13. leading: new IconButton(
  14. onPressed: () {
  15. Navigator.pop(context);
  16. },
  17. icon: Icon(Icons.chevron_left),
  18. ),
  19. elevation: 0,
  20. ),
  21. body: WebView(
  22. initialUrl: url,
  23. javascriptMode: JavascriptMode.unrestricted,
  24. ),
  25. );
  26. }
  27. }