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

fixes https://github.com/flutter/flutter/issues/138289 --- SegmentedButtom.styleFrom has been added to the segment button, so there is no longer any need to the button style from the beginning. It works like ElevatedButton.styleFrom only I added selectedForegroundColor, selectedBackgroundColor. In this way, the user will be able to change the color first without checking the MaterialState states. I added tests of the same controls. #129215 I opened this problem myself, but I was rejected because I handled too many items in a PR. For now, I wrote a structure that only handles MaterialStates instead of users. old (still avaliable) <img width="626" alt="image" src="https://github.com/flutter/flutter/assets/65075121/9446b13b-c355-4d20-bda2-c47a23d42d4f"> new (just an option for developer) <img width="483" alt="image" src="https://github.com/flutter/flutter/assets/65075121/0a645257-4c83-4029-9484-bd746c02265f"> ### Code sample <details> <summary>expand to view the code sample</summary> ```dart import 'package:flutter/material.dart'; /// Flutter code sample for [SegmentedButton]. void main() { runApp(const SegmentedButtonApp()); } enum Calendar { day, week, month, year } class SegmentedButtonApp extends StatefulWidget { const SegmentedButtonApp({super.key}); @override State<SegmentedButtonApp> createState() => _SegmentedButtonAppState(); } class _SegmentedButtonAppState extends State<SegmentedButtonApp> { Calendar calendarView = Calendar.day; @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData(useMaterial3: true), home: Scaffold( body: Center( child: SegmentedButton<Calendar>( style: SegmentedButton.styleFrom( foregroundColor: Colors.amber, visualDensity: VisualDensity.comfortable, ), // style: const ButtonStyle( // foregroundColor: MaterialStatePropertyAll<Color>(Colors.deepPurple), // visualDensity: VisualDensity.comfortable, // ), segments: const <ButtonSegment<Calendar>>[ ButtonSegment<Calendar>( value: Calendar.day, label: Text('Day'), icon: Icon(Icons.calendar_view_day)), ButtonSegment<Calendar>( value: Calendar.week, label: Text('Week'), icon: Icon(Icons.calendar_view_week)), ButtonSegment<Calendar>( value: Calendar.month, label: Text('Month'), icon: Icon(Icons.calendar_view_month)), ButtonSegment<Calendar>( value: Calendar.year, label: Text('Year'), icon: Icon(Icons.calendar_today)), ], selected: <Calendar>{calendarView}, onSelectionChanged: (Set<Calendar> newSelection) { setState(() { calendarView = newSelection.first; }); }, ), ), ), ); } } ``` </details>
67 lines
2.1 KiB
Dart
67 lines
2.1 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.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Flutter code sample for [SegmentedButton.styleFrom].
|
|
|
|
void main() {
|
|
runApp(const SegmentedButtonApp());
|
|
}
|
|
|
|
class SegmentedButtonApp extends StatelessWidget {
|
|
const SegmentedButtonApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const MaterialApp(
|
|
home: Scaffold(
|
|
body: Center(
|
|
child: SegmentedButtonExample(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SegmentedButtonExample extends StatefulWidget {
|
|
const SegmentedButtonExample({super.key});
|
|
|
|
@override
|
|
State<SegmentedButtonExample> createState() => _SegmentedButtonExampleState();
|
|
}
|
|
|
|
enum Calendar { day, week, month, year }
|
|
|
|
class _SegmentedButtonExampleState extends State<SegmentedButtonExample> {
|
|
Calendar calendarView = Calendar.week;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SegmentedButton<Calendar>(
|
|
style: SegmentedButton.styleFrom(
|
|
backgroundColor: Colors.grey[200],
|
|
foregroundColor: Colors.red,
|
|
selectedForegroundColor: Colors.white,
|
|
selectedBackgroundColor: Colors.green,
|
|
),
|
|
segments: const <ButtonSegment<Calendar>>[
|
|
ButtonSegment<Calendar>(value: Calendar.day, label: Text('Day'), icon: Icon(Icons.calendar_view_day)),
|
|
ButtonSegment<Calendar>(value: Calendar.week, label: Text('Week'), icon: Icon(Icons.calendar_view_week)),
|
|
ButtonSegment<Calendar>(value: Calendar.month, label: Text('Month'), icon: Icon(Icons.calendar_view_month)),
|
|
ButtonSegment<Calendar>(value: Calendar.year, label: Text('Year'), icon: Icon(Icons.calendar_today)),
|
|
],
|
|
selected: <Calendar>{calendarView},
|
|
onSelectionChanged: (Set<Calendar> newSelection) {
|
|
setState(() {
|
|
// By default there is only a single segment that can be
|
|
// selected at one time, so its value is always the first
|
|
// item in the selected set.
|
|
calendarView = newSelection.first;
|
|
});
|
|
},
|
|
);
|
|
}
|
|
}
|