From a290f3739b6fc2e7700eb469a52ca187f69610c9 Mon Sep 17 00:00:00 2001 From: akmhmgc Date: Sat, 12 Aug 2023 23:41:26 +0900 Subject: [PATCH] =?UTF-8?q?tmp:=20=E6=8A=BC=E3=81=99=E3=81=A8=E9=96=8B?= =?UTF-8?q?=E3=81=8F=E3=83=9C=E3=82=BF=E3=83=B3=E3=81=AE=E3=82=B5=E3=83=B3?= =?UTF-8?q?=E3=83=97=E3=83=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/main.dart | 368 +++++++++++++++++++++++++++----------------------- sample.dart | 20 +++ 2 files changed, 218 insertions(+), 170 deletions(-) create mode 100644 sample.dart diff --git a/lib/main.dart b/lib/main.dart index 1740fd3..2366fac 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,207 +1,235 @@ -import 'package:english_words/english_words.dart'; import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; -void main() { - runApp(MyApp()); +class Product { + const Product({required this.name}); + + final String name; } -class MyApp extends StatelessWidget { - const MyApp({super.key}); +typedef CartChangedCallback = Function(Product product, bool inCart); + +class ShoppingListItem extends StatelessWidget { + ShoppingListItem({ + required this.product, + required this.inCart, + required this.onCartChanged, + required Key key, + }) : super( + key: key, + ); + + final Product product; + final bool inCart; + final CartChangedCallback onCartChanged; + + Color _getColor(BuildContext context) { + // The theme depends on the BuildContext because different + // parts of the tree can have different themes. + // The BuildContext indicates where the build is + // taking place and therefore which theme to use. + + return inCart // + ? Colors.black54 + : Theme.of(context).primaryColor; + } + + TextStyle? _getTextStyle(BuildContext context) { + if (!inCart) return null; + + return const TextStyle( + color: Colors.black54, + decoration: TextDecoration.lineThrough, + ); + } @override Widget build(BuildContext context) { - return ChangeNotifierProvider( - create: (context) => MyAppState(), - child: MaterialApp( - title: 'Namer App', - theme: ThemeData( - useMaterial3: true, - colorScheme: ColorScheme.fromSeed( - seedColor: Colors.deepOrange, - ), - ), - home: MyHomePage(), + return ListTile( + onTap: () { + onCartChanged(product, inCart); + }, + leading: CircleAvatar( + backgroundColor: _getColor(context), + child: Text(product.name[0]), + ), + title: Text( + product.name, + style: _getTextStyle(context), ), ); } } -class MyAppState extends ChangeNotifier { - var current = WordPair.random(); - - void getNext() { - current = WordPair.random(); - notifyListeners(); - } +class ShoppingList extends StatefulWidget { + const ShoppingList({required this.products, super.key}); - var favorites = []; + final List products; - void toggleFavorite() { - if (favorites.contains(current)) { - favorites.remove(current); - } else { - favorites.add(current); - } - notifyListeners(); - } -} + // The framework calls createState the first time + // a widget appears at a given location in the tree. + // If the parent rebuilds and uses the same type of + // widget (with the same key), the framework re-uses + // the State object instead of creating a new State object. -class MyHomePage extends StatefulWidget { @override - State createState() => _MyHomePageState(); + State createState() => _ShoppingListState(); } -class _MyHomePageState extends State { - var selectedIndex = 0; - @override - Widget build(BuildContext context) { - Widget page; - switch (selectedIndex) { - case 0: - page = GeneratorPage(); - break; - case 1: - page = FavoritesPage(); - break; - default: - throw UnimplementedError('no widget for $selectedIndex'); - } - - return LayoutBuilder(builder: (context, constraints) { - return Scaffold( - body: Row( - children: [ - SafeArea( - child: NavigationRail( - extended: constraints.maxWidth >= 600, - destinations: [ - NavigationRailDestination( - icon: Icon(Icons.home), - label: Text('Home'), - ), - NavigationRailDestination( - icon: Icon(Icons.favorite), - label: Text('Favorites'), - ), - ], - selectedIndex: selectedIndex, - onDestinationSelected: (value) { - setState(() { - selectedIndex = value; - }); - }, - ), - ), - Expanded( - child: Container( - color: Theme.of(context).colorScheme.primaryContainer, - child: page, - ), - ), - ], - ), - ); +class _ShoppingListState extends State { + final _shoppingCart = {}; + + void _handleCartChanged(Product product, bool inCart) { + setState(() { + // When a user changes what's in the cart, you need + // to change _shoppingCart inside a setState call to + // trigger a rebuild. + // The framework then calls build, below, + // which updates the visual appearance of the app. + print(_shoppingCart.runtimeType); + + if (!inCart) { + _shoppingCart.add(product); + } else { + _shoppingCart.remove(product); + } }); } -} -class GeneratorPage extends StatelessWidget { @override Widget build(BuildContext context) { - var appState = context.watch(); - var pair = appState.current; - - IconData icon; - if (appState.favorites.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.toggleFavorite(); - }, - icon: Icon(icon), - label: Text('Like'), - ), - SizedBox(width: 10), - ElevatedButton( - onPressed: () { - appState.getNext(); - }, - child: Text('Next'), - ), - ], - ), - ], + return Scaffold( + appBar: AppBar( + title: const Text('Shopping List'), + ), + body: ListView( + padding: const EdgeInsets.symmetric(vertical: 8), + children: widget.products.asMap().entries.map((entry) { + final product = entry.value; + final index = entry.key; + return ShoppingListItem( + key: Key('$index'), + product: product, + inCart: _shoppingCart.contains(product), + onCartChanged: _handleCartChanged, + ); + }).toList(), ), ); } } -class BigCard extends StatelessWidget { - const BigCard({ - super.key, - required this.pair, - }); +void main() { + runApp(MaterialApp(title: 'Shopping App', home: SamplePage())); +} - final WordPair pair; +class SamplePage extends StatefulWidget { + SamplePage({Key? key}) : super(key: key); @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), - child: Text( - pair.asLowerCase, - style: style, - semanticsLabel: "${pair.first} ${pair.second}", - ), - ), - ); - } + State createState() => _SamplePageState(); } -class FavoritesPage extends StatelessWidget { +class _SamplePageState extends State { + bool isVisibled1 = false; + bool isVisibled2 = false; + @override Widget build(BuildContext context) { - var appState = context.watch(); - - if (appState.favorites.isEmpty) { - return Center( - child: Text('No favorites yet.'), - ); - } - - return ListView( - children: [ - Padding( - padding: const EdgeInsets.all(20), - child: Text('You have ' - '${appState.favorites.length} favorites:'), - ), - for (var pair in appState.favorites) - ListTile( - leading: Icon(Icons.favorite), - title: Text(pair.asLowerCase), - ), - ], - ); + return Scaffold( + appBar: AppBar(title: const Text('Shopping App')), + body: Column( + children: [ + Stack( + clipBehavior: Clip.none, + alignment: Alignment.bottomCenter, + children: [ + SizedBox( + height: 100, + child: Container( + color: Colors.orange, + ), + ), + ], + ), + Visibility( + visible: isVisibled1, + child: Container( + height: 50, + decoration: BoxDecoration( + border: Border.all(color: Colors.black), + borderRadius: BorderRadius.circular(10), + ), + child: Center( + child: Text('Hello'), + ), + ), + ), + Stack( + clipBehavior: Clip.none, + alignment: Alignment.topCenter, + children: [ + SizedBox( + height: 100, + child: Container( + color: Colors.green, + ), + ), + Visibility( + child: Positioned( + top: -20, + child: TextButton( + style: ButtonStyle( + backgroundColor: + MaterialStateProperty.all(Colors.white), + ), + onPressed: () { + setState(() { + isVisibled1 = !isVisibled1; + }); + }, + child: const Text('Go'), + ), + ), + visible: !isVisibled1, + ) + ], + ), + Visibility( + visible: isVisibled1, + child: Container( + height: 50, + decoration: BoxDecoration( + border: Border.all(color: Colors.black), + borderRadius: BorderRadius.circular(10), + ), + child: Center( + child: Text('Hello'), + ), + ), + ), + Stack( + clipBehavior: Clip.none, + alignment: Alignment.topCenter, + children: [ + SizedBox( + height: 100, + child: Container( + color: Colors.grey, + ), + ), + Positioned( + top: -20, + child: TextButton( + style: ButtonStyle( + backgroundColor: MaterialStateProperty.all(Colors.white), + ), + onPressed: () {}, + child: const Text('Go'), + ), + ), + ], + ), + ], + )); } } diff --git a/sample.dart b/sample.dart new file mode 100644 index 0000000..78799e3 --- /dev/null +++ b/sample.dart @@ -0,0 +1,20 @@ +class People { + String name; + int age; + People({required this.name, required this.age}); + void sayHello() { + print('Hello, my name is $name, I am $age years old.'); + } +} + +class Japanese extends People { + Japanese({required String name, required int age}) + : super(name: name, age: age); + void sayHello() { + print('こんにちは、私の名前は$nameです、私は$age歳です。'); + } +} + +void main(List args) { + p.sayHello(); +}