首页 > 解决方案 > 如何在 Scaffold 中添加另一个按钮?

问题描述

我是新来的,我正在尝试重建演示应用程序。如何将 FloatingActioButton 添加到应用程序中,我似乎无法添加其他主体或其他东西。我怎样才能做到这一点 ?

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  int _counter = 0;

  void increase_Counter() {
    setState(() {
      _counter += 1;
    });
  }


  Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(
      appBar: AppBar(
        title: Text("Demo App"),
      ),
      body: Center(
        child: Text("Your press button $_counter times"),)
      body:(FloatingActionButton(onPressed: increase_Counter,)
    ),)
    ,
    );
  }
}

标签: flutterdart

解决方案


您可以检查以下代码。浮动操作按钮总是在脚手架内。

 Widget build(BuildContext context) {
    return MaterialApp(home: Scaffold(
      appBar: AppBar(
        title: Text("Demo App"),
      ),
      body: Center(
        child: Text("Your press button $_counter times"),),
      floatingActionButton: FloatingActionButton(
    onPressed: () {
      // Add your onPressed code here!
    },
    child: Icon(Icons.navigation),
    backgroundColor: Colors.green,
    ),
    
 
    );
  }

并且您也可以关注FloatingActionButton以获得更好的理解。


推荐阅读