如何在无状态中调用statefulwidget函数?

时间:2019-04-06 10:04:29

标签: flutter

主页上的脚手架上有一个饼图。 MyPieChart是一个statefulWidget,它具有布尔变量切换。我这样做的目的是,当切换为false时,构建返回一个文本“测试”,而当切换为true时,构建返回实际的PieChart。我该如何从首页中的浮动操作按钮的onPressed更改切换。这是我主页的代码:

import 'package:flutter/material.dart';
import 'package:testing/MyPieChart.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Stateful Chart',
      theme: ThemeData(
        primarySwatch: Colors.blueGrey,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Map<String, double> dataMap = new Map();
    dataMap.putIfAbsent("Flutter", () => 5);
    dataMap.putIfAbsent("React", () => 3);
    dataMap.putIfAbsent("Xamarin", () => 2);
    dataMap.putIfAbsent("Ionic", () => 2);
    return Scaffold(
      appBar: AppBar(
        title: Text("PieChart"),
      ),
      body: MyPieChart(dataMap),
      floatingActionButton:
          FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {}
            ),
    );
  }
}

0 个答案:

没有答案