【问题标题】:How to make a Button execute different "classes' every button click如何使按钮在每次单击按钮时执行不同的“类”
【发布时间】:2021-08-04 05:34:53
【问题描述】:

我试图让一个浮动操作按钮在每次单击时更改一个文本小部件,我有 3 个类,我希望 ONE 按钮能够在每次单击按钮时调用不同的类,并且当它完成调用第三类时它回到执行第一个类,并不断重复..

我可以使用 onpressed 执行一个类,但我不确定如何在每次点击时执行不同的类..

这是代码

    class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  void classes1(){
    setState(() {
      text = 'This is Text From Class1';
    });
  }

  void classes2(){
    setState(() {
      text = 'This is Text From Class 2';
    });
  }
  void classes3(){
    setState(() {
      text = 'This is Text From Class 3 ';
    });
  }

  String text = '';

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(

        title: Text('AppBar'),
      ),
      body: Center(

        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '$text',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: classes1,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

【问题讨论】:

    标签: android flutter dart flutter-layout


    【解决方案1】:

    这是您的解决方案,请结帐

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    void main() => runApp(MaterialApp(
        home: MyApp()));
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
    
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
    int count = 0;
      void classes1(){
        setState(() {
          text = 'This is Text From Class1';
        });
      }
    
      void classes2(){
        setState(() {
          text = 'This is Text From Class 2';
        });
      }
      void classes3(){
        setState(() {
          text = 'This is Text From Class 3 ';
        });
      }
    
      String text = '';
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          appBar: AppBar(
    
            title: Text('AppBar'),
          ),
          body: Center(
    
            child: Column(
    
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '$text',
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: (){
              switch (count%3) {
                case 0:
                  count++;
                  return classes1();
                case 1:
                  count++;
                  return classes2();
                case 2:
                  count++;
                  return classes3();
                default:
                  return null;
              }
    
            },
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多