今回は、ボタンやテキスト入力やらの入力操作が必要なウィジェットの実装方法の知識を深めていきたいと思います。
・Xcode 15.1
・VSCode 1.85.1
・Flutter 3.16.5
・Dart 3.2.3
環境
・MacOS Ventura 13.6.3・Xcode 15.1
・VSCode 1.85.1
・Flutter 3.16.5
・Dart 3.2.3


class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      navigationBar: const CupertinoNavigationBar(
        middle: Text(
          'Interactive',
        ),
      ),
      child: SafeArea(
        child: SingleChildScrollView(
          child: Container(
            width: double.infinity,
            color: Colors.grey,
            child: Container(
              padding: const EdgeInsets.all(15),
              child: const Column(
                children: [
                  TextFieldExample(), // テキスト入力
                  SwitchExample(), // スイッチ
                  SliderExample(), // スライダー
                  SegmentsExample(), // セグメント
                &nb
class TextFieldExample extends StatefulWidget {
  const TextFieldExample({super.key});
  @override
  State<TextFieldExample> createState() => _TextFieldExampleState();
}
class _TextFieldExampleState extends State<TextFieldExample> {
  late TextEditingController _text1Controller;
  late TextEditingController _text2Controller;
  late TextEditingController _text3Controller;
  @override
  void initState() {
    super.initState();
    _text1Controller = TextEditingController(text: 'text1');
    _text2Controller = TextEditingController(text: 'text2');
    _text3Controller = TextEditingController(text: 'multi1\nmulti2');
  }
  @override
  void dispose() {
    _text1Controller.dispose();
    _text2Controller.dispose();
    _text3Controller.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: Container(
        color: Colors.blue,
        child: Padding(
          padding: const EdgeInsets.all(10),
          child: Column(
            children: [
              CupertinoTextField(
                controller: _text1Controller,
     &nb
class SwitchExample extends StatefulWidget {
  const SwitchExample({super.key});
  @override
  State<SwitchExample> createState() => _SwitchExampleState();
}
class _SwitchExampleState extends State<SwitchExample> {
  bool switchValue = true;
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: Container(
        color: Colors.white,
        child: Column(
          children: [
            Row(
              children: [
                const Text('スイッチ'),
                const Spacer(),
                CupertinoSwitch(
                  value: switchValue,
                  activeColor: CupertinoColors.activeGreen,
                  onChanged: (value) {
                    setState(() {
                      switchValue = value;
&nbs
class SliderExample extends StatefulWidget {
  const SliderExample({super.key});
  @override
  State<SliderExample> createState() => _SliderExampleState();
}
class _SliderExampleState extends State<SliderExample> {
  double _currentSliderValue = 0.0;
  String? _sliderStatus;
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: Container(
        width: double.infinity,
        color: Colors.white,
        child: Column(
          children: [
            Text('スイッチ値 $_currentSliderValue'),
            SizedBox(
              width: double.infinity,
              child: CupertinoSlider(
                key: const Key('slider'),
                value: _currentSliderValue,
                divisions: 5,
                max: 100,
                activeColor: CupertinoColors.systemBlue,
                t
enum SegmentType { seg1, seg2, seg3 }
Map<SegmentType, Color> segmentTypes = <SegmentType, Color>{
  SegmentType.seg1: Colors.red,
  SegmentType.seg2: Colors.green,
  SegmentType.seg3: Colors.blue,
};
class SegmentsExample extends StatefulWidget {
  const SegmentsExample({super.key});
  @override
  State<SegmentsExample> createState() => _SegmentsExampleState();
}
class _SegmentsExampleState extends State<SegmentsExample> {
  SegmentType _selectedType = SegmentType.seg1;
  SegmentType _selectedSlidingType = SegmentType.seg2;
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: Container(
        width: double.infinity,
        color: Colors.white,
        child: Column(
          children: [
            SizedBox(
              width: double.infinity,
              child: CupertinoSegmentedControl<SegmentType>(
                selectedColor: CupertinoColors.activeBlue,
                groupValue: _selectedType,
 &n
const List<String> _fruitNames = <String>[
  'Apple',
  'Mango',
  'Banana',
  'Orange',
  'Pineapple',
  'Strawberry',
];
class PickerExample extends StatefulWidget {
  const PickerExample({super.key});
  @override
  State<PickerExample> createState() => _PickerExampleState();
}
class _PickerExampleState extends State<PickerExample> {
  int _selectedFruit = 0;
  DateTime dateTime = DateTime.now();
  @override
  Widget build(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: Container(
        width: double.infinity,
        color: Colors.white,
        child: Column(
          children: [
            SizedBox(
              height: 200,
              child: CupertinoPicker(
                itemExtent: 32,
                scrollController: FixedExtentScrollController(
                  initialItem: _selectedFruit,
                ),
          &今回は、入力操作が必要なウィジェットの実装方法を深めるのを目的に記事を書きました。
次回は、ポップアップ等の入力操作ウィジェットの実装方法に触れたいと思います。