Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize the switch button in a flutter [closed]

In my app, I want the switch is used to toggle a setting between on/off which is true/false respectively. When I went to build it, it turned out that flutter provides a default switch but it is not what I want, I want to customize it accordingly to my UI

This is the flutter switch button: -

enter image description here

Here is what I want: -

enter image description here

How can I make it possible for my UI?

like image 512
Sumit Tifane Avatar asked Oct 19 '19 12:10

Sumit Tifane


People also ask

How do I change the switch size in Flutter?

You could wrap your Switch inside a Transform widget and change the scale. Scale = 1 is the default size , 0.5 half size , 2.0 double size, you can play with the values :) SizedBox( width: 150, height: 40, child: FittedBox( fit: BoxFit.

How do you change Cupertino switch size in Flutter?

So instead, you can wrap your CupertinoSwitch in FittedBox which is inside another Container, giving you more control over your widget. You can copy-paste the below code, you only need to set height and width and make your FittedBox to BoxFit. contain.


2 Answers

set

bool _switchValue=true;

in your screen.dart

 CupertinoSwitch(
              value: _switchValue,
              onChanged: (value) {
                setState(() {
                  _switchValue = value;
                });
              },
            ),
like image 131
makri aymen abderraouf Avatar answered Sep 24 '22 08:09

makri aymen abderraouf


You can use package https://pub.dev/packages/custom_switch or fork it and modify to your

full code

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.deepOrange
      ),
      home: HomeScreen(),
    );
  }
}


class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  bool status = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Switch Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CustomSwitch(
              activeColor: Colors.pinkAccent,
              value: status,
              onChanged: (value) {
                print("VALUE : $value");
                setState(() {
                  status = value;
                });
              },
            ),
            SizedBox(height: 12.0,),
            Text('Value : $status', style: TextStyle(
              color: Colors.black,
              fontSize: 20.0
            ),)
          ],
        ),
      ),
    );
  }
}

enter image description here

like image 29
chunhunghan Avatar answered Sep 22 '22 08:09

chunhunghan