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: -
Here is what I want: -
How can I make it possible for my UI?
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.
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.
set
bool _switchValue=true;
in your screen.dart
CupertinoSwitch(
value: _switchValue,
onChanged: (value) {
setState(() {
_switchValue = value;
});
},
),
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
),)
],
),
),
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With