如何在 Flutter 中创建带有图标和文本的提升按钮

Jav*_*haq 10 dart flutter

如何使用最新的按钮小部件 ElevatedButton 创建具有文本和图标的按钮。

San*_* AV 18

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),
            ),
          ),
        )));
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 11

在这种情况下,您可以添加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)),
        ],
    ),
),
Run Code Online (Sandbox Code Playgroud)


tom*_*fic 8

您可以使用ElevatedButton.icon构造函数:

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