Created
November 16, 2023 23:36
-
-
Save luke14free/1c2f0af2703e57f24e3846c3ba7d9ddb to your computer and use it in GitHub Desktop.
Riverpod non funzionante..
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
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
import '../settings/settings_view.dart'; | |
import 'sample_item.dart'; | |
import 'sample_item_details_view.dart'; | |
final textControllerProvider = StateProvider<TextEditingController>((ref) { | |
return TextEditingController(); | |
}); | |
class ListControllerNotifier extends StateNotifier<List<String>> { | |
ListControllerNotifier() : super([]); | |
List<SampleItem> getItems() { | |
return state.map((element) => SampleItem(element)).toList(); | |
} | |
void addString(String stringToAdd) { | |
state = [...state, stringToAdd]; | |
} | |
} | |
final listControllerProvider = | |
StateNotifierProvider((ref) => ListControllerNotifier()); | |
class ItemListView extends ConsumerWidget { | |
const ItemListView({super.key}); | |
static const routeName = '/'; | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
debugPrint('rebuild!'); | |
final items = ref.watch(listControllerProvider.notifier).getItems(); | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Roddy gay'), | |
actions: [ | |
IconButton( | |
icon: const Icon(Icons.settings), | |
onPressed: () { | |
Navigator.restorablePushNamed(context, SettingsView.routeName); | |
}, | |
), | |
], | |
), | |
body: Column(children: [ | |
Text(items.toString()), | |
ListView.builder( | |
restorationId: 'sampleItemListView', | |
itemCount: items.length, | |
shrinkWrap: true, | |
scrollDirection: Axis.vertical, | |
itemBuilder: (BuildContext context, int index) { | |
final item = items[index]; | |
return ListTile( | |
title: Text('${item.name}'), | |
leading: const CircleAvatar( | |
foregroundImage: | |
AssetImage('assets/images/flutter_logo.png'), | |
), | |
onTap: () { | |
// Navigate to the details page. If the user leaves and returns to | |
// the app after it has been killed while running in the | |
// background, the navigation stack is restored. | |
Navigator.restorablePushNamed( | |
context, | |
SampleItemDetailsView.routeName, | |
); | |
}); | |
}, | |
), | |
Row( | |
children: [ | |
Expanded( | |
child: TextField( | |
controller: ref.read(textControllerProvider.notifier).state, | |
onChanged: (value) { | |
debugPrint(value); | |
ref.read(textControllerProvider.notifier).state.text = value; | |
}, | |
)), | |
FloatingActionButton(onPressed: () { | |
ref.read(listControllerProvider.notifier).addString( | |
ref.read(textControllerProvider.notifier).state.text); | |
ref.read(textControllerProvider.notifier).state.text = ""; | |
}) | |
], | |
) | |
])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment