mirror of
https://github.com/flutter/samples.git
synced 2026-04-01 01:03:30 +00:00
Testing sample (#500)
This commit is contained in:
34
testing_app/lib/main.dart
Normal file
34
testing_app/lib/main.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:testing_app/models/favorites.dart';
|
||||
import 'package:testing_app/screens/favorites.dart';
|
||||
import 'package:testing_app/screens/home.dart';
|
||||
|
||||
void main() {
|
||||
runApp(TestingApp());
|
||||
}
|
||||
|
||||
class TestingApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ChangeNotifierProvider<Favorites>(
|
||||
create: (context) => Favorites(),
|
||||
child: MaterialApp(
|
||||
title: 'Testing Sample',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
visualDensity: VisualDensity.adaptivePlatformDensity,
|
||||
),
|
||||
routes: {
|
||||
HomePage.routeName: (context) => HomePage(),
|
||||
FavoritesPage.routeName: (context) => FavoritesPage(),
|
||||
},
|
||||
initialRoute: HomePage.routeName,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
22
testing_app/lib/models/favorites.dart
Normal file
22
testing_app/lib/models/favorites.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// The [Favorites] class holds a list of favorite items saved by the user.
|
||||
class Favorites extends ChangeNotifier {
|
||||
final List<int> _favoriteItems = [];
|
||||
|
||||
List<int> get items => _favoriteItems;
|
||||
|
||||
void add(int itemNo) {
|
||||
_favoriteItems.add(itemNo);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void remove(int itemNo) {
|
||||
_favoriteItems.remove(itemNo);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
64
testing_app/lib/screens/favorites.dart
Normal file
64
testing_app/lib/screens/favorites.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:testing_app/models/favorites.dart';
|
||||
|
||||
class FavoritesPage extends StatelessWidget {
|
||||
static String routeName = '/favorites_page';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Favorites'),
|
||||
),
|
||||
body: Consumer<Favorites>(
|
||||
builder: (context, value, child) => ListView.builder(
|
||||
itemCount: value.items.length,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
itemBuilder: (context, index) => FavoriteItemTile(value.items[index]),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class FavoriteItemTile extends StatelessWidget {
|
||||
final int itemNo;
|
||||
|
||||
const FavoriteItemTile(
|
||||
this.itemNo,
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: Colors.primaries[itemNo % Colors.primaries.length],
|
||||
),
|
||||
title: Text(
|
||||
'Item $itemNo',
|
||||
key: Key('favorites_text_$itemNo'),
|
||||
),
|
||||
trailing: IconButton(
|
||||
key: Key('remove_icon_$itemNo'),
|
||||
icon: Icon(Icons.close),
|
||||
onPressed: () {
|
||||
Provider.of<Favorites>(context, listen: false).remove(itemNo);
|
||||
Scaffold.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Removed from favorites.'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
82
testing_app/lib/screens/home.dart
Normal file
82
testing_app/lib/screens/home.dart
Normal file
@@ -0,0 +1,82 @@
|
||||
// Copyright 2020 The Flutter team. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:testing_app/models/favorites.dart';
|
||||
import 'package:testing_app/screens/favorites.dart';
|
||||
|
||||
class HomePage extends StatelessWidget {
|
||||
static String routeName = '/';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Testing Sample'),
|
||||
actions: <Widget>[
|
||||
FlatButton.icon(
|
||||
textColor: Colors.white,
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, FavoritesPage.routeName);
|
||||
},
|
||||
icon: Icon(Icons.favorite_border),
|
||||
label: Text('Favorites'),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: ListView.builder(
|
||||
itemCount: 100,
|
||||
cacheExtent: 20.0,
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
itemBuilder: (context, index) => ItemTile(index),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ItemTile extends StatelessWidget {
|
||||
final int itemNo;
|
||||
|
||||
const ItemTile(
|
||||
this.itemNo,
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var favoritesList = Provider.of<Favorites>(context);
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: Colors.primaries[itemNo % Colors.primaries.length],
|
||||
),
|
||||
title: Text(
|
||||
'Item $itemNo',
|
||||
key: Key('text_$itemNo'),
|
||||
),
|
||||
trailing: IconButton(
|
||||
key: Key('icon_$itemNo'),
|
||||
icon: favoritesList.items.contains(itemNo)
|
||||
? Icon(Icons.favorite)
|
||||
: Icon(Icons.favorite_border),
|
||||
onPressed: () {
|
||||
!favoritesList.items.contains(itemNo)
|
||||
? favoritesList.add(itemNo)
|
||||
: favoritesList.remove(itemNo);
|
||||
Scaffold.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(favoritesList.items.contains(itemNo)
|
||||
? 'Added to favorites.'
|
||||
: 'Removed from favorites.'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user