A
A
Alexander Krivitsky2020-01-13 07:57:12
Dart
Alexander Krivitsky, 2020-01-13 07:57:12

How to add a button in Dart?

Started writing a program in Flutter. I do not know where to start, I decided to start by adding a button. Please tell me how to create a button and its functionality.

floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiT, 2020-01-13
@MiT_73

Here is an example of how to create buttons in Flutter ( Also documentation , and basic knowledge about widgets ):

import 'package:flutter/material.dart';

void main() {
  runApp(MyWidget());
}

//У этого виджета имеется состояние.
class MyWidget extends StatefulWidget {
  @override
  createState() => new MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  String text; // Наш текст для демонстрации функционала кнопки.

  @override
  initState() {
    super.initState();
    text = "test"; // Начальная инициализация кнопки.
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            children: [
              Text(text), // Наш текст для демонстрации функционала кнопки.

              // Flat кнопка.
              FlatButton(
                color: Colors.blue, // Цвет кнопки.
                textColor: Colors.white, // Цвет текста кнопки.
                child: Text("Flat Button"), // Текст кнопки.

                // При нажатие меняем текст (Функционал кнопки).
                onPressed: () => {
                  // При помощи установки нового состояния.
                  setState(() {
                    text = "Flat Button";
                  })
                },
              ),

              // Raised кнопка.
              RaisedButton(
                child: Text('Raised Button'),
                onPressed: () => {
                  setState(() {
                    text = "Raised Button";
                  })
                },
              ),

              // Icon кнопка.
              IconButton(
                icon: Icon(Icons.android),
                color: Colors.blue,
                onPressed: () => {
                  setState(() {
                    text = "Icon Button";
                  })
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question