简体   繁体   English

Flutter 库比蒂诺开关

[英]Flutter cupertino switch

I am trying to automate a page with multiple switches and text which are in a row.我正在尝试使用连续的多个开关和文本来自动化页面。 I have made a custom widget so that I can call it at times when I need to render a string and a switch together.我制作了一个自定义小部件,以便在需要同时渲染字符串和开关时可以调用它。 But I get an error with it.但我得到了一个错误。

The error is:错误是:

════════ Exception caught by gesture ═══════════════════════════════════════════════════
The method 'call' was called on null.
Receiver: null
Tried calling: call(false)
══════════════════════════════════════════════════════════════════════════════

Here is my code:这是我的代码:

    class FavoriteScreen extends StatefulWidget {
  @override
  _FavoriteScreenState createState() => _FavoriteScreenState();
}

class _FavoriteScreenState extends State<FavoriteScreen> {
  Widget stringSwitch(
      String text, bool val, bool newval, Function onChangedMethod) {
    return Padding(
      padding: EdgeInsets.only(top: 22.0, left: 16.0, right: 16.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            text,
            style: TextStyle(
                fontSize: 12.0,
                fontFamily: 'Roboto',
                fontWeight: FontWeight.w600,
                color: Hexcolor('#676767')),
          ),
          Spacer(),
          CupertinoSwitch(
              trackColor: Hexcolor('#dee7f5'),
              activeColor: Hexcolor('#0565ac'),
              value: val,
              onChanged: (newval) {
                onChangedMethod(newval);
              })
        ],
      ),
    );
  }

  bool val1 = true, val2 = false, val3 = true;

  bool newval1, newval2, newval3;

  onChangedFunction1(bool newval1) {
    setState(() {
      val1 = newval1;
    });
  }

  onChangedFunction2(bool newval2) {
    setState(() {
      val2 = newval2;
    });
  }

  onChangedFunction3(bool newval3) {
    setState(() {
      val3 = newval3;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Hexcolor('#e9f1fe'),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            stringSwitch('ABC', val1, newval1, onChangedFunction1(newval1)),
            stringSwitch('PQR', val2, newval2, onChangedFunction2(newval2)),
            stringSwitch('XYZ', val3, newval3, onChangedFunction3(newval3)),
          ],
        ),
      ),
    );
  }
}

Can you please help me with this?你能帮我解决这个问题吗? A code snippet of correction would be awesome.一个更正的代码片段会很棒。

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can use onChangedFunction 1/2/3 without parameter您可以使用onChangedFunction 1/2/3 不带参数

        stringSwitch('ABC', val1, newval1, onChangedFunction1),
        stringSwitch('PQR', val2, newval2, onChangedFunction2),
        stringSwitch('XYZ', val3, newval3, onChangedFunction3),

working demo工作演示

在此处输入图像描述

full code完整代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

class FavoriteScreen extends StatefulWidget {
  @override
  _FavoriteScreenState createState() => _FavoriteScreenState();
}

class _FavoriteScreenState extends State<FavoriteScreen> {
  Widget stringSwitch(
      String text, bool val, bool newval, Function onChangedMethod) {
    return Padding(
      padding: EdgeInsets.only(top: 22.0, left: 16.0, right: 16.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            text,
            style: TextStyle(
                fontSize: 12.0,
                fontFamily: 'Roboto',
                fontWeight: FontWeight.w600,
                color: Hexcolor('#676767')),
          ),
          Spacer(),
          CupertinoSwitch(
              trackColor: Hexcolor('#dee7f5'),
              activeColor: Hexcolor('#0565ac'),
              value: val,
              onChanged: (newval) {
                onChangedMethod(newval);
              })
        ],
      ),
    );
  }

  bool val1 = true, val2 = false, val3 = true;

  bool newval1, newval2, newval3;

  onChangedFunction1(bool newval1) {
    setState(() {
      val1 = newval1;
    });
  }

  onChangedFunction2(bool newval2) {
    setState(() {
      val2 = newval2;
    });
  }

  onChangedFunction3(bool newval3) {
    setState(() {
      val3 = newval3;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Hexcolor('#e9f1fe'),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            stringSwitch('ABC', val1, newval1, onChangedFunction1),
            stringSwitch('PQR', val2, newval2, onChangedFunction2),
            stringSwitch('XYZ', val3, newval3, onChangedFunction3),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: FavoriteScreen(),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM