【问题标题】:Stateful constrictor Widget in flutter颤动中的有状态收缩器小部件
【发布时间】:2021-02-24 02:52:34
【问题描述】:

我正在尝试使用有状态小部件进行自定义类,我必须使用有状态,因为它具有 setState 函数但是我想为该类添加属性,因此,每当我调用该类时,我都会传递我想要的颜色或存储我想要的数据我使用无状态小部件对 Rassed Button 做了同样的事情,它是有效的,但是对于有状态的我有一个错误,即变量未定义 我尝试使用 widget.borderColor 调用它,但我有一个错误,即未定义小部件
这是代码:

class DoseDropDown extends StatefulWidget {
  Color borderColor;
  Color hintColor;
  DoseDropDown({
    this.hintColor,
    this.borderColor,
  });

  @override
  _DoseDropDownState createState() => _DoseDropDownState();
}

String medicationDose;
List<DropdownMenuItem> getDropDownItem() {
  List<DropdownMenuItem> dropDownItems = [];
  for (String dose in medcationDose) {
    var newItem = DropdownMenuItem(
      child: Text(
        dose,
        style: TextStyle(

我在这里尝试使用它:

color: hintColor,

我有错误,它没有定义

      fontSize: 23, fontWeight: FontWeight.bold, 
        ),
      ),
      value: dose,
    );
    dropDownItems.add(newItem);
  }
  return dropDownItems;
}

List<String> medcationDose = [
  'مرة واحدة في اليوم',
  'مرتان في اليوم',
  'ثلاث مرات في اليوم',
  'اربعة مرات في اليوم',
  'وقت الحاجة'
];

class _DoseDropDownState extends State<DoseDropDown> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 70,
      width: 350,
      child: DropdownButtonFormField(
        dropdownColor: white,
        value: medicationDose,
        items: getDropDownItem(),
        iconSize: 50,
        iconEnabledColor: yellow,
        onChanged: (value) {
          setState(() {
            medicationDose = value;
          });
        },
        decoration: InputDecoration(
          prefixIcon: Icon(
            MyFlutterApp.pills__2_,
            color: yellow,
            size: 30,
          ),
          hintText: 'الجرعة',
          hintStyle: TextStyle(
              fontSize: 30, fontWeight: FontWeight.bold, color: white),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(
              color: borderColor,
            ),
            borderRadius: BorderRadius.circular(30.0),
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    在这种情况下,函数 getDropDownItem() 是全局的,而不是在 class _DoseDropDownState
    您可以将hintColor 作为参数传递
    您可以在DropdownButtonFormField 中使用widget.hintColor 并传递给getDropDownItem
    代码sn-p

    List<DropdownMenuItem> getDropDownItem(Color hintColor) {
    ...
    child: DropdownButtonFormField(
            dropdownColor: Colors.white,
            value: medicationDose,
            items: getDropDownItem(widget.hintColor),
    

    工作演示

    完整代码

    import 'package:flutter/material.dart';
    
    String medicationDose;
    List<DropdownMenuItem> getDropDownItem(Color hintColor) {
      List<DropdownMenuItem> dropDownItems = [];
      for (String dose in medcationDose) {
        var newItem = DropdownMenuItem(
          child: Text(
            dose,
            style: TextStyle(
              color: hintColor,
              fontSize: 23,
              fontWeight: FontWeight.bold,
            ),
          ),
          value: dose,
        );
        dropDownItems.add(newItem);
      }
      return dropDownItems;
    }
    
    List<String> medcationDose = [
      'مرة واحدة في اليوم',
      'مرتان في اليوم',
      'ثلاث مرات في اليوم',
      'اربعة مرات في اليوم',
      'وقت الحاجة'
    ];
    
    class DoseDropDown extends StatefulWidget {
      Color borderColor;
      Color hintColor;
      DoseDropDown({
        this.hintColor,
        this.borderColor,
      });
    
      @override
      _DoseDropDownState createState() => _DoseDropDownState();
    }
    
    class _DoseDropDownState extends State<DoseDropDown> {
      @override
      Widget build(BuildContext context) {
        return SizedBox(
          height: 70,
          width: 350,
          child: DropdownButtonFormField(
            dropdownColor: Colors.white,
            value: medicationDose,
            items: getDropDownItem(widget.hintColor),
            iconSize: 50,
            iconEnabledColor: Colors.yellow,
            onChanged: (value) {
              setState(() {
                medicationDose = value;
              });
            },
            decoration: InputDecoration(
              prefixIcon: Icon(
                Icons.home,
                color: Colors.yellow,
                size: 30,
              ),
              hintText: 'الجرعة',
              hintStyle: TextStyle(
                  fontSize: 30, fontWeight: FontWeight.bold, color: Colors.white),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.green,
                ),
                borderRadius: BorderRadius.circular(30.0),
              ),
            ),
          ),
        );
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                DoseDropDown(
                  hintColor: Colors.brown,
                )
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 2022-11-12
      • 1970-01-01
      • 2021-11-01
      • 2020-08-01
      • 2019-10-06
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多