Codice Dart su Flutter per la visualizzazione di un pulsante:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Esempio Pulsante',
home: const ContatorePage(),
);
}
}
class ContatorePage extends StatefulWidget {
const ContatorePage({super.key});
@override
State<ContatorePage> createState() => _ContatorePageState();
}
class _ContatorePageState extends State<ContatorePage> {
int _contatore = 0;
void _incrementa() {
setState(() {
_contatore++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Pulsante con risultato a schermo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Hai premuto il pulsante $_contatore volte',
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _incrementa,
child: const Text("Premi qui"),
),
],
),
),
);
}
}
L’esito è il seguente:
