【问题标题】:How to Show different Widget in showModel on buttonPressed如何在 buttonPressed 上的 showModel 中显示不同的小部件
【发布时间】:2020-12-14 08:58:10
【问题描述】:

我正在尝试显示 showModelBottomSheet() 并且它工作得很好,bottomSheet 中有一个按钮,所以每当我按下这个按钮时,它都会显示/返回一个新的容器(容器不同的小部件)。在这个 onTap() 上,我已将 bool 变量设置为 true 或 false ...但不起作用。是否 setState(());方法在 ModelBottomSheet 中不起作用?和 showGeneralDialog() 的类似情况... 我该如何解决这两个问题。

bool p=true;
showModalBottomSheet(
  backgroundColor: Colors.transparent,
    enableDrag: true,
    isScrollControlled: true,
    context: context, builder: (BuildContext context){

  return p==true?Container(
    width: MediaQuery.of(context).size.width,
    height: MediaQuery.of(context).size.height/2,
    padding: EdgeInsets.all(5.0),
    decoration: BoxDecoration(
      color: Theme.of(context).primaryColor,
      borderRadius: BorderRadius.only(topLeft: Radius.circular(20.0),topRight: Radius.circular(20.0)),
    ),
    child: Column(
      children: <Widget>[
        Container(
          alignment: Alignment.topLeft,
          width: MediaQuery.of(context).size.width,
          height: 30.0,
          child: IconButton(icon: Icon(Icons.arrow_downward,color: Colors.white,size: 32.0,), onPressed: (){
            Navigator.pop(context);
          }),
        ),

        Text(widget.model.cName,style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 25.0,fontFamily: 'Rajdhani'),),

        SizedBox(height: 10.0,),
        Text('AMOUNT',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.w100,fontSize: 12.0),),

        SizedBox(height: 20.0,),
        Text(widget.model.b,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 32.0,fontFamily: 'Rajdhani'),),

        SizedBox(height: 10.0,),
        Text('REPEAT',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.bold,fontSize: 12.0,fontFamily: 'Rajdhani'),),
        SizedBox(height: 15.0,),
        Text('EVERY MONTH AT   '+widget.model.date,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 20.0,fontFamily: 'Rajdhani'),),


        SizedBox(height: 20.0,),
        GestureDetector(
          onTap: (){
            pay=false;
            print('ayeeee $p');
          },
          child: Container(
            alignment: Alignment.bottomCenter,
            width: MediaQuery.of(context).size.width*0.8,
            height: 40.0,
            decoration: BoxDecoration(color: Colors.white,
              borderRadius: BorderRadius.circular(10.0),
            ),
            child: Center(
              child: Text('P ',style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 18.0,fontFamily: 'Rajdhani')),
            ),
          ),
        ),




      ],
    ),
  ):Container(
    width: MediaQuery.of(context).size.width,
    height: MediaQuery.of(context).size.height/2,
    padding: EdgeInsets.all(5.0),
    decoration: BoxDecoration(
      color: Theme.of(context).primaryColor,
      borderRadius: BorderRadius.only(topLeft: Radius.circular(20.0),topRight: Radius.circular(20.0)),
    ),
    child: Column(
      children: <Widget>[
        Container(
          alignment: Alignment.topLeft,
          width: MediaQuery.of(context).size.width,
          height: 30.0,
          child: IconButton(icon: Icon(Icons.arrow_downward,color: Colors.white,size: 32.0,), onPressed: (){
            Navigator.pop(context);
          }),
        ),

        Text('AMOUNT',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.w100,fontSize: 12.0),),

        SizedBox(height: 20.0,),
        Text(widget.model.b,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 32.0,fontFamily: 'Rajdhani'),),

        SizedBox(height: 10.0,),
        Text('SOURCE',style: TextStyle(color: Colors.white60,fontWeight: FontWeight.bold,fontSize: 12.0,fontFamily: 'Rajdhani'),),
        SizedBox(height: 15.0,),
        Text('EVERY MONTH AT   '+widget.model.date,style: TextStyle(color: Colors.white,fontWeight: FontWeight.w600,fontSize: 20.0,fontFamily: 'Rajdhani'),),


        SizedBox(height: 20.0,),
        Container(
          alignment: Alignment.bottomCenter,
          width: MediaQuery.of(context).size.width*0.8,
          height: 40.0,
          decoration: BoxDecoration(color: Colors.white,
            borderRadius: BorderRadius.circular(10.0),
          ),
          child: Center(
            child: Text('PA ',style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold,fontSize: 18.0,fontFamily: 'Rajdhani')),
          ),
        ),




      ],
    ),
  );

【问题讨论】:

    标签: flutter dart dialog flutter-showmodalbottomsheet


    【解决方案1】:

    要更新底部工作表的state,您需要用StatefulBuilder 包裹小部件并使用提供的StateSetter 设置状态:

    我在下面添加了一个示例:

    showModalBottomSheet(
            context: context,
            builder: (context) {
              return StatefulBuilder(
                  builder: (BuildContext context, StateSetter setModalState) {
                return  // ** YOUR WIDGETS ** 
              });
        });
    

    【讨论】:

    • 我需要将 showModalBottomSheet() 包装在 statfulWidgetClass 中吗?
    • 您只需要将widget 包装起来,showModalBottomSheet 将返回一个StatefulBuilder。然后您可以在Stateful Widget 中调用showModalBottomSheet
    • 它不工作...... setModalState 的任何功能都可以使用吗?
    • 是的,您必须执行setModalState((){}); 才能触发重建。
    • infact bool 值从 true 重置为 false 但仍然不显示其他容器
    猜你喜欢
    • 2020-10-16
    • 1970-01-01
    • 2021-01-30
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多