Created
December 14, 2022 01:10
-
-
Save gothedistance/81251160d64c8dbf0372da2a4c41ab32 to your computer and use it in GitHub Desktop.
Riverpodでラジオボタンの選択値を変える
This file contains hidden or 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:hooks_riverpod/hooks_riverpod.dart'; | |
final stateValue = StateProvider(((ref) => "")); | |
final List<String> bloodType = ["A", "B", "O", "AB"]; | |
void main() { | |
runApp(const ProviderScope(child: MyApp())); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const RiverpodRadioGroup(), | |
); | |
} | |
} | |
class RiverpodRadioGroup extends ConsumerWidget { | |
const RiverpodRadioGroup({super.key}); | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
final groupValue = ref.watch(stateValue); | |
return Scaffold( | |
appBar: AppBar(), | |
body: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: bloodType | |
.map((e) => RadioListTile( | |
activeColor: Colors.green, | |
title: Text(e), | |
value: e, | |
groupValue: groupValue, | |
onChanged: (value) { | |
ref.read(stateValue.notifier).state = value ?? ""; | |
}, | |
)) | |
.toList()), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment