import 'package:flutter/material.dart';
// Entry point of the application
void main() => runApp(ToggleButtonRun());
// Stateful widget to manage the state of the toggle buttons
class ToggleButtonRun extends StatefulWidget {
@override
_ToggleButtonRunState createState() => _ToggleButtonRunState();
}
class _ToggleButtonRunState extends State<ToggleButtonRun> {
// List to track the selection state of the toggle buttons
List<bool> _selections = List.generate(3, (_) => false);
// Variables to store the text style properties
var TxtBold = FontWeight.normal;
var TxtItalic = FontStyle.normal;
var TxtUnder = TextDecoration.none;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Toggle Buttons',
home: Scaffold(
appBar: AppBar(
title: Text("Toggle Buttons"),
backgroundColor: Colors.teal,
foregroundColor: Colors.white,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
// ToggleButtons widget to toggle text styles
ToggleButtons(
children: <Widget>[
Icon(Icons.format_bold), // Bold icon
Icon(Icons.format_italic), // Italic icon
Icon(Icons.format_underlined), // Underline icon
],
isSelected: _selections, // Track the selection state
onPressed: (int index) {
setState(() {
// Toggle the selection state
_selections[index] = !_selections[index];
// Update text style based on the selected button
if (index == 0 && _selections[index]) {
TxtBold = FontWeight.bold;
} else if (index == 0 && !_selections[index]) {
TxtBold = FontWeight.normal;
}
if (index == 1 && _selections[index]) {
TxtItalic = FontStyle.italic;
} else if (index == 1 && !_selections[index]) {
TxtItalic = FontStyle.normal;
}
if (index == 2 && _selections[index]) {
TxtUnder = TextDecoration.underline;
} else if (index == 2 && !_selections[index]) {
TxtUnder = TextDecoration.none;
}
});
},
color: Colors.teal,
fillColor: Colors.deepPurple,
),
// Text widget to display the styled text
Text(
"This Is A Simple Text,Press Buttons Up And See What Gonna Happen",
style: TextStyle(
fontWeight: TxtBold, // Apply bold style
fontStyle: TxtItalic, // Apply italic style
decoration: TxtUnder, // Apply underline style
),
)
],
),
),
),
);
}
}