added a page to view individual listings, switched from list to map

This commit is contained in:
Lynne Megido 2020-09-30 13:23:01 +10:00
parent 55e8c828a4
commit 395778a623
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
6 changed files with 55 additions and 17 deletions

View file

@ -17,6 +17,7 @@
import 'package:buyeeb_mobile/pages/main_page.dart'; import 'package:buyeeb_mobile/pages/main_page.dart';
import 'package:buyeeb_mobile/pages/splash.dart'; import 'package:buyeeb_mobile/pages/splash.dart';
import 'package:buyeeb_mobile/pages/view_listing.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'models/user_listings.dart'; import 'models/user_listings.dart';
@ -30,6 +31,7 @@ void main() {
routes: { routes: {
'/': (context) => RouteHome(), '/': (context) => RouteHome(),
'/splash': (context) => RouteSplash(), '/splash': (context) => RouteSplash(),
'/view_listing': (context) => RouteViewListing(),
}, },
title: "Buypeeb Mobile", title: "Buypeeb Mobile",

View file

@ -3,17 +3,18 @@ import 'package:buyeeb_mobile/models/yahoo_auctions_item.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
class UserListings extends ChangeNotifier { class UserListings extends ChangeNotifier {
final List<YahooAuctionsItem> _items = []; // final List<YahooAuctionsItem> _items = [];
final Map<String, YahooAuctionsItem> _items = {};
UnmodifiableListView<YahooAuctionsItem> get items => UnmodifiableListView(_items); UnmodifiableMapView<String, YahooAuctionsItem> get items => UnmodifiableMapView(_items);
void add (YahooAuctionsItem item) { void add (String key, YahooAuctionsItem item) {
_items.add(item); _items[key] = item;
notifyListeners(); notifyListeners();
} }
void remove(YahooAuctionsItem item) { void remove(String key) {
_items.remove(item); _items.remove(key);
notifyListeners(); notifyListeners();
} }

View file

@ -1,12 +1,10 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
class YahooAuctionsItem { class YahooAuctionsItem {
String id;
String name; String name;
int price; int price;
YahooAuctionsItem({ YahooAuctionsItem({
@required this.id,
@required this.name, @required this.name,
@required this.price @required this.price
}); });

View file

@ -52,13 +52,23 @@ class _MainPageState extends State<MainPage> {
padding: EdgeInsets.all(8.0), padding: EdgeInsets.all(8.0),
separatorBuilder: (context, i) => Divider(), separatorBuilder: (context, i) => Divider(),
itemCount: listings.items.length, itemCount: listings.items.length,
itemBuilder: (context, i) => ListTile( itemBuilder: (context, i) {
title: Text(listings.items[i].name), String key = listings.items.keys.elementAt(i);
return ListTile(
title: Text(listings.items[key].name),
subtitle: Text(sprintf( subtitle: Text(sprintf(
"¥%d (≈\$%2.2f)", "¥%d (≈\$%2.2f)",
[listings.items[i].price, listings.items[i].getLocalPrice()] [listings.items[key].price, listings.items[key].getLocalPrice()]
)), )),
), onTap: () {
Navigator.pushNamed(
context,
'/view_listing',
arguments: key
);
}
);
}
); );
} }
} }

View file

@ -18,8 +18,9 @@ class _RouteSplashState extends State<RouteSplash> {
var _rng = new Random(); var _rng = new Random();
final names = ["Hi Pempa!", "Pebulon Crystal", "The Peebler Chronicles", "Pecha Sludge", "The Forbidden Book of Peebus"]; final names = ["Hi Pempa!", "Pebulon Crystal", "The Peebler Chronicles", "Pecha Sludge", "The Forbidden Book of Peebus"];
for (var i = 0; i < 15; i++) { for (var i = 0; i < 15; i++) {
listings.add(YahooAuctionsItem( listings.add(
id: "Henlo", "k" + (_rng.nextInt(500000) + 100000).toString(),
YahooAuctionsItem(
name: names[_rng.nextInt(names.length)], name: names[_rng.nextInt(names.length)],
price: _rng.nextInt(5000) + 500 price: _rng.nextInt(5000) + 500
)); ));

View file

@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
class RouteViewListing extends StatefulWidget {
@override
_RouteViewListingState createState() => _RouteViewListingState();
}
class _RouteViewListingState extends State<RouteViewListing> {
@override
Widget build(BuildContext context) {
final String id = ModalRoute.of(context).settings.arguments;
return Scaffold(
appBar: AppBar(
title: Text("View listing"),
automaticallyImplyLeading: false, // remove the useless back button
),
body: ListView(
children: [
ListTile(
title: Text("ID: $id")
)
]
)
);
}
}