Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() {
- runApp(const MyApp());
- }
- class MyApp extends StatelessWidget {
- const MyApp({Key? key}) : super(key: key);
- // This widget is the root of your application.
- @override
- Widget build(BuildContext context) {
- return const MaterialApp(
- debugShowCheckedModeBanner: false,
- home: CupertinoBottomNav(),
- );
- }
- }
- // My Bottom Navigation Bar:
- class CupertinoBottomNav extends StatelessWidget {
- const CupertinoBottomNav({Key? key}) : super(key: key);
- @override
- Widget build(BuildContext context) {
- return CupertinoTabScaffold(
- tabBar: CupertinoTabBar(
- items: const [
- BottomNavigationBarItem(
- icon: Icon(
- Icons.home_filled,
- size: 28,
- ),
- label: 'Home'),
- BottomNavigationBarItem(
- icon: Icon(
- Icons.storage_rounded,
- size: 28,
- ),
- label: 'Favorites'),
- ],
- ),
- tabBuilder: (context, index) {
- switch (index) {
- case 0:
- return CupertinoTabView(builder: (context) => const ListPage());
- case 1:
- default:
- return CupertinoTabView(builder: (context) => const Favorites());
- }
- },
- );
- }
- }
- // This is my model class of the items which get stored as favorites
- class TestItems {
- String name;
- int id;
- TestItems({required this.name, required this.id});
- factory TestItems.fromJson(Map<String, dynamic> json) {
- return TestItems(
- name: json['name'] as String,
- id: json['id'] as int,
- );
- }
- Map<String, dynamic> toMap() {
- return {
- 'id': id,
- 'name': name,
- };
- }
- }
- // This is the page where the stored favorite items are displayed.
- //You will see that it only refreshes the page when I restart the app.
- //I think this is caused by the CupertinoBottomNavigationBar as it works properly with the Material on.
- // But I would like to keep the CupertinoBar as I want it to be shown on every page
- // Thank you so much for you support!!!
- class Favorites extends StatefulWidget {
- const Favorites({Key? key}) : super(key: key);
- @override
- _FavoritesState createState() => _FavoritesState();
- }
- class _FavoritesState extends State<Favorites> {
- // ignore: prefer_typing_uninitialized_variables
- var database;
- List<TestItems> items = <TestItems>[];
- Future initDb() async {
- database = await openDatabase(
- join(await getDatabasesPath(), 'person_database.db'),
- onCreate: (db, version) {
- return db.execute(
- "CREATE TABLE person(id INTEGER PRIMARY KEY, name TEXT)",
- );
- },
- version: 1,
- );
- getMechanism().then((value) {
- setState(() {
- items = value;
- });
- });
- }
- Future<List<TestItems>> getMechanism() async {
- final Database db = await database;
- final List<Map<String, dynamic>> maps = await db.query('person');
- return List.generate(maps.length, (i) {
- return TestItems(
- id: maps[i]['id'],
- name: maps[i]['name'] as String,
- );
- });
- }
- Future<void> deleteDB(int id) async {
- final db = await database;
- await db.delete(
- 'person',
- where: "id = ?",
- whereArgs: [id],
- );
- }
- // Future<void> updateDB(Mechanism person) async {
- // final db = await database;
- // await db.update(
- // 'person',
- // where: "id = ?",
- // whereArgs: [person.id],
- // );
- // }
- @override
- void initState() {
- super.initState();
- initDb();
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text("Favorites"),
- ),
- body: ListView.builder(
- itemCount: items.length,
- itemBuilder: (context, index) {
- var item = items[index];
- return ListTile(
- title: Text(
- item.name,
- ),
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => ItemDtl(items: item, id: index)),
- );
- },
- trailing: IconButton(
- color: Colors.red,
- icon: const Icon(Icons.delete_forever_rounded),
- onPressed: () {
- deleteDB(item.id).then((value) {
- getMechanism().then((value) {
- setState(() {
- items = value;
- });
- });
- });
- },
- ),
- );
- }),
- );
- }
- }
- // This is the Detailpage of the items
- class ItemDtl extends StatefulWidget {
- const ItemDtl({Key? key, required this.items, required this.id})
- : super(key: key);
- final TestItems items;
- final int id;
- @override
- _ItemDtlState createState() => _ItemDtlState();
- }
- class _ItemDtlState extends State<ItemDtl> {
- // ignore: prefer_typing_uninitialized_variables
- var database;
- @override
- void initState() {
- super.initState();
- initDb();
- }
- Future initDb() async {
- database = openDatabase(
- join(await getDatabasesPath(), 'person_database.db'),
- onCreate: (db, version) {
- return db.execute(
- "CREATE TABLE person(id INTEGER PRIMARY KEY, name TEXT, height TEXT, mass TEXT, hair_color TEXT, skin_color TEXT, eye_color TEXT, birth_year TEXT, gender TEXT)",
- );
- },
- version: 1,
- );
- }
- Future<void> insertEntry(TestItems items) async {
- final Database db = await database;
- await db.insert(
- 'person',
- items.toMap(),
- conflictAlgorithm: ConflictAlgorithm.replace,
- );
- }
- Future<void> updateDB(TestItems items) async {
- final db = await database;
- await db.update(
- 'person',
- items.toMap(),
- where: "id = ?",
- whereArgs: [items.id],
- );
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- centerTitle: true,
- title: Text(widget.items.name),
- ),
- body: Padding(
- padding: const EdgeInsets.all(8.0),
- child: ListView(children: <Widget>[
- TextButton.icon(
- icon: const Icon(Icons.storage),
- label: const Text(
- "Add To Favorites",
- ),
- onPressed: () {
- insertEntry(widget.items);
- },
- ),
- Text(
- "Name: ${widget.items.name}",
- ),
- ]),
- ),
- );
- }
- }
- // This is the page where user is albe to store individuall list items as favorites.
- class ListPage extends StatefulWidget {
- const ListPage({Key? key}) : super(key: key);
- @override
- _ListPageState createState() => _ListPageState();
- }
- class _ListPageState extends State<ListPage> {
- TextEditingController searchController = TextEditingController();
- List<TestItems> shownList = <TestItems>[
- TestItems(name: 'Test', id: 1),
- TestItems(name: 'Test2', id: 2),
- TestItems(name: 'Test3', id: 3)
- ];
- List<TestItems> initialData = <TestItems>[
- TestItems(name: 'Test', id: 1),
- TestItems(name: 'Test2', id: 2),
- TestItems(name: 'Test3', id: 3)
- ];
- void queryPeople(String queryString) {
- if (kDebugMode) {
- print("queryString = $queryString");
- }
- setState(() {
- shownList = initialData.where((string) {
- if (string.name.toLowerCase().contains(queryString.toLowerCase())) {
- return true;
- } else {
- return false;
- }
- }).toList();
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text('Detail'),
- ),
- body: Column(
- children: <Widget>[
- TextButton.icon(
- label: const Text('Favorites'),
- icon: const Icon(
- Icons.storage,
- ),
- onPressed: () {
- Navigator.push(
- context,
- MaterialPageRoute(builder: (context) => const Favorites()),
- );
- },
- ),
- Expanded(
- child: ItemList(
- item: shownList,
- ),
- ),
- ],
- ),
- );
- }
- }
- class ItemList extends StatelessWidget {
- final List<TestItems> item;
- const ItemList({Key? key, required this.item}) : super(key: key);
- @override
- Widget build(BuildContext context) {
- return ListView.builder(
- itemCount: item.length,
- itemBuilder: (context, index) {
- var items = item[index];
- var name = items.name;
- return ListTile(
- title: Text(
- name,
- ),
- onTap: () {
- items.id = index;
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => ItemDtl(
- id: index,
- items: items,
- )),
- );
- },
- );
- },
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement