簡體   English   中英

如何在 Flutter 中創建帶有圖標和文本的高架按鈕

[英]How to Create Elevated Button with Icon and Text in Flutter

如何使用最新的按鈕小部件 ElevatedButton 創建具有文本和圖標的按鈕。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Elevated Button',
      home: FlutterExample(),
    );
  }
}

class FlutterExample extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Elevated Button with Icon and Text')),
        body: Center(
            child: ElevatedButton.icon(
          icon: Icon(
            Icons.home,
            color: Colors.green,
            size: 30.0,
          ),
          label: Text('Elevated Button'),
          onPressed: () {
            print('Button Pressed');
          },
          style: ElevatedButton.styleFrom(
            shape: new RoundedRectangleBorder(
              borderRadius: new BorderRadius.circular(20.0),
            ),
          ),
        )));
  }
}

在這種情況下,您可以添加具有多個子項的Row()Wrap()小部件Icon()Text()

ElevatedButton(
    onPressed:() {},
    child: Wrap(
        children: <Widget>[
        Icon(
            Icons.favorite,
            color: Colors.pink,
            size: 24.0,
        ),
        SizedBox(
            width:10,
        ),
        Text("Click me!", style:TextStyle(fontSize:20)),
        ],
    ),
),

您可以使用ElevatedButton.icon構造函數:

ElevatedButton.icon(
                  onPressed: () {
                      //OnPressed Logic
                  },
                  icon: const Icon(Icons.plus_one),
                  label: const Text(
                      "Button Text"
                  )
                ),

您可以為此使用 ElevatedButton.icon 構造函數

ElevatedButton.icon(
            icon: const Icon(Icons.add, size: 18),
            label: Text('My elevated button'),
            onPressed: () {},
          ),

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM