From d9e66711e427f9826b1d07262c7f8413bfe56884 Mon Sep 17 00:00:00 2001 From: Jali Date: Sat, 3 May 2025 00:25:11 +0200 Subject: [PATCH] Split into multiple files --- lib/main.dart | 88 ++--------------------------------- lib/states/my_app_states.dart | 23 +++++++++ lib/widgets/big_card.dart | 30 ++++++++++++ lib/widgets/generator.dart | 50 ++++++++++++++++++++ lib/widgets/my_home_page.dart | 41 ++++++++++++++++ 5 files changed, 147 insertions(+), 85 deletions(-) create mode 100644 lib/states/my_app_states.dart create mode 100644 lib/widgets/big_card.dart create mode 100644 lib/widgets/generator.dart create mode 100644 lib/widgets/my_home_page.dart diff --git a/lib/main.dart b/lib/main.dart index ee2dc3d..54f6738 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,8 @@ -import 'package:english_words/english_words.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'states/my_app_states.dart'; +import 'widgets/big_card.dart'; +import 'widgets/my_home_page.dart'; void main() { runApp(MyApp()); @@ -25,90 +27,6 @@ class MyApp extends StatelessWidget { } } -class MyAppState extends ChangeNotifier { - var current = WordPair.random(); - var favourites = []; - - void getNext() { - current = WordPair.random(); - notifyListeners(); - } - - void toggleFavourites() { - if (favourites.contains(current)) { - favourites.remove(current); - } else { - favourites.add(current); - } - notifyListeners(); - } -} - -class BigCard extends StatelessWidget { - const BigCard({ - super.key, - required this.pair, - }); - - final WordPair pair; - - @override - Widget build(BuildContext context) { - final theme = Theme.of(context); - final style = theme.textTheme.displayMedium!.copyWith( - color: theme.colorScheme.onPrimary, - ); - - return Card( - color: theme.colorScheme.primary, - child: Padding( - padding: const EdgeInsets.all(20.0), - child: Text(pair.asLowerCase, - semanticsLabel: "${pair.first} ${pair.second}", style: style), - ), - ); - } -} - -// ... - -class MyHomePage extends StatelessWidget { - @override - Widget build(BuildContext context) { - return Scaffold( - body: Row( - children: [ - SafeArea( - child: NavigationRail( - extended: true, - destinations: [ - NavigationRailDestination( - icon: Icon(Icons.home), - label: Text('Home'), - ), - NavigationRailDestination( - icon: Icon(Icons.favorite), - label: Text('Favorites'), - ), - ], - selectedIndex: 0, - onDestinationSelected: (value) { - print('selected: $value'); - }, - ), - ), - Expanded( - child: Container( - color: Theme.of(context).colorScheme.primaryContainer, - child: GeneratorPage(), - ), - ), - ], - ), - ); - } -} - class GeneratorPage extends StatelessWidget { @override Widget build(BuildContext context) { diff --git a/lib/states/my_app_states.dart b/lib/states/my_app_states.dart new file mode 100644 index 0000000..a7c9074 --- /dev/null +++ b/lib/states/my_app_states.dart @@ -0,0 +1,23 @@ +library; + +import 'package:english_words/english_words.dart'; +import 'package:flutter/material.dart'; + +class MyAppState extends ChangeNotifier { + var current = WordPair.random(); + var favourites = []; + + void getNext() { + current = WordPair.random(); + notifyListeners(); + } + + void toggleFavourites() { + if (favourites.contains(current)) { + favourites.remove(current); + } else { + favourites.add(current); + } + notifyListeners(); + } +} diff --git a/lib/widgets/big_card.dart b/lib/widgets/big_card.dart new file mode 100644 index 0000000..711dee1 --- /dev/null +++ b/lib/widgets/big_card.dart @@ -0,0 +1,30 @@ +library; + +import 'package:english_words/english_words.dart'; +import 'package:flutter/material.dart'; + +class BigCard extends StatelessWidget { + const BigCard({ + super.key, + required this.pair, + }); + + final WordPair pair; + + @override + Widget build(BuildContext context) { + final theme = Theme.of(context); + final style = theme.textTheme.displayMedium!.copyWith( + color: theme.colorScheme.onPrimary, + ); + + return Card( + color: theme.colorScheme.primary, + child: Padding( + padding: const EdgeInsets.all(20.0), + child: Text(pair.asLowerCase, + semanticsLabel: "${pair.first} ${pair.second}", style: style), + ), + ); + } +} diff --git a/lib/widgets/generator.dart b/lib/widgets/generator.dart new file mode 100644 index 0000000..1f98869 --- /dev/null +++ b/lib/widgets/generator.dart @@ -0,0 +1,50 @@ +library; + +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import '../states/my_app_states.dart'; +import 'big_card.dart'; + +class GeneratorPage extends StatelessWidget { + @override + Widget build(BuildContext context) { + var appState = context.watch(); + var pair = appState.current; + + IconData icon; + if (appState.favourites.contains(pair)) { + icon = Icons.favorite; + } else { + icon = Icons.favorite_border; + } + + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + BigCard(pair: pair), + SizedBox(height: 10), + Row( + mainAxisSize: MainAxisSize.min, + children: [ + ElevatedButton.icon( + onPressed: () { + appState.toggleFavourites(); + }, + icon: Icon(icon), + label: Text('Like'), + ), + SizedBox(width: 10), + ElevatedButton( + onPressed: () { + appState.getNext(); + }, + child: Text('Next'), + ), + ], + ), + ], + ), + ); + } +} diff --git a/lib/widgets/my_home_page.dart b/lib/widgets/my_home_page.dart new file mode 100644 index 0000000..d26f0e4 --- /dev/null +++ b/lib/widgets/my_home_page.dart @@ -0,0 +1,41 @@ +library; + +import 'package:flutter/material.dart'; +import 'generator.dart'; + +class MyHomePage extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + body: Row( + children: [ + SafeArea( + child: NavigationRail( + extended: true, + destinations: [ + NavigationRailDestination( + icon: Icon(Icons.home), + label: Text('Home'), + ), + NavigationRailDestination( + icon: Icon(Icons.favorite), + label: Text('Favorites'), + ), + ], + selectedIndex: 0, + onDestinationSelected: (value) { + print('selected: $value'); + }, + ), + ), + Expanded( + child: Container( + color: Theme.of(context).colorScheme.primaryContainer, + child: GeneratorPage(), + ), + ), + ], + ), + ); + } +}