class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { /// MaterialApp is the base Widget for your Flutter Application /// Gives us access to routing, context, and meta info functionality. return MaterialApp( title: 'We Rate Dogs', // Make all our text default to white // and backgrounds default to dark theme: ThemeData(brightness: Brightness.dark), home: MyHomePage(title: 'We Rate Dogs'), ); } }
class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { /// Scaffold is the base for a page. /// It gives an AppBar for the top, /// Space for the main body, bottom navigation, and more. return Scaffold( /// App bar has a ton of functionality, but for now lets /// just give it a color and a title. appBar: AppBar( /// Access this widgets properties with 'widget' title: Text(widget.title), backgroundColor: Colors.black87, ), /// Container is a convenience widget that lets us style it's /// children. It doesn't take up any space itself, so it /// can be used as a placeholder in your code. body: Container(), ); } }
2. Dog Model 클래스 생성.
data model을 위해 Dog이라는 일반 Dart클래스를 만든다.
첫번째로 dog_model.dart라는 파일을 lib 폴더안에 생성한다.
1 2 3
- lib -dog_model.dart -main.dart
저 파일에서 몇 가지 속성을 가진 수퍼 기본 클래스 (super basic class)를 만들 것이다.
1 2 3 4 5 6 7 8 9 10 11
class Dog { final String name; final String location; final String description; String imageUrl;
// 모든 강아지는 10점부터 출발한다. 왜냐면 다 좋은 강아지니깐~ int rating = 10;
Future getImageUrl() async { // Null check so our app isn't doing extra work. // If there's already an image, we don't need to get one. if (imageUrl != null) { return; }
// This is how http calls are done in flutter: HttpClient http = HttpClient(); try { // Use darts Uri builder var uri = Uri.http('dog.ceo', '/api/breeds/image/random'); var request = await http.getUrl(uri); var response = await request.close(); var responseBody = await response.transform(utf8.decoder).join(); // The dog.ceo API returns a JSON object with a property // called 'message', which actually is the URL. imageUrl = json.decode(responseBody)['message']; } catch (exception) { print(exception); } }
class _MyHomePageState extends State<MyHomePage> { List<Dog> initialDoggos = [] ..add(Dog('Ruby', 'Portland, OR, USA', 'Ruby is a very good girl. Yes: Fetch, loungin\'. No: Dogs who get on furniture.')) ..add(Dog('Rex', 'Seattle, WA, USA', 'Best in Show 1999')) ..add(Dog('Rod Stewart', 'Prague, CZ', 'Star good boy on international snooze team.')) ..add(Dog('Herbert', 'Dallas, TX, USA', 'A Very Good Boy')) ..add(Dog('Buddy', 'North Pole, Earth', 'Self proclaimed human lover.')); }