Estou estudando e praticando Flutter. Porém o curso que acompanho não está com a versão mais recente do Flutter, o que fez eu me deparar com os seguintes erros:
The parameter 'onPress' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.
e
The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.
Pesquisei, mas não consegui resolver. Segue o código:
import 'package:flutter/material.dart';
void main() => runApp(MeuApp());
// ignore: use_key_in_widget_constructors
class MeuApp extends StatelessWidget {
btnAction() {
print("Clicou no botão");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("Meu primeiro App")),
body: Column(
children: [
Exemplo(
title: ('Texto 01'),
onPress: () {
print('Clicou no Item 01');
}),
Exemplo(
title: ('Texto 02'),
onPress: () {
print('Clicou no Item 02');
}),
],
)),
);
}
}
class Exemplo extends StatelessWidget {
Exemplo({
this.title,
this.onPress,
});
var title;
Function onPress;
@override
Widget build(BuildContext context) {
if (title == null) {
title = 'Titulo Reserva';
}
return Container(
width: 200,
margin: EdgeInsets.all(10),
padding: EdgeInsets.all(10),
color: Colors.cyan,
child: Column(
children: [
Text('Texto de Exemplo'),
TextButton(
child: Text(title),
onPressed: onPress,
)
],
));
}
}