Skip to content

Commit

Permalink
Refatorar HttpClient, adicionar componente Expert e melhorar tratamen…
Browse files Browse the repository at this point in the history
…to de erros na página de login
  • Loading branch information
Polabiel committed Nov 5, 2024
1 parent 1447c45 commit 20d3ea3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 24 deletions.
23 changes: 1 addition & 22 deletions lib/api/services/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
class HttpClient {
late Dio client;

HttpRequestHandler() {
HttpClient() {
client = Dio(BaseOptions(
baseUrl: dotenv.env['BASE_URL'] ?? 'http://10.0.2.2:3000',
headers: {
Expand All @@ -13,27 +13,6 @@ class HttpClient {
));
}

Future<Response> fetchWithRetry(String url,
{int retries = 3, int delay = 1000}) async {
int attempt = 0;
while (attempt < retries) {
try {
final response = await Dio().get(url);
return response;
} on DioException catch (e) {
if (e.response?.statusCode == 429) {
attempt++;
if (attempt >= retries) {
rethrow;
}
await Future.delayed(Duration(milliseconds: delay * attempt));
} else {
rethrow;
}
}
}
throw Exception('Failed to fetch data after $retries attempts');
}
}


Expand Down
7 changes: 7 additions & 0 deletions lib/src/app/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class HomepageState extends State<Homepage> {
),
child: const BannerComponent(hasPromotion: true),
),
Divider(),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: screenHeight * 0.2, // 60% da altura da tela
),
child: const BannerComponent(hasPromotion: true),
),
],
),
),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/app/login/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ class LoginPageState extends State<LoginPage> {
.then((value) => value)
.catchError((error) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Erro ao fazer login'),
SnackBar(
content: Text(error.toString()),
backgroundColor: Colors.redAccent,
),
);
Expand Down
75 changes: 75 additions & 0 deletions lib/src/components/Expert.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:flutter/material.dart';
import 'package:gohealth/api/repositories/expert_doctor_repository.dart';
import 'package:http/http.dart';

class Expert extends StatefulWidget {
@override
_ExpertState createState() => _ExpertState();
}

class _ExpertState extends State<Expert> {
final TextEditingController _controller = TextEditingController();
late final expertDoctor;

Future<Response> texto = Future.value(Response('', 200));

@override
void initState() {
super.initState();
expertDoctor = expertDoctor();
}

void getSymptoms(String prompt) {
setState(() {
texto = expertDoctor.getSymptoms(prompt);
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: FutureBuilder<Response>(
future: texto,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Text('Erro: ${snapshot.error}');
} else {
if (!snapshot.hasData || (snapshot.data!.body as List).isEmpty) {
return const Center(child: Text('Nenhum produto encontrado.'));
} else {
return Text(snapshot.data.toString());
}
}
},
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
child: Column(
children: [
TextFormField(
controller: _controller,
decoration: InputDecoration(labelText: 'Digite o sintoma'),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () {
getSymptoms(_controller.text);
},
child: Text('Enviar'),
),
],
),
),
),
],
),
);
}
}

0 comments on commit 20d3ea3

Please sign in to comment.