facebook twitter hatena line email

「Flutter/UI/Button」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(body内にボタンを表示)
(body内にボタンを表示)
行18: 行18:
 
         ),
 
         ),
 
  ),
 
  ),
 +
 +
==上部中央に青いボタン==
 +
<pre>
 +
Container(
 +
          padding: const EdgeInsets.symmetric(vertical: 24.0, horizontal: 72.0)
 +
              + MediaQuery.of(context).padding,
 +
          child: Center(
 +
            child: Column(
 +
            mainAxisAlignment: MainAxisAlignment.center,
 +
            children: <Widget>[
 +
              CupertinoButton.filled(
 +
                child: const Text('ボタン'),
 +
                onPressed: () {
 +
                  // 処理
 +
                },
 +
              ),
 +
            ],
 +
            ),
 +
          ),
 +
        ),
 +
</pre>
  
 
==画面右下にボタンを表示==
 
==画面右下にボタンを表示==

2019年5月16日 (木) 01:07時点における版

body内にボタンを表示

テキストの下に灰色のボタン

body: Center(
       child: Column(
         mainAxisAlignment: MainAxisAlignment.center,
         children: <Widget>[
           Text(
             'hello',
           ),
           RaisedButton(
             child: Text('button1'),
             onPressed: () {
               // ボタン処理
             },
           ),
         ],
       ),
),

上部中央に青いボタン

Container(
          padding: const EdgeInsets.symmetric(vertical: 24.0, horizontal: 72.0)
              + MediaQuery.of(context).padding,
          child: Center(
            child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              CupertinoButton.filled(
                child: const Text('ボタン'),
                onPressed: () {
                  // 処理
                },
              ),
            ],
            ),
          ),
        ),

画面右下にボタンを表示

floatingActionButtonをScaffoldの項目に追加

return Scaffold(
     appBar: AppBar(
       title: Text(widget.title),
     ),
     body: Center(
       child: Column(
         mainAxisAlignment: MainAxisAlignment.center,
         children: <Widget>[
           Text(
             'hello',
           ),
         ],
       ),
     ),
     floatingActionButton: FloatingActionButton(
       onPressed:() {
              // ボタン処理
            },
       tooltip: 'Increment',
       child: Icon(Icons.add),
     ),
);

上部バーの右にボタンを設置

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("$widget.title"),
        actions: _buildAppBarActionButton()
      ),
    );
  }
  List<Widget> _buildAppBarActionButton() {
    return <Widget>[
      MaterialButton(
        onPressed: () {
        },
        child: Text(
          "Setting",
          style: TextStyle(
              fontSize: 14.0,
              color: Colors.white
          ),
        ),
      )
    ];
  }

iconを右上に設定したい場合

flutter/アイコン追加 [ショートカット]

  List<Widget> _buildAppBarActionButton() {
    return <Widget>[
      MaterialButton(
        onPressed: () {
        },
        child: Icon(
          Icons.settings,
          color: Colors.white,
          size: 30.0,
        )
      )
    ];
  }