颤动铜杯开关

时间:2020-08-14 01:44:18

标签: flutter dart flutter-layout

我正在尝试使具有连续多个开关和文本的页面自动化。我制作了一个自定义窗口小部件,以便在需要一起渲染字符串和开关时可以调用它。但是我得到了一个错误。

错误是:

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

这是我的代码:

    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)),
          ],
        ),
      ),
    );
  }
}

您能帮我吗?更正的代码片段很棒。

1 个答案:

答案 0 :(得分:0)

您可以在下面复制粘贴运行完整代码
您可以不带参数使用onChangedFunction 1/2/3

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

工作演示

enter image description here

完整代码

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(),
    );
  }
}