Skip to content

Instantly share code, notes, and snippets.

@gothedistance
Created December 14, 2022 01:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gothedistance/81251160d64c8dbf0372da2a4c41ab32 to your computer and use it in GitHub Desktop.
Save gothedistance/81251160d64c8dbf0372da2a4c41ab32 to your computer and use it in GitHub Desktop.
Riverpodでラジオボタンの選択値を変える
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