【问题标题】:Flutter cupertino switchFlutter Cupertino 开关
【发布时间】:2020-08-14 01:44:18
【问题描述】:

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

错误是:

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

你能帮我解决这个问题吗?更正的代码 sn-p 会很棒。

【问题讨论】:

  • 方法 'call' 在 null 上被调用。但是您没有包含调用“调用”函数的代码。
  • @Kent 雅,我只是想通了。谢谢!

标签: flutter dart flutter-layout


【解决方案1】:

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

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

工作演示

完整代码

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

【讨论】:

  • 很高兴为您提供帮助。如果对您有帮助,请将其标记为答案。谢谢。
猜你喜欢
  • 2019-08-01
  • 1970-01-01
  • 2019-08-23
  • 2018-10-20
  • 2019-07-24
  • 2019-04-23
  • 2021-11-10
  • 2021-03-04
  • 2023-01-10
相关资源
最近更新 更多