【问题标题】:Flutter OutlinedButton: set disabled colorFlutter OutlinedButton:设置禁用颜色
【发布时间】:2021-08-21 11:28:01
【问题描述】:

在我的 Flutter 应用中,我有一个带圆角的 OutlinedButton:

OutlinedButton(
  child: Text(
    "a label",
  ),
  onPressed: () {
     // do something
  },
  style: OutlinedButton.styleFrom(
    primary: Colors.white,
    backgroundColor: MyColor.colorAccent,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.all(
        Radius.circular(10),
      ),
    ),
  ),
)

现在我需要为禁用状态设置按钮样式,但我不需要如何知道这个小部件(在 EevatedButton 上,我使用 MaterialStateProperty.resolveWith 作为背景颜色,但它不适合轮廓按钮)

【问题讨论】:

    标签: flutter button flutter-widget


    【解决方案1】:

    我不知道这个代码示例是否可以帮助回答您的问题,先生。

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      // This widget is the root of your application.
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      bool isEnableButton = true;
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            body: Center(
              child: Container(
                child: OutlinedButton(
                  child: Text(
                    "a label",
                  ),
                  onPressed: () {
                    setState(() {
                      if (isEnableButton) {
                        isEnableButton = false;
                      } else {
                        isEnableButton = true;
                      }
                    });
                  },
                  style:
                      isEnableButton ? enableButtonStyle() : disableButtonStyle(),
                ),
              ),
            ),
          ),
        );
      }
    
      dynamic disableButtonStyle() {
        return OutlinedButton.styleFrom(
          primary: Colors.black,
          backgroundColor: Colors.grey,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(10),
            ),
          ),
        );
      }
    
      dynamic enableButtonStyle() {
        return OutlinedButton.styleFrom(
          primary: Colors.black,
          backgroundColor: Colors.green,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(10),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      这不是最佳做法,但您可以这样做

      bool? _isdisabled ;
      //null check , if u are on an older version of dart of you want to declare it just do this :
      bool _isdisabled = false;
       
      OutlinedButton(
        child: Text(
          "a label",
        ),
        onPressed: () {
           // do something
        },
        style:_isdisabled==false? OutlinedButton.styleFrom(
          primary: Colors.white,
          backgroundColor: MyColor.colorAccent,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(10),
            ),
          ),
        ): //another style ,
      )
      

      【讨论】:

        猜你喜欢
        • 2020-01-12
        • 1970-01-01
        • 1970-01-01
        • 2013-06-14
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 2019-07-20
        • 2014-05-20
        相关资源
        最近更新 更多