Add Material 3 SwitchListTile example and update existing examples (#119714)
* Add Material 3 `SwitchListTile` example and update existing examples * Update examples with `useMaterial3: true` and update example descriptions. * add a `ColorScheme` colour
This commit is contained in:
@@ -14,8 +14,9 @@ class CheckboxListTileApp extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: CheckboxListTileExample(),
|
||||
return MaterialApp(
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: const CheckboxListTileExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ class CheckboxListTileApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4), useMaterial3: true),
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: const CheckboxListTileExample(),
|
||||
);
|
||||
}
|
||||
@@ -44,7 +44,7 @@ class _CheckboxListTileExampleState extends State<CheckboxListTileExample> {
|
||||
setState(() {
|
||||
checkboxValue1 = value!;
|
||||
});
|
||||
},
|
||||
},
|
||||
title: const Text('Headline'),
|
||||
subtitle: const Text('Supporting text'),
|
||||
),
|
||||
@@ -55,7 +55,7 @@ class _CheckboxListTileExampleState extends State<CheckboxListTileExample> {
|
||||
setState(() {
|
||||
checkboxValue2 = value!;
|
||||
});
|
||||
},
|
||||
},
|
||||
title: const Text('Headline'),
|
||||
subtitle: const Text('Longer supporting text to demonstrate how the text wraps and the checkbox is centered vertically with the text.'),
|
||||
),
|
||||
@@ -66,7 +66,7 @@ class _CheckboxListTileExampleState extends State<CheckboxListTileExample> {
|
||||
setState(() {
|
||||
checkboxValue3 = value!;
|
||||
});
|
||||
},
|
||||
},
|
||||
title: const Text('Headline'),
|
||||
subtitle: const Text("Longer supporting text to demonstrate how the text wraps and how setting 'CheckboxListTile.isThreeLine = true' aligns the checkbox to the top vertically with the text."),
|
||||
isThreeLine: true,
|
||||
|
||||
@@ -8,15 +8,16 @@ import 'package:flutter/gestures.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const LabeledCheckBoxApp());
|
||||
void main() => runApp(const LabeledCheckboxApp());
|
||||
|
||||
class LabeledCheckBoxApp extends StatelessWidget {
|
||||
const LabeledCheckBoxApp({super.key});
|
||||
class LabeledCheckboxApp extends StatelessWidget {
|
||||
const LabeledCheckboxApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: LabeledCheckBoxExample(),
|
||||
return MaterialApp(
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: const LabeledCheckboxExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -68,14 +69,14 @@ class LinkedLabelCheckbox extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class LabeledCheckBoxExample extends StatefulWidget {
|
||||
const LabeledCheckBoxExample({super.key});
|
||||
class LabeledCheckboxExample extends StatefulWidget {
|
||||
const LabeledCheckboxExample({super.key});
|
||||
|
||||
@override
|
||||
State<LabeledCheckBoxExample> createState() => _LabeledCheckBoxExampleState();
|
||||
State<LabeledCheckboxExample> createState() => _LabeledCheckboxExampleState();
|
||||
}
|
||||
|
||||
class _LabeledCheckBoxExampleState extends State<LabeledCheckBoxExample> {
|
||||
class _LabeledCheckboxExampleState extends State<LabeledCheckboxExample> {
|
||||
bool _isSelected = false;
|
||||
|
||||
@override
|
||||
|
||||
@@ -6,15 +6,16 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const LabeledCheckBoxApp());
|
||||
void main() => runApp(const LabeledCheckboxApp());
|
||||
|
||||
class LabeledCheckBoxApp extends StatelessWidget {
|
||||
const LabeledCheckBoxApp({super.key});
|
||||
class LabeledCheckboxApp extends StatelessWidget {
|
||||
const LabeledCheckboxApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: LabeledCheckBoxExample(),
|
||||
return MaterialApp(
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: const LabeledCheckboxExample(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -57,14 +58,14 @@ class LabeledCheckbox extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class LabeledCheckBoxExample extends StatefulWidget {
|
||||
const LabeledCheckBoxExample({super.key});
|
||||
class LabeledCheckboxExample extends StatefulWidget {
|
||||
const LabeledCheckboxExample({super.key});
|
||||
|
||||
@override
|
||||
State<LabeledCheckBoxExample> createState() => _LabeledCheckBoxExampleState();
|
||||
State<LabeledCheckboxExample> createState() => _LabeledCheckboxExampleState();
|
||||
}
|
||||
|
||||
class _LabeledCheckBoxExampleState extends State<LabeledCheckBoxExample> {
|
||||
class _LabeledCheckboxExampleState extends State<LabeledCheckboxExample> {
|
||||
bool _isSelected = false;
|
||||
|
||||
@override
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
// 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.
|
||||
|
||||
// Flutter code sample for custom labeled switch.
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const LabeledSwitchApp());
|
||||
|
||||
class LabeledSwitchApp extends StatelessWidget {
|
||||
const LabeledSwitchApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text('Custom Labeled Switch Sample')),
|
||||
body: const Center(
|
||||
child: LabeledSwitchExample(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class LinkedLabelSwitch extends StatelessWidget {
|
||||
const LinkedLabelSwitch({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.padding,
|
||||
required this.value,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
final String label;
|
||||
final EdgeInsets padding;
|
||||
final bool value;
|
||||
final ValueChanged<bool> onChanged;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: padding,
|
||||
child: Row(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: RichText(
|
||||
text: TextSpan(
|
||||
text: label,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () {
|
||||
debugPrint('Label has been tapped.');
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
Switch(
|
||||
value: value,
|
||||
onChanged: (bool newValue) {
|
||||
onChanged(newValue);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class LabeledSwitchExample extends StatefulWidget {
|
||||
const LabeledSwitchExample({super.key});
|
||||
|
||||
@override
|
||||
State<LabeledSwitchExample> createState() => _LabeledSwitchExampleState();
|
||||
}
|
||||
|
||||
class _LabeledSwitchExampleState extends State<LabeledSwitchExample> {
|
||||
bool _isSelected = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LinkedLabelSwitch(
|
||||
label: 'Linked, tappable label text',
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20.0),
|
||||
value: _isSelected,
|
||||
onChanged: (bool newValue) {
|
||||
setState(() {
|
||||
_isSelected = newValue;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,25 +2,23 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Flutter code sample for [SwitchListTile].
|
||||
// Flutter code sample for custom labeled switch.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
void main() => runApp(const LabeledSwitchApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
class LabeledSwitchApp extends StatelessWidget {
|
||||
const LabeledSwitchApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: _title,
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text(_title)),
|
||||
appBar: AppBar(title: const Text('Custom Labeled Switch Sample')),
|
||||
body: const Center(
|
||||
child: MyStatefulWidget(),
|
||||
child: LabeledSwitchExample(),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -65,14 +63,14 @@ class LabeledSwitch extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class MyStatefulWidget extends StatefulWidget {
|
||||
const MyStatefulWidget({super.key});
|
||||
class LabeledSwitchExample extends StatefulWidget {
|
||||
const LabeledSwitchExample({super.key});
|
||||
|
||||
@override
|
||||
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
State<LabeledSwitchExample> createState() => _LabeledSwitchExampleState();
|
||||
}
|
||||
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
||||
class _LabeledSwitchExampleState extends State<LabeledSwitchExample> {
|
||||
bool _isSelected = false;
|
||||
|
||||
@override
|
||||
@@ -6,35 +6,33 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
void main() => runApp(const SwitchListTileApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
class SwitchListTileApp extends StatelessWidget {
|
||||
const SwitchListTileApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: _title,
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text(_title)),
|
||||
appBar: AppBar(title: const Text('SwitchListTile Sample')),
|
||||
body: const Center(
|
||||
child: MyStatefulWidget(),
|
||||
child: SwitchListTileExample(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyStatefulWidget extends StatefulWidget {
|
||||
const MyStatefulWidget({super.key});
|
||||
class SwitchListTileExample extends StatefulWidget {
|
||||
const SwitchListTileExample({super.key});
|
||||
|
||||
@override
|
||||
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
State<SwitchListTileExample> createState() => _SwitchListTileExampleState();
|
||||
}
|
||||
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
||||
class _SwitchListTileExampleState extends State<SwitchListTileExample> {
|
||||
bool _lights = false;
|
||||
|
||||
@override
|
||||
|
||||
@@ -4,99 +4,77 @@
|
||||
|
||||
// Flutter code sample for [SwitchListTile].
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(const MyApp());
|
||||
void main() => runApp(const SwitchListTileApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
static const String _title = 'Flutter Code Sample';
|
||||
class SwitchListTileApp extends StatelessWidget {
|
||||
const SwitchListTileApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: _title,
|
||||
theme: ThemeData(useMaterial3: true),
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: const Text(_title)),
|
||||
body: const Center(
|
||||
child: MyStatefulWidget(),
|
||||
),
|
||||
),
|
||||
appBar: AppBar(title: const Text('SwitchListTile Sample')),
|
||||
body: const SwitchListTileExample()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class LinkedLabelSwitch extends StatelessWidget {
|
||||
const LinkedLabelSwitch({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.padding,
|
||||
required this.value,
|
||||
required this.onChanged,
|
||||
});
|
||||
class SwitchListTileExample extends StatefulWidget {
|
||||
const SwitchListTileExample({super.key});
|
||||
|
||||
final String label;
|
||||
final EdgeInsets padding;
|
||||
final bool value;
|
||||
final ValueChanged<bool> onChanged;
|
||||
@override
|
||||
State<SwitchListTileExample> createState() => _SwitchListTileExampleState();
|
||||
}
|
||||
|
||||
class _SwitchListTileExampleState extends State<SwitchListTileExample> {
|
||||
bool switchValue1 = true;
|
||||
bool switchValue2 = true;
|
||||
bool switchValue3 = true;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: padding,
|
||||
child: Row(
|
||||
return Scaffold(
|
||||
body: Column(
|
||||
children: <Widget>[
|
||||
Expanded(
|
||||
child: RichText(
|
||||
text: TextSpan(
|
||||
text: label,
|
||||
style: const TextStyle(
|
||||
color: Colors.blueAccent,
|
||||
decoration: TextDecoration.underline,
|
||||
),
|
||||
recognizer: TapGestureRecognizer()
|
||||
..onTap = () {
|
||||
debugPrint('Label has been tapped.');
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
Switch(
|
||||
value: value,
|
||||
onChanged: (bool newValue) {
|
||||
onChanged(newValue);
|
||||
SwitchListTile(
|
||||
value: switchValue1,
|
||||
onChanged: (bool? value) {
|
||||
setState(() {
|
||||
switchValue1 = value!;
|
||||
});
|
||||
},
|
||||
title: const Text('Headline'),
|
||||
subtitle: const Text('Supporting text'),
|
||||
),
|
||||
const Divider(height: 0),
|
||||
SwitchListTile(
|
||||
value: switchValue2,
|
||||
onChanged: (bool? value) {
|
||||
setState(() {
|
||||
switchValue2 = value!;
|
||||
});
|
||||
},
|
||||
title: const Text('Headline'),
|
||||
subtitle: const Text('Longer supporting text to demonstrate how the text wraps and the switch is centered vertically with the text.'),
|
||||
),
|
||||
const Divider(height: 0),
|
||||
SwitchListTile(
|
||||
value: switchValue3,
|
||||
onChanged: (bool? value) {
|
||||
setState(() {
|
||||
switchValue3 = value!;
|
||||
});
|
||||
},
|
||||
title: const Text('Headline'),
|
||||
subtitle: const Text("Longer supporting text to demonstrate how the text wraps and how setting 'SwitchListTile.isThreeLine = true' aligns the switch to the top vertically with the text."),
|
||||
isThreeLine: true,
|
||||
),
|
||||
const Divider(height: 0),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyStatefulWidget extends StatefulWidget {
|
||||
const MyStatefulWidget({super.key});
|
||||
|
||||
@override
|
||||
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
|
||||
}
|
||||
|
||||
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
|
||||
bool _isSelected = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LinkedLabelSwitch(
|
||||
label: 'Linked, tappable label text',
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20.0),
|
||||
value: _isSelected,
|
||||
onChanged: (bool newValue) {
|
||||
setState(() {
|
||||
_isSelected = newValue;
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user