Active Track Color for Switch
Flutter Switch Active Color is the color used for the track area of the switch when the switch is on. The track area is the horizontal bar that appears behind the thumb.
To set a specific color for the track area when the Switch is active, set activeTrackColor
of Switch with the required Color value.
Code Snippet
Switch(
activeTrackColor: Colors.amber,
),
Example
In the following example, we shall implement a Switch widget with active track color set to Colors.amber
.
main.dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// main application widget
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Application';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: const MyStatefulWidget(),
),
);
}
}
/// stateful widget that the main application instantiates
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// private State class that goes with MyStatefulWidget
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool isSwitched = false;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(50.0),
child: Switch(
activeTrackColor: Colors.amber,
value: isSwitched,
onChanged: (value) {
setState(() {
isSwitched = value;
});
},
),
),
],
),
);
}
}
iPhone Screenshot
Summary
In this Flutter Tutorial, we learned how to set color for the track area of Switch widget when the state of the Switch is active.