size_recommender 0.0.1 copy "size_recommender: ^0.0.1" to clipboard
size_recommender: ^0.0.1 copied to clipboard

Flutter plugin for size recommendations based on BMI.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:size_recommender/size_recommender.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Size Recommender',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const InputScreen(),
    );
  }
}

class InputScreen extends StatefulWidget {
  const InputScreen({super.key});

  @override
  State<InputScreen> createState() => _InputScreenState();
}

class _InputScreenState extends State<InputScreen> {
  final TextEditingController heightController = TextEditingController();
  final TextEditingController weightController = TextEditingController();
  final _sizeRecommenderPlugin = SizeRecommender();

  Future<void> getSizeRecommendation() async {
    try {
      final heightCm = double.parse(heightController.text);
      final weightKg = double.parse(weightController.text);
      final recommendedSize =
          await _sizeRecommenderPlugin.calculateSizeRecommendation(
        heightCm: heightCm,
        weightKg: weightKg,
      );

      // Check if widget is still mounted before navigating
      if (mounted) {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) =>
                ResultScreen(recommendedSize: recommendedSize ?? 'Unknown'),
          ),
        );
      }
    } catch (e) {
      // Handle invalid input or plugin error
      if (mounted) {
        showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: const Text("Input Error"),
            content: Text("Error: $e"),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: const Text("OK"),
              ),
            ],
          ),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Find Your Perfect Fit")),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextField(
              controller: heightController,
              keyboardType: TextInputType.number,
              decoration: const InputDecoration(
                labelText: "Height (cm)",
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 16),
            TextField(
              controller: weightController,
              keyboardType: TextInputType.number,
              decoration: const InputDecoration(
                labelText: "Weight (kg)",
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 32),
            ElevatedButton(
              onPressed: getSizeRecommendation,
              child: const Text("Get Size Recommendation"),
            ),
          ],
        ),
      ),
    );
  }
}

class ResultScreen extends StatelessWidget {
  final String recommendedSize;

  const ResultScreen({super.key, required this.recommendedSize});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Your Recommended Size")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "Your Recommended Size: $recommendedSize",
              style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 16),
            Text(
              "Based on your info, size $recommendedSize is recommended.",
              textAlign: TextAlign.center,
              style: const TextStyle(fontSize: 16),
            ),
            const SizedBox(height: 32),
            ElevatedButton(
              onPressed: () => Navigator.pop(context),
              child: const Text("OK"),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
150
points
16
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter plugin for size recommendations based on BMI.

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on size_recommender