This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add services and todo feature (#16)
- Loading branch information
1 parent
be9196a
commit fee8774
Showing
33 changed files
with
753 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:flutter_starter/features/counter/counter_page.dart'; | ||
import 'package:flutter_starter/features/todos/todos_page.dart'; | ||
import 'package:flutter_starter/l10n/app_localizations.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
|
||
class _TabItem { | ||
final String label; | ||
final Icon icon; | ||
final Widget screen; | ||
|
||
_TabItem(this.label, this.icon, this.screen); | ||
} | ||
|
||
class AppTabs extends HookConsumerWidget { | ||
const AppTabs({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
final selectedIndex = useState(0); | ||
|
||
final tabs = [ | ||
_TabItem( | ||
Loc.of(context).counter, | ||
const Icon(Icons.numbers), | ||
const CounterPage(), | ||
), | ||
_TabItem( | ||
Loc.of(context).todos, | ||
const Icon(Icons.check), | ||
const TodosPage(), | ||
), | ||
]; | ||
|
||
return Scaffold( | ||
body: IndexedStack( | ||
index: selectedIndex.value, | ||
children: tabs.map((tab) => tab.screen).toList(), | ||
), | ||
bottomNavigationBar: BottomNavigationBar( | ||
fixedColor: Theme.of(context).colorScheme.primary, | ||
selectedFontSize: 12, | ||
currentIndex: selectedIndex.value, | ||
onTap: (index) => selectedIndex.value = index, | ||
items: tabs | ||
.map( | ||
(tab) => BottomNavigationBarItem( | ||
icon: tab.icon, | ||
label: tab.label, | ||
), | ||
) | ||
.toList(), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:flutter_starter/l10n/app_localizations.dart'; | ||
|
||
class CounterPage extends HookWidget { | ||
const CounterPage({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final counter = useState(0); | ||
|
||
return Scaffold( | ||
appBar: AppBar(title: Text(Loc.of(context).appName)), | ||
body: Center( | ||
child: Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
Text( | ||
Loc.of(context).youHavePushedTheButton, | ||
textAlign: TextAlign.center, | ||
), | ||
Text( | ||
'${counter.value}', | ||
style: Theme.of(context).textTheme.headlineMedium, | ||
), | ||
], | ||
), | ||
), | ||
floatingActionButton: FloatingActionButton( | ||
heroTag: 'counter', | ||
onPressed: () => counter.value++, | ||
tooltip: Loc.of(context).increment, | ||
child: const Icon(Icons.add), | ||
), | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
import 'package:flutter_starter/features/todos/todos_notifier.dart'; | ||
import 'package:flutter_starter/l10n/app_localizations.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:models/models.dart'; | ||
|
||
class TodoFormPage extends HookConsumerWidget { | ||
final Todo? todo; | ||
final bool isNew; | ||
|
||
const TodoFormPage({this.todo, super.key}) : isNew = todo == null; | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
final title = useState(todo?.title); | ||
final description = useState(todo?.description); | ||
|
||
return Scaffold( | ||
appBar: AppBar(title: Text(isNew ? 'New Todo' : 'Edit Todo')), | ||
body: Form( | ||
child: Padding( | ||
padding: const EdgeInsets.all(16), | ||
child: Stack( | ||
children: [ | ||
SingleChildScrollView( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.max, | ||
children: [ | ||
TextFormField( | ||
autofocus: true, | ||
initialValue: title.value, | ||
textCapitalization: TextCapitalization.sentences, | ||
textInputAction: TextInputAction.next, | ||
onChanged: (value) => title.value = value, | ||
decoration: InputDecoration( | ||
labelText: Loc.of(context).title, | ||
), | ||
), | ||
TextFormField( | ||
initialValue: description.value, | ||
textCapitalization: TextCapitalization.sentences, | ||
textInputAction: TextInputAction.done, | ||
onChanged: (value) => description.value = value, | ||
decoration: InputDecoration( | ||
labelText: Loc.of(context).description, | ||
), | ||
maxLines: null, | ||
), | ||
], | ||
), | ||
), | ||
Align( | ||
alignment: Alignment.bottomCenter, | ||
child: SizedBox( | ||
width: double.infinity, | ||
child: FilledButton( | ||
child: Text(Loc.of(context).save), | ||
onPressed: () => | ||
_save(context, ref, title.value, description.value), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
void _save( | ||
BuildContext context, WidgetRef ref, String? title, String? description) { | ||
if (title == null || title.isEmpty) { | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
SnackBar(content: Text(Loc.of(context).titleRequired)), | ||
); | ||
return; | ||
} | ||
|
||
if (isNew) { | ||
ref.read(todosProvider.notifier).add( | ||
Todo( | ||
title: title, | ||
description: description, | ||
), | ||
); | ||
} else { | ||
ref.read(todosProvider.notifier).update( | ||
todo!.copyWith( | ||
title: title, | ||
description: description, | ||
), | ||
); | ||
} | ||
|
||
Navigator.of(context).pop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:models/models.dart'; | ||
import 'package:services/services.dart'; | ||
|
||
final todosProvider = | ||
NotifierProvider<TodosNotifier, List<Todo>>(TodosNotifier.new); | ||
|
||
class TodosNotifier extends Notifier<List<Todo>> { | ||
@override | ||
List<Todo> build() { | ||
return service.todos; | ||
} | ||
|
||
TodosService get service => ref.read(todosServiceProvider); | ||
List<Todo> get serviceTodos => List.from(service.todos); | ||
|
||
void add(Todo todo) { | ||
service.add(todo); | ||
state = serviceTodos; | ||
} | ||
|
||
void update(Todo todo) { | ||
service.update(todo); | ||
state = serviceTodos; | ||
} | ||
|
||
void remove(Todo todo) { | ||
service.remove(todo); | ||
state = serviceTodos; | ||
} | ||
} |
Oops, something went wrong.