Flutter #120: Apple iOS 16 Cupertino Switch in Flutter


Apple iOS 16 Cupertino Swtich in Flutter. Copy and paste the below code as per your requirements. To watch GTA 5, go to my youtube channel.

import 'package:flutter/cupertino.dart';
void main() => runApp(const CupertinoSwitchApp());

class CupertinoSwitchApp extends StatelessWidget{
  const CupertinoSwitchApp({super.key});
  @override
  Widget build(BuildContext context){
    return const CupertinoApp(
      theme: CupertinoThemeData(brightness: Brightness.light),
      home: CupertinoSwitchExample(),
    );
  }
}
class CupertinoSwitchExample extends StatefulWidget{
  const CupertinoSwitchExample({super.key});
  @override
  State<CupertinoSwitchExample> createState()=> _CupertinoSwitchExampleState();
}
class _CupertinoSwitchExampleState extends State<CupertinoSwitchExample>{
  bool wifi= true;
  @override
  Widget build(BuildContext context){
    return CupertinoPageScaffold(
      navigationBar: const CupertinoNavigationBar(
        middle: Text("Cupertino Switch"),
      ),
        child: Center(
          child: IntrinsicHeight(
            child: Container(
              color: CupertinoTheme.of(context).barBackgroundColor,
              child: CupertinoFormRow(
                prefix: Row(
                  children: [
                    Icon(
                      wifi? CupertinoIcons.wifi : CupertinoIcons.wifi_slash,
                      color: wifi? CupertinoColors.systemBlue:
                      CupertinoColors.systemRed,
                    ),
                    const SizedBox(width: 10,),
                    const Text('Wi-Fi')
                  ],
                ),
                child: CupertinoSwitch(
                  value: wifi,
                  thumbColor: CupertinoColors.white,
                  trackColor: CupertinoColors.systemGreen,
                  activeColor: CupertinoColors.systemGreen,
                  onChanged: (bool? value){
                    setState(() {
                      wifi=value!;
                    });
                  },
                ),
              ),
            ),
          ),
        )
    );
  }
}

Leave a Reply

Your email address will not be published.