第一个简单的示例代码:
1、添加控件
import 'package:flutter/material.dart'; import 'package:english_words/english_words.dart'; void main() { runApp(new MaterialApp( home: new TutoriaHome(), )); } class TutoriaHome extends StatelessWidget { @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( leading: new IconButton(icon: new Icon(Icons.menu),tooltip: 'memu', onPressed: null), title: new Text('appbar'), actions: <Widget>[ new IconButton(icon: new Icon(Icons.search),tooltip: 'search', onPressed: null) ], ), body: new Center( child: new Text('body'), ), floatingActionButton: new FloatingActionButton(tooltip:'fbutton',child: new Icon(Icons.add),onPressed: null), ); } }
-
appBar中的leading和actions;一个前一个后
-
floatingActionButton右下角的button
2、处理手势
class MyButton extends StatelessWidget { @override Widget build(BuildContext context) { return new GestureDetector( onTap: (){ print('MyButton tap'); }, child: new Container( height: 36.0, padding: const EdgeInsets.all(8.0), margin: const EdgeInsets.symmetric(horizontal: 8.0), decoration: new BoxDecoration( borderRadius: new BorderRadius.circular(5.0), color:Colors.lightGreen[500] ), child: new Center( child: new Text('sdsds'), ), ), ); } }
-
自定义button GestureDetector观察点击事件
3、父类子类
import 'package:flutter/material.dart'; import 'package:english_words/english_words.dart'; class Product { const Product({this.name}); final String name; } typedef void CartChangedCallback(Product product, bool inCart); class ShoppingListItem extends StatelessWidget { ShoppingListItem({Product product, this.inCart, this.onCartChanged}) : product = product, super(key: new ObjectKey(product)); final Product product; final bool inCart; final CartChangedCallback onCartChanged; Color _getColor(BuildContext context) { // The theme depends on the BuildContext because different parts of the tree // can have different themes. The BuildContext indicates where the build is // taking place and therefore which theme to use. return inCart ? Colors.black54 : Theme.of(context).primaryColor; } TextStyle _getTextStyle(BuildContext context) { if (!inCart) return null; return new TextStyle( color: Colors.black54, decoration: TextDecoration.lineThrough, ); } @override Widget build(BuildContext context) { return new ListTile( onTap: () { onCartChanged(product, !inCart); }, leading: new CircleAvatar( backgroundColor: _getColor(context), child: new Text(product.name[0]), ), title: new Text(product.name, style: _getTextStyle(context)), ); } } class ShoppingList extends StatefulWidget { ShoppingList({Key key, this.products}) : super(key: key); final List<Product> products; // The framework calls createState the first time a widget appears at a given // location in the tree. If the parent rebuilds and uses the same type of // widget (with the same key), the framework will re-use the State object // instead of creating a new State object. @override _ShoppingListState createState() => new _ShoppingListState(); } class _ShoppingListState extends State<ShoppingList> { Set<Product> _shoppingCart = new Set<Product>(); void _handleCartChanged(Product product, bool inCart) { setState(() { // When user changes what is in the cart, we need to change _shoppingCart // inside a setState call to trigger a rebuild. The framework then calls // build, below, which updates the visual appearance of the app. if (inCart) _shoppingCart.add(product); else _shoppingCart.remove(product); }); } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Shopping List'), ), body: new ListView( padding: new EdgeInsets.symmetric(vertical: 8.0), children: widget.products.map((Product product) { return new ShoppingListItem( product: product, inCart: _shoppingCart.contains(product), onCartChanged: _handleCartChanged, ); }).toList(), ), ); } } void main() { runApp(new MaterialApp( title: 'Shopping App', home: new ShoppingList( products: <Product>[ new Product(name: 'Eggs'), new Product(name: 'Flour'), new Product(name: 'Chocolate chips'), ], ), )); }
完