10

Flutter 禁用按钮/禁止按钮点击

 3 years ago
source link: https://www.bugcatt.com/archives/2071
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

需具备的条件

若要顺利阅读本篇文章, 需要你具备如下条件:

本篇文章的环境:

环境版本Windows10Android Studio3.5Flutter1.19.0-2.0.pre

一定要注意环境的差异, 考虑不兼容的可能性; 并且具备以上条件. 否则阅读本篇博客可能会给你带来困扰.

创建一个空项目.

删除./test目录.

替换./lib/main.dart为:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Disable Button',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Disable Button'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State {

  // 是否开启按钮
  bool isEnabled = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [

            // 开关控件
            CupertinoSwitch(
              value: this.isEnabled,
              onChanged: (bool value) {
                setState(() {
                  this.isEnabled = value;
                });
              },
            ),
            RaisedButton(child: Text("目标按钮", style: TextStyle(fontSize: 25,color: Colors.white)), color: Colors.blue,onPressed: (){}),
          ],
        ),
      ),
    );
  }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK