Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d9e66711e4 | |||
| 88f0b8d4d1 |
@@ -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());
|
||||
@@ -17,7 +19,7 @@ class MyApp extends StatelessWidget {
|
||||
title: 'Namer App',
|
||||
theme: ThemeData(
|
||||
useMaterial3: true,
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
|
||||
),
|
||||
home: MyHomePage(),
|
||||
),
|
||||
@@ -25,51 +27,36 @@ class MyApp extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class MyAppState extends ChangeNotifier {
|
||||
var current = WordPair.random();
|
||||
|
||||
void getNext() {
|
||||
current = WordPair.random();
|
||||
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, style: style),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatelessWidget {
|
||||
class GeneratorPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var appState = context.watch<MyAppState>();
|
||||
var pair = appState.current;
|
||||
|
||||
return Scaffold(
|
||||
body: Column(
|
||||
IconData icon;
|
||||
if (appState.favourites.contains(pair)) {
|
||||
icon = Icons.favorite;
|
||||
} else {
|
||||
icon = Icons.favorite_border;
|
||||
}
|
||||
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text('A random idea:'),
|
||||
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();
|
||||
@@ -78,6 +65,10 @@ class MyHomePage extends StatelessWidget {
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
23
lib/states/my_app_states.dart
Normal file
23
lib/states/my_app_states.dart
Normal file
@@ -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 = <WordPair>[];
|
||||
|
||||
void getNext() {
|
||||
current = WordPair.random();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void toggleFavourites() {
|
||||
if (favourites.contains(current)) {
|
||||
favourites.remove(current);
|
||||
} else {
|
||||
favourites.add(current);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
30
lib/widgets/big_card.dart
Normal file
30
lib/widgets/big_card.dart
Normal file
@@ -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),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
50
lib/widgets/generator.dart
Normal file
50
lib/widgets/generator.dart
Normal file
@@ -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<MyAppState>();
|
||||
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'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
41
lib/widgets/my_home_page.dart
Normal file
41
lib/widgets/my_home_page.dart
Normal file
@@ -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(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user