Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter_switch/flutter_switch.dart';
- import 'package:flutter/material.dart';
- import 'package:smart_relay/theme.dart';
- class HomePage extends StatefulWidget {
- const HomePage({Key? key}) : super(key: key);
- @override
- State<HomePage> createState() => _HomePageState();
- }
- class _HomePageState extends State<HomePage> {
- List state = [
- false,
- true,
- false,
- true,
- ];
- List<Widget> w = [];
- @override
- Widget build(BuildContext context) {
- /*Widget switchs(label, index) {
- return Container(
- padding: const EdgeInsets.only(
- top: 20,
- ),
- child: Row(
- children: <Widget>[
- Text(
- label,
- style: primaryTextStyle.copyWith(
- fontSize: 24,
- fontWeight: semiBold,
- ),
- ),
- const SizedBox(
- width: 200,
- ),
- FlutterSwitch(
- width: 125.0,
- height: 55.0,
- valueFontSize: 25.0,
- toggleSize: 45.0,
- value: state[index],
- borderRadius: 30.0,
- padding: 8.0,
- showOnOff: true,
- activeColor: Colors.blueAccent,
- onToggle: (val) {
- setState(() {
- state[index] = val;
- });
- },
- ),
- ],
- ),
- );
- }
- Widget switchGenerator() {
- return ListView(
- children: [
- switchs("Relay 1", 0),
- ],
- );
- }*/
- w.add(generateSwitch(
- stateHeader: true,
- labelHeader: "Rumah",
- labelRelay: "Relay 1",
- stateRelay: state[0],
- ));
- return Material(
- child: Scaffold(
- appBar: AppBar(
- leading: const Image(
- image: AssetImage(
- "assets/sebuahhobi.png",
- ),
- ),
- title: const Text(
- "Smart Relay",
- ),
- ),
- body: Container(
- margin: const EdgeInsets.all(10),
- child: ListView(
- children: [
- Column(
- children: [
- //cara agar bisa pakai List,
- //agar jumlah switch dinamis
- generateSwitch(
- stateHeader: true,
- labelHeader: "Rumah",
- labelRelay: "Relay 1",
- stateRelay: state[0], //cara agar state switch bisa berubah
- ),
- generateSwitch(
- stateHeader: false,
- labelRelay: "Relay 2",
- stateRelay: state[1],
- ),
- generateSwitch(
- stateHeader: false,
- labelRelay: "Relay 3",
- stateRelay: state[2],
- ),
- ],
- ),
- ],
- ),
- ),
- ),
- );
- }
- Column generateSwitch({
- stateHeader = false,
- labelHeader,
- stateRelay,
- required String labelRelay,
- }) {
- return Column(
- // mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- (stateHeader) == true
- ? Text(
- labelHeader,
- style: TextStyle(
- fontSize: 20,
- fontWeight: bold,
- ),
- )
- : const Text(""),
- Row(
- children: [
- const Icon(Icons.home),
- const SizedBox(
- width: 5,
- ),
- Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(
- labelRelay,
- style: const TextStyle(
- fontSize: 18,
- ),
- ),
- Row(
- children: const [
- Text(
- "18:19",
- style: TextStyle(
- fontSize: 10,
- ),
- ),
- Text(
- " - ",
- style: TextStyle(
- fontSize: 10,
- ),
- ),
- Text(
- "20:20",
- style: TextStyle(
- fontSize: 10,
- ),
- ),
- ],
- ),
- ],
- ),
- const SizedBox(
- width: 120,
- ),
- FlutterSwitch(
- width: 70.0,
- height: 33.0,
- valueFontSize: 18.0,
- toggleSize: 25.0,
- value: stateRelay,
- borderRadius: 30.0,
- padding: 3.0,
- showOnOff: true,
- activeColor: Colors.blueAccent,
- inactiveColor: Colors.black,
- inactiveTextColor: Colors.white,
- activeTextColor: Colors.white,
- onToggle: (val) {
- setState(() {
- stateRelay = val;
- });
- },
- )
- ],
- ),
- ],
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement