1. Home
  2. /
  3. Flutter
  4. /
  5. Dark/Light Theme Toggle

Dark/Light Theme Toggle - Flutter Typing CST Test

Loading…

Dark/Light Theme Toggle — Flutter Code

Toggles between light and dark themes using StatefulWidget.

import 'package:flutter/material.dart';

void main() => runApp(ThemeToggleApp());

class ThemeToggleApp extends StatefulWidget {
	@override
	_ThemeToggleAppState createState() => _ThemeToggleAppState();
}

class _ThemeToggleAppState extends State<ThemeToggleApp> {
	bool _isDark = false;

	void _toggleTheme() => setState(() => _isDark = !_isDark);

	@override
	Widget build(BuildContext context) {
		return MaterialApp(
			theme: _isDark ? ThemeData.dark() : ThemeData.light(),
			home: Scaffold(
				appBar: AppBar(title: Text('Theme Toggle')),
				body: Center(
					child: ElevatedButton(
						onPressed: _toggleTheme,
						child: Text(_isDark ? 'Switch to Light' : 'Switch to Dark'),
					),
				),
			),
		);
	}
}

Flutter Language Guide

Flutter is an open-source UI framework by Google for building cross-platform mobile, web, and desktop applications using a single Dart codebase, featuring a highly performant rendering engine and a rich set of customizable widgets.

Primary Use Cases

  • ▸Cross-platform mobile apps (iOS & Android)
  • ▸Web applications
  • ▸Desktop apps (macOS, Windows, Linux)
  • ▸Startup MVP development
  • ▸Enterprise internal tools
  • ▸Apps requiring advanced UI/animations
  • ▸Real-time dashboards and admin apps

Notable Features

  • ▸Single codebase for all platforms
  • ▸Hot reload and hot restart
  • ▸High-performance Skia rendering engine
  • ▸Rich built-in widget library
  • ▸Material & Cupertino components
  • ▸Dart AOT/JIT compilation
  • ▸Support for custom animations
  • ▸Strong ecosystem with Firebase

Origin & Creator

Created by Google; initial public release in 2017.

Industrial Note

Dominates fast cross-platform development for mobile-first startups, enterprise apps, fintech dashboards, e-commerce platforms, and apps requiring custom, fluid UI experiences across devices.

More Flutter Typing Exercises

Flutter Stateless WidgetFlutter Counter with Increment and DecrementFlutter Counter with ResetFlutter Simple StopwatchFlutter Counter with History

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher