Flutter基礎(chǔ)控件

基礎(chǔ) Widgets

Container

一個(gè)擁有繪制、定位、調(diào)整大小的widget

new Container(
            constraints: BoxConstraints.expand(
              height: Theme.of(context).textTheme.display1.fontSize * 1.1 + 200.0,
            ),
            padding: const EdgeInsets.all(8.0),
            color: Colors.blue[600],
            alignment: Alignment.center,
            child: Text('Hello World',
            style:Theme.of(context).textTheme.display1.copyWith(color: Colors.white)),
            transform: Matrix4.rotationZ(0.1),
          ),

Row

在水平方向上排列子Widget的列表
Expanded
Creates a widget that expands a child of a [Row], [Column], or [Flex] so that the child fills the available space along the flex widget's main axis.

 new Row(
            children: <Widget>[
              Expanded(
                child: Text('Deliver features faster',textAlign:TextAlign.center),
              ),
              Expanded(
                child: Text('could you speak chinese',textAlign:TextAlign.center),
              ),
              Expanded(
                child: FittedBox(
                  fit: BoxFit.contain, // otherwise the logo will be tiny
                  child: const FlutterLogo(),
                ),
              )
            ],
          )
new Row(
            children: <Widget>[
              const FlutterLogo(),
              const Expanded(
                child: Text('Flutter\'s hot reload helps you quickly and easily experiment, build UIs, add features, and fix bug faster. Experience sub-second reload times, without losing state, on emulators, simulators, and hardware for iOS and Android.'),
              ),
              const Icon(Icons.sentiment_very_satisfied),
            ],
          )

Column

在垂直方向上排列子widget的列表

new Column(
            children: <Widget>[
              Text('Deliver feature faster'),
              Text('Craft beautiful UIs'),
              Expanded(
                child: FittedBox(
                  fit: BoxFit.contain,
                  child: const FlutterLogo(),
                ),
              )
            ],
          ),
new Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text('We move under cover and we move as one'),
              Text('Through the night, we have one shot to live another day'),
              Text('We cannot let a stray gunshot give us away'),
              Text('We will fight up close, seize the moment and stay in it'),
              Text('It’s either that or meet the business end of a bayonet'),
              Text('The code word is ‘Rochambeau,’ dig me?'),
            ],
          ),

Image

一個(gè)顯示圖片的widget

The following image formats are supported: JPEG, PNG, GIF, Animated GIF, WebP, Animated WebP, BMP, and WBMP

Text

單一格式的文本

new Text(
            'hello,! How are you?',
            textAlign:TextAlign.left,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontWeight: FontWeight.bold),
          )
const Text.rich(TextSpan(text: 'Hello ', children: <TextSpan>[
          TextSpan(
              text: 'beautiful ', style: TextStyle(fontStyle: FontStyle.italic)),
          TextSpan(
              text: 'world', style: TextStyle(fontWeight: FontWeight.bold)),
        ])),

Icon

A Material Design icon
各種各樣的圖標(biāo) √,+,??等

new Icon(
              Icons.add,
              color:Colors.pink,
              size: 30.0,
            )

RaisedButton

Material Design中的button,一個(gè)凸起的材質(zhì)矩形按鈕

Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            const RaisedButton(
              onPressed: null,
              child: Text('Disabled Button', style: TextStyle(fontSize: 20)),
            ),
            const SizedBox(
              height: 30,
            ),
            RaisedButton(
              onPressed: () {},
              child:
                  const Text('Enabled Button', style: TextStyle(fontSize: 20)),
            ),
            const SizedBox(
              height: 30,
            ),
            RaisedButton(
              onPressed: () {},
              textColor: Colors.white,
              padding: const EdgeInsets.all(0.0),
              child: Container(
                decoration: const BoxDecoration(
                    gradient: LinearGradient(colors: <Color>[
                        Color(0xFF0D47A1),
                        Color(0xFF1976D2),  
                        Color(0xFF42A5F5),
                ])),
                padding: const EdgeInsets.all(10.0),
                child: const Text(
                  'Gradient Button',
                  style:TextStyle(fontSize:20)
                ),
              ),
            )
          ],
        )),

Scaffold

Material Design 布局結(jié)構(gòu)的基本實(shí)現(xiàn)。此類(lèi)提供了用于顯示 drawer、Snackbar和底部sheet的API。

class Counter extends StatefulWidget {
  @override
  _CounterState createState() => new _CounterState();
}

class _CounterState extends State<Counter> {
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Sample Code'),
      ),
      body: Center(
        child: Text('You have pressed the button $_count times'),
      ),
      bottomNavigationBar: BottomAppBar(
        child: Container(height: 50.0,),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed:() => setState(() {
          _count++;
        }),
        tooltip: 'Increment Counter',
        child: Icon(Icons.add),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}

AppBar 基礎(chǔ)

一個(gè)典型的AppBar,帶有標(biāo)題、操作和溢出的下拉菜單。

class BasicAppBarSample extends StatefulWidget {
  @override
  _BasicAppBarSampleState createState() => _BasicAppBarSampleState();
}

class _BasicAppBarSampleState extends State<BasicAppBarSample> {
  Choice _selectedChoice = choices[0];

  void _select(Choice choice) {
    setState(() {
      _selectedChoice = choice;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Basic AppBar'),
          actions: <Widget>[
            new IconButton(
              icon: new Icon(choices[0].icon),
              onPressed: (){ _select(choices[0]);},
            ),
            new IconButton(
              icon: new Icon(choices[1].icon),
              onPressed: (){ _select(choices[1]); },
            ),
            new PopupMenuButton<Choice>(
              onSelected: _select,
              itemBuilder: (BuildContext context) {
                return choices.skip(2).map((Choice choice){
                  return new PopupMenuItem<Choice>(
                    value: choice,
                    child: new Text(choice.title),
                  );
                }).toList();
              },
            )
          ],
        ),
        body: new Padding(
          padding: const EdgeInsets.all(16.0),
          child: new ChoiceCard(choice: _selectedChoice,),
        ),
      ),
    );
  }

}

class Choice {
  const Choice({this.title, this.icon});
  final String title;
  final IconData icon;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
];


class ChoiceCard extends StatelessWidget {
  const ChoiceCard ({Key key,this.choice}) :super(key:key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle =Theme.of(context).textTheme.display1;
    return new Card(
      color: Colors.white,
      child: new Center(
        child: new Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            new Icon(choice.icon,size:128.0,color:textStyle.color),
            new Text(choice.title,style:textStyle),
          ],
        ),
      ),
    );
  }
}
Scaffold Material Design 布局結(jié)構(gòu)的基本實(shí)現(xiàn)。此類(lèi)提供了用于顯示 drawer、Snackbar和底部sheet的API。

選項(xiàng)卡式的AppBar

一個(gè)以TabBar作為底部widget的AppBar。
TabBar可用于在TabBarView中顯示的頁(yè)面之間導(dǎo)航。雖然TabBar是一個(gè)可以出現(xiàn)在任何地方的普通widget,但它通常包含在應(yīng)用程序的AppBar中。

class TabbadAppBarSample extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home:new DefaultTabController(
        length: choices.length,
        child: new Scaffold(
          appBar: new AppBar(
            title: const Text('Tabbed AppBar'),
            bottom: new TabBar(
              isScrollable: true,
              tabs:choices.map((Choice choice){
                return new Tab(
                  text:choice.title,
                  icon:new Icon(choice.icon),
                );
              }).toList(),
            ),
          ),
          body: new TabBarView(
            children: choices.map((Choice choice) {
              return new Padding(
                padding: const EdgeInsets.all(16.0),
                child: new ChoiceCard(choice: choice),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }

}
選項(xiàng)卡式的AppBar

未完待續(xù)。。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,246評(píng)論 0 23
  • 今天也是好累的一天。 早上7點(diǎn)起,然后睜眼就開(kāi)始背單詞,這個(gè)方法很好,省時(shí)間,堅(jiān)持!然后吃飯,實(shí)驗(yàn)室,口語(yǔ),聽(tīng)力,...
    么得感情的日更機(jī)器閱讀 101評(píng)論 0 1
  • 我這樣說(shuō)一個(gè)“同能量”,也不曉得對(duì)不對(duì),大概的意思是,思維,經(jīng)歷都在同一頻率。 和同能量的人在一起,會(huì)感覺(jué)相見(jiàn)太晚...
    lv小明閱讀 420評(píng)論 0 0
  • ——和偏教黛眉長(zhǎng)《三千溫柔漾眼角》 旗袍秀美人終老, 窗緊難搪歲月刀。 不入詩(shī)詞藏倩影, 要學(xué)嫵媚競(jìng)妖嬈。
    不惑而歌閱讀 1,725評(píng)論 7 17

友情鏈接更多精彩內(nèi)容