flutter_switch_dark_light 0.0.4

SDKflutter
Platformandroidioswindowslinuxmacosweb

A package that provides a SwitchListTile widget for toggling between dark and light modes.

Flutter Switch Dark/Light

A Flutter package to easily add a switch button to change between dark and light theme in your app.

Installation

Add the following line to your pubspec.yaml file:

dependencies:
  flutter_switch_dark_light: ^0.0.1

Then, run flutter pub get.

Usage

Import the package in your Dart file:

import 'package:flutter_switch_dark_light/flutter_switch_dark_light.dart';

Add the FlutterSwitchDarkLight widget to your app:

FlutterSwitchDarkLight(
  onChanged: (value) {
    //do something with isDark value
  },
  darkModeStatus: _isDarkModeEnabled,
),

Customize the widget using its parameters:

FlutterSwitchDarkLight(
  onChanged: (bool isDark) {
    // do something with isDark value
  },
  padding: const EdgeInsets.all(16.0),
  onText: 'Dark',
  offText: 'Light',
  onColor: Colors.grey[900],
  offColor: Colors.grey[200],
  activeColor: Colors.amber,
),

Parameters

ParameterDescriptionDefault
onChangedCallback function that receives a bool value indicating if the switch is on or off.Required
paddingThe padding around the switch.EdgeInsets.zero
onTextThe text displayed when the switch is on.'Dark'
offTextThe text displayed when the switch is off.'Light'
onColorThe color of the switch when it's on.Colors.grey[900]
offColorThe color of the switch when it's off.Colors.grey[200]
activeColorThe color of the switch's thumb when it's on.Colors.amber

Example

import 'package:flutter/material.dart';
import 'package:flutter_switch_dark_light/flutter_switch_dark_light.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isDarkMode = false;

  void _onThemeChanged(bool value) {
    setState(() {
      _isDarkMode = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Switch Dark/Light Demo',
      theme: _isDarkMode ? ThemeData.dark() : ThemeData.light(),
      home: Scaffold(
        body: Center(
          child: SwitchListTileTheme(
            onChanged: _onThemeChanged,
            onColor: Colors.black,
            offColor: Colors.white,
            activeColor: Colors.blueAccent,
          ),
        ),
      ),
    );
  }
}