import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('FlutterDemo')), body: LayoutDemo(), )); } } class LayoutDemo extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return RaisedButton( child: Text('第一季'), textColor: Theme.of(context).accentColor, onPressed: (){ }, ); } }
可是單獨爲了一個按鈕寫一個組件的話,當須要多個按鈕的時候,就須要進行封裝了。app
class LayoutDemo extends StatelessWidget { @override Widget build(BuildContext context) { return Row( children: <Widget>[ MyButton("第1集"), MyButton("第2集"), MyButton("第3集"), MyButton("第4集"), MyButton("第5集"), MyButton("第6集"), MyButton("第7集"), MyButton("第8集"), MyButton("第9集"), MyButton("第10集"), MyButton("第11集"), MyButton("第3集"), MyButton("第4集"), MyButton("第5集"), MyButton("第6集"), MyButton("第7集"), MyButton("第8集"), MyButton("第9集"), MyButton("第10集"), MyButton("第11集"), ], ), ); } } class MyButton extends StatelessWidget{ final String text; const MyButton(this.text,{Key key}) : super(key: key); @override Widget build(BuildContext context) { // TODO: implement build return RaisedButton( child: Text(this.text), textColor:Theme.of(context).accentColor, onPressed: (){ } ); } }
此時,因爲按鈕數量較多,一行排不下了,可是並無像咱們預期的那樣自動換行,而是出現了溢出的提示,這種狀況下,咱們就須要使用Wrap組件了。less
class LayoutDemo extends StatelessWidget { @override Widget build(BuildContext context) { return Wrap( spacing:10, runSpacing: 10, children: <Widget>[ MyButton("第1集"), MyButton("第2集"), MyButton("第3集"), MyButton("第4集"), MyButton("第5集"), MyButton("第6集"), MyButton("第7集"), MyButton("第8集"), MyButton("第9集"), MyButton("第10集"), MyButton("第11集"), MyButton("第3集"), MyButton("第4集"), MyButton("第5集"), MyButton("第6集"), MyButton("第7集"), MyButton("第8集"), MyButton("第9集"), MyButton("第10集"), MyButton("第11集"), ], ); } } class MyButton extends StatelessWidget{ final String text; const MyButton(this.text,{Key key}) : super(key: key); @override Widget build(BuildContext context) { // TODO: implement build return RaisedButton( child: Text(this.text), textColor:Theme.of(context).accentColor, onPressed: (){ } ); } }
如今,功能已經實現了,咱們能夠在此基礎上,結合其餘的組件進行一下界面優化了:ide
class LayoutDemo extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return Container( height: 600, width: 400, color: Colors.pink, padding: EdgeInsets.all(10), child: Wrap( spacing:10, runSpacing: 10, direction: Axis.vertical, // alignment:WrapAlignment.spaceEvenly, // runAlignment: WrapAlignment.center, children: <Widget>[ MyButton("第1集"), MyButton("第2集"), MyButton("第3集"), MyButton("第4集"), MyButton("第5集"), MyButton("第6集"), MyButton("第7集"), MyButton("第8集"), MyButton("第9集"), MyButton("第10集"), MyButton("第11集"), MyButton("第3集"), MyButton("第4集"), MyButton("第5集"), MyButton("第6集"), MyButton("第7集"), MyButton("第8集"), MyButton("第9集"), MyButton("第10集"), MyButton("第11集"), ], ), ); } } class MyButton extends StatelessWidget{ final String text; const MyButton(this.text,{Key key}) : super(key: key); @override Widget build(BuildContext context) { // TODO: implement build return RaisedButton( child: Text(this.text), textColor:Theme.of(context).accentColor, onPressed: (){ } ); } }