1
I have a function called showAlertDialogSimNao() and this receives the name parameter and content. I would like to take the return of this function and treat at the end of the application. However I’m not getting. The function call it happens however the function already gives the return without even the user have chosen the option "YES" or "NO" to terminate the application.
Follows the code:
main.dart
import 'package:flutter/material.dart';
import 'package:flutter_app01/principal.dart';
import 'biblioteca.dart';
void main() => runApp(MaterialApp(
theme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: GestaoComercial()));
class GestaoComercial extends StatefulWidget {
@override
_GestaoComercialState createState() => _GestaoComercialState();
}
class _GestaoComercialState extends State<GestaoComercial> {
TextEditingController _loginEditingController = TextEditingController(text: 'usuario');
TextEditingController _pwdEditingController = TextEditingController(text: 'senha');
bool _loading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Gestão Comercial"),
centerTitle: true,
),
body: Column(
children: <Widget>[
SizedBox(height: 20.0),
Text(
'Autenticação',
style: TextStyle(fontSize: 20),
),
SizedBox(height: 20.0),
TextFormField(
controller: _loginEditingController,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
labelText: "Identificação",
icon: Icon(Icons.person),
)),
TextFormField(
obscureText: true,
controller: _pwdEditingController,
decoration: InputDecoration(
labelText: "Senha", icon: Icon(Icons.lock))),
SizedBox(height: 20.0),
RaisedButton(
child: _loading
? linearProgressIndicator(context)
: Text("Autenticar"),
onPressed: () async {
setState(() {
_loading = true;
});
await Future.delayed(Duration(seconds: 1));
Navigator.push(context, SlideRightRoute(page: Principal()));
setState(() {
_loading = false;
});
},
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(20.0),
side: BorderSide(color: Colors.green)),
color: Colors.green,
textColor: Colors.white,
),
],
),
);
}
}
biblioteca.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Widget linearProgressIndicator(BuildContext context) {
return Container(
width: 45.0,
height: 45.0,
alignment: Alignment.center,
child: CircularProgressIndicator(
backgroundColor: Colors.green[900],
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
strokeWidth: 3.0));
}
class SlideRightRoute extends PageRouteBuilder {
final Widget page;
SlideRightRoute({this.page})
: super(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
page,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
SlideTransition(
position: Tween<Offset>(
begin: const Offset(-1, 0),
end: Offset.zero,
).animate(animation),
child: child,
),
);
}
bool showAlertDialogSimNao(BuildContext context, String titulo, String conteudo) {
// configura o button
Widget btnSim = FlatButton(
child: Text("Sim"),
onPressed: () {
Navigator.of(context).pop(true);
},
);
Widget btnNao = FlatButton(
child: Text("Não"),
onPressed: () {
Navigator.pop(context,false);
},
);
// configura o AlertDialog
AlertDialog alerta = AlertDialog(
title: Text("$titulo"),
content: Text("$conteudo"),
actions: [
btnSim,
btnNao
],
);
// exibe o dialog
showDialog(
barrierDismissible: false,
context: context,
builder: ( context ) => alerta);
}
principal.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'biblioteca.dart';
class Principal extends StatefulWidget {
@override
_PrincipalState createState() => _PrincipalState();
}
class _PrincipalState extends State<Principal> {
Future<bool> _onWillPop() async {
var resposta = showAlertDialogSimNao(
context, "Encerrar Aplicativo", "Deseja encerrar?");
print(resposta);
return resposta;
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green,
leading: Text("AÇÃO 1"),
centerTitle: true,
title: Text("APLICATIVO - MODELO"),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: GridView.count(
crossAxisCount: 2,
children: List.generate(choices.length, (index) {
return ChoiceCard(choice: choices[index]);
})),
)),
);
}
}
class ChoiceCard extends StatelessWidget {
const ChoiceCard({Key key, this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
final TextStyle textStyle = Theme.of(context).textTheme.display1;
return Card(
color: Colors.green[900],
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Icon(
choice.icon,
color: textStyle.color,
size: 50.0,
),
Text(
choice.title,
style: textStyle,
)
],
),
),
);
}
}
class Choice {
const Choice({this.title, this.icon});
final String title;
final IconData icon;
}
const List<Choice> choices = const <Choice>[
const Choice(title: 'Venda', icon: Icons.attach_money),
const Choice(title: 'Cliente', icon: Icons.people),
const Choice(title: 'Produto', icon: Icons.wallpaper),
const Choice(title: 'Venda', icon: Icons.monetization_on),
const Choice(title: 'Cliente', icon: Icons.people),
const Choice(title: 'Produto', icon: Icons.wallpaper),
const Choice(title: 'Venda', icon: Icons.monetization_on),
const Choice(title: 'Cliente', icon: Icons.people),
const Choice(title: 'Produto', icon: Icons.wallpaper),
const Choice(title: 'Venda', icon: Icons.monetization_on),
const Choice(title: 'Cliente', icon: Icons.people),
const Choice(title: 'Produto', icon: Icons.wallpaper),
];
Legal Matheus.. really the problem was related to Synchronization. Thanks. It worked.
– Fernando Casagrande
@Fernandocasagrande if solved if problem and you do not want to wait another answer, mark mine as accepted to facilitate for those who have similar problems! And good dev ai!
– Matheus Ribeiro