name27 / flutter

0 stars 0 forks source link

json 네트워크 데이터 사용 #87

Open name27 opened 1 year ago

name27 commented 1 year ago

image

main.dart

import 'package:class_practice_app/model/User.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var dio = Dio();
  Future<GetData> getData() async {
    var res = await dio.get('https://sniperfactory.com/sfac/http_json_data');
    Map<String, dynamic> networkData = res.data["item"];
    if (res.statusCode == 200) {
      return GetData.fromMap(networkData);
    }
    return GetData(name: '', description: '', image: '', price: 0);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
        ),
        body: Center(
            child: FutureBuilder(
          future: getData(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return Card(
                child: SizedBox(
                  width: 250,
                  height: 400,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Expanded(
                          child: Image.network(
                        snapshot.data!.image,
                        fit: BoxFit.cover,
                      )),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          snapshot.data!.name,
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                      ),
                      SizedBox(
                        height: 8,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(snapshot.data!.description),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: ElevatedButton(
                          onPressed: () {
                            setState(() {});
                          },
                          child: Text('${snapshot.data!.price}원 결제하고 등록'),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            }
            return CircularProgressIndicator();
          },
        )),
      ),
    );
  }
}
class GetData {
  String name;
  String description;
  String image;
  int price;

  GetData(
      {required this.name,
      required this.description,
      required this.image,
      required this.price});

  GetData.fromMap(Map<String, dynamic> map)
      : name = map["name"],
        description = map["description"],
        image = map["image"],
        price = map["price"];
}