mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This extracts the sample code out from the API doc comments, and places them in separate files on disk, allowing running of the examples locally, testing them, and building of slightly larger examples.
116 lines
4.0 KiB
Dart
116 lines
4.0 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// Template: dev/snippets/config/templates/stateful_widget_scaffold_center.tmpl
|
|
//
|
|
// Comment lines marked with "▼▼▼" and "▲▲▲" are used for authoring
|
|
// of samples, and may be ignored if you are just exploring the sample.
|
|
|
|
// Flutter code sample for Radio
|
|
//
|
|
//***************************************************************************
|
|
//* ▼▼▼▼▼▼▼▼ description ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
|
|
|
// Here is an example of Radio widgets wrapped in ListTiles, which is similar
|
|
// to what you could get with the RadioListTile widget.
|
|
//
|
|
// The currently selected character is passed into `groupValue`, which is
|
|
// maintained by the example's `State`. In this case, the first `Radio`
|
|
// will start off selected because `_character` is initialized to
|
|
// `SingingCharacter.lafayette`.
|
|
//
|
|
// If the second radio button is pressed, the example's state is updated
|
|
// with `setState`, updating `_character` to `SingingCharacter.jefferson`.
|
|
// This causes the buttons to rebuild with the updated `groupValue`, and
|
|
// therefore the selection of the second button.
|
|
//
|
|
// Requires one of its ancestors to be a [Material] widget.
|
|
|
|
//* ▲▲▲▲▲▲▲▲ description ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
|
//***************************************************************************
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() => runApp(const MyApp());
|
|
|
|
/// This is the main application widget.
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
static const String _title = 'Flutter Code Sample';
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: _title,
|
|
home: Scaffold(
|
|
appBar: AppBar(title: const Text(_title)),
|
|
body: const Center(
|
|
child: MyStatefulWidget(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
//*****************************************************************************
|
|
//* ▼▼▼▼▼▼▼▼ code-preamble ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
|
|
|
enum SingingCharacter { lafayette, jefferson }
|
|
|
|
//* ▲▲▲▲▲▲▲▲ code-preamble ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
|
//*****************************************************************************
|
|
|
|
/// This is the stateful widget that the main application instantiates.
|
|
class MyStatefulWidget extends StatefulWidget {
|
|
const MyStatefulWidget({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
|
}
|
|
|
|
/// This is the private State class that goes with MyStatefulWidget.
|
|
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
|
//********************************************************************
|
|
//* ▼▼▼▼▼▼▼▼ code ▼▼▼▼▼▼▼▼ (do not modify or remove section marker)
|
|
|
|
SingingCharacter? _character = SingingCharacter.lafayette;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
children: <Widget>[
|
|
ListTile(
|
|
title: const Text('Lafayette'),
|
|
leading: Radio<SingingCharacter>(
|
|
value: SingingCharacter.lafayette,
|
|
groupValue: _character,
|
|
onChanged: (SingingCharacter? value) {
|
|
setState(() {
|
|
_character = value;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
ListTile(
|
|
title: const Text('Thomas Jefferson'),
|
|
leading: Radio<SingingCharacter>(
|
|
value: SingingCharacter.jefferson,
|
|
groupValue: _character,
|
|
onChanged: (SingingCharacter? value) {
|
|
setState(() {
|
|
_character = value;
|
|
});
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
//* ▲▲▲▲▲▲▲▲ code ▲▲▲▲▲▲▲▲ (do not modify or remove section marker)
|
|
//********************************************************************
|
|
|
|
}
|