Advertisement
Sebuahhobi98

dinamis

Jun 6th, 2022
1,529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.37 KB | None | 0 0
  1. import 'package:flutter_switch/flutter_switch.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:smart_relay/theme.dart';
  4.  
  5. class HomePage extends StatefulWidget {
  6.   const HomePage({Key? key}) : super(key: key);
  7.  
  8.   @override
  9.   State<HomePage> createState() => _HomePageState();
  10. }
  11.  
  12. class _HomePageState extends State<HomePage> {
  13.   List state = [
  14.     false,
  15.     true,
  16.     false,
  17.     true,
  18.   ];
  19.  
  20.   List<Widget> w = [];
  21.  
  22.   @override
  23.   Widget build(BuildContext context) {
  24.     /*Widget switchs(label, index) {
  25.       return Container(
  26.         padding: const EdgeInsets.only(
  27.           top: 20,
  28.         ),
  29.         child: Row(
  30.           children: <Widget>[
  31.             Text(
  32.               label,
  33.               style: primaryTextStyle.copyWith(
  34.                 fontSize: 24,
  35.                 fontWeight: semiBold,
  36.               ),
  37.             ),
  38.             const SizedBox(
  39.               width: 200,
  40.             ),
  41.             FlutterSwitch(
  42.               width: 125.0,
  43.               height: 55.0,
  44.               valueFontSize: 25.0,
  45.               toggleSize: 45.0,
  46.               value: state[index],
  47.               borderRadius: 30.0,
  48.               padding: 8.0,
  49.               showOnOff: true,
  50.               activeColor: Colors.blueAccent,
  51.               onToggle: (val) {
  52.                 setState(() {
  53.                   state[index] = val;
  54.                 });
  55.               },
  56.             ),
  57.           ],
  58.         ),
  59.       );
  60.     }
  61.  
  62.     Widget switchGenerator() {
  63.       return ListView(
  64.         children: [
  65.           switchs("Relay 1", 0),
  66.         ],
  67.       );
  68.     }*/
  69.     w.add(generateSwitch(
  70.       stateHeader: true,
  71.       labelHeader: "Rumah",
  72.       labelRelay: "Relay 1",
  73.       stateRelay: state[0],
  74.     ));
  75.  
  76.     return Material(
  77.       child: Scaffold(
  78.         appBar: AppBar(
  79.           leading: const Image(
  80.             image: AssetImage(
  81.               "assets/sebuahhobi.png",
  82.             ),
  83.           ),
  84.           title: const Text(
  85.             "Smart Relay",
  86.           ),
  87.         ),
  88.         body: Container(
  89.           margin: const EdgeInsets.all(10),
  90.           child: ListView(
  91.             children: [
  92.               Column(
  93.                 children: [
  94.                   //cara agar bisa pakai List,
  95.                   //agar jumlah switch dinamis
  96.                   generateSwitch(
  97.                     stateHeader: true,
  98.                     labelHeader: "Rumah",
  99.                     labelRelay: "Relay 1",
  100.                     stateRelay: state[0], //cara agar state switch bisa berubah
  101.                   ),
  102.                   generateSwitch(
  103.                     stateHeader: false,
  104.                     labelRelay: "Relay 2",
  105.                     stateRelay: state[1],
  106.                   ),
  107.                   generateSwitch(
  108.                     stateHeader: false,
  109.                     labelRelay: "Relay 3",
  110.                     stateRelay: state[2],
  111.                   ),
  112.                 ],
  113.               ),
  114.             ],
  115.           ),
  116.         ),
  117.       ),
  118.     );
  119.   }
  120.  
  121.   Column generateSwitch({
  122.     stateHeader = false,
  123.     labelHeader,
  124.     stateRelay,
  125.     required String labelRelay,
  126.   }) {
  127.     return Column(
  128.       // mainAxisAlignment: MainAxisAlignment.start,
  129.       crossAxisAlignment: CrossAxisAlignment.start,
  130.       children: [
  131.         (stateHeader) == true
  132.             ? Text(
  133.                 labelHeader,
  134.                 style: TextStyle(
  135.                   fontSize: 20,
  136.                   fontWeight: bold,
  137.                 ),
  138.               )
  139.             : const Text(""),
  140.         Row(
  141.           children: [
  142.             const Icon(Icons.home),
  143.             const SizedBox(
  144.               width: 5,
  145.             ),
  146.             Column(
  147.               crossAxisAlignment: CrossAxisAlignment.start,
  148.               children: [
  149.                 Text(
  150.                   labelRelay,
  151.                   style: const TextStyle(
  152.                     fontSize: 18,
  153.                   ),
  154.                 ),
  155.                 Row(
  156.                   children: const [
  157.                     Text(
  158.                       "18:19",
  159.                       style: TextStyle(
  160.                         fontSize: 10,
  161.                       ),
  162.                     ),
  163.                     Text(
  164.                       " - ",
  165.                       style: TextStyle(
  166.                         fontSize: 10,
  167.                       ),
  168.                     ),
  169.                     Text(
  170.                       "20:20",
  171.                       style: TextStyle(
  172.                         fontSize: 10,
  173.                       ),
  174.                     ),
  175.                   ],
  176.                 ),
  177.               ],
  178.             ),
  179.             const SizedBox(
  180.               width: 120,
  181.             ),
  182.             FlutterSwitch(
  183.               width: 70.0,
  184.               height: 33.0,
  185.               valueFontSize: 18.0,
  186.               toggleSize: 25.0,
  187.               value: stateRelay,
  188.               borderRadius: 30.0,
  189.               padding: 3.0,
  190.               showOnOff: true,
  191.               activeColor: Colors.blueAccent,
  192.               inactiveColor: Colors.black,
  193.               inactiveTextColor: Colors.white,
  194.               activeTextColor: Colors.white,
  195.               onToggle: (val) {
  196.                 setState(() {
  197.                   stateRelay = val;
  198.                 });
  199.               },
  200.             )
  201.           ],
  202.         ),
  203.       ],
  204.     );
  205.   }
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement