对于我的 flutter 应用程序,我需要一个可以在我按下添加按钮时添加的容器。于是我查看了其他 Stack Overflow 问题,例如:Flutter - Render new Widgets on click和 Flutter - Add new Widget on click。之后,这就是我想出的。
class Body extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var tPadding= MediaQuery.of(context).size.width  * 0.08; 
    var bPadding= MediaQuery.of(context).size.width  * 0.15; 
    var vPadding = MediaQuery.of(context).size.height  * 0.015;
    return Expanded (
      child: Container (
        child: NotificationListener<OverscrollIndicatorNotification> (
          onNotification: (OverscrollIndicatorNotification overscroll) {
            overscroll.disallowGlow();
          },
          child: PageView.builder(
            pageSnapping: false,
            controller: PageController(viewportFraction: 0.85),
            itemCount: container.length,
            itemBuilder: (context, i) {
              return Padding (
                padding: EdgeInsets.only(
                  left: vPadding,
                  right: vPadding,
                  top: tPadding,
                  bottom: bPadding
                ),
                child: container[i]
              );
            },
          )
        )
      )
    );
  }
}
 int _count = 1;
List container = [
  List.generate(_count, (int i) => ContainerCard),
  AddContainer()
];
class ContainerCard extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Material (
      borderRadius: BorderRadius.circular(50.0),
      color: Colors.white,
      elevation: 8.0,
    );
  }
}
class AddContainer extends StatefulWidget {
  @override
  AddContainerState createState() => AddContainerState();
}
class AddContainerState extends State<AddContainer> {
  @override
  Widget build(BuildContext context) {
    return Material(
      borderRadius: BorderRadius.circular(50.0),
      color: Colors.white,
      elevation: 8.0,
      child: InkWell (
        onTap: _addContainer,
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
        child: Column (
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Icon (
              Icons.add,
              size: 50.0,
            )
          ]
        ),
      )
    );
  }
  void _addContainer() {
    setState(() {
      _count = _count + 1;
    });
  }
}
但由于某种原因,这是行不通的。出了什么问题,我该如何解决?
完整代码:
import 'package:flutter/material.dart';
class MainPage extends StatelessWidget {
  @override
    Widget build(BuildContext context) {
      return MaterialApp (
        debugShowCheckedModeBanner: false,
        home: Scaffold (
          backgroundColor: Colors.white,
          body: Column (
            children: <Widget> [
              AppBar(),
              Body(),
            ]
          )
        )
      );
    }
}
class AppBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var abHeight = MediaQuery.of(context).size.height  * 0.2;
    var vPadding = MediaQuery.of(context).size.height  * 0.07;
    return Container (
      height: abHeight,
      child:  Column (
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget> [
          Row (
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(left: vPadding),
                child: PoppinsText (
                  text: "App",
                  fontSize: 40.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.black,
                ),
              )
            ]
          )
        ]
      )
    );
  }
}
class Body extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var tPadding= MediaQuery.of(context).size.width  * 0.08; 
    var bPadding= MediaQuery.of(context).size.width  * 0.15; 
    var vPadding = MediaQuery.of(context).size.height  * 0.015;
    return Expanded (
      child: Container (
        child: NotificationListener<OverscrollIndicatorNotification> (
          onNotification: (OverscrollIndicatorNotification overscroll) {
            overscroll.disallowGlow();
          },
          child: PageView.builder(
            pageSnapping: false,
            controller: PageController(viewportFraction: 0.85),
            itemCount: container.length,
            itemBuilder: (context, i) {
              return Padding (
                padding: EdgeInsets.only(
                  left: vPadding,
                  right: vPadding,
                  top: tPadding,
                  bottom: bPadding
                ),
                child: container[i]
              );
            },
          )
        )
      )
    );
  }
}
 int _count = 1;
List container = [
  List.generate(_count, (int i) => ContainerCard),
  AddContainer()
];
class ContainerCard extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Material (
      borderRadius: BorderRadius.circular(50.0),
      color: Colors.white,
      elevation: 8.0,
    );
  }
}
class AddContainer extends StatefulWidget {
  @override
  AddContainerState createState() => AddContainerState();
}
class AddContainerState extends State<AddContainer> {
  @override
  Widget build(BuildContext context) {
    return Material(
      borderRadius: BorderRadius.circular(50.0),
      color: Colors.white,
      elevation: 8.0,
      child: InkWell (
        onTap: _addContainer,
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
        child: Column (
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Icon (
              Icons.add,
              size: 50.0,
            )
          ]
        ),
      )
    );
  }
  void _addContainer() {
    setState(() {
      _count = _count + 1;
    });
  }
}
class PoppinsText extends StatelessWidget {
  PoppinsText ({Key key, 
    this.text, 
    this.fontSize, 
    this.fontWeight,
    this.color}) : super(key: key);
  final String text;
  final double fontSize;
  final FontWeight fontWeight;
  final Color color;
  @override
  Widget build(BuildContext context) {
    return Text (
      text,
      style: TextStyle (
        fontFamily: 'Poppins',
        fontWeight: fontWeight,
        fontSize: fontSize,
        color: color
      ),
    );
  }
}
您正在使用无状态小部件。切换到有状态小部件
编辑
按要求更新。这里的技巧是使用页面浏览量的反向属性。
class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}
class _BodyState extends State<Body> {
  int count = 1;
  @override
  Widget build(BuildContext context) {
    return Expanded(
        child: Container(
            child: NotificationListener<OverscrollIndicatorNotification>(
                onNotification: (OverscrollIndicatorNotification overscroll) {
                  overscroll.disallowGlow();
                },
                child: PageView.builder(
                    reverse: true,
                    pageSnapping: false,
                    controller: PageController(viewportFraction: 0.85),
                    itemCount: count,
                    itemBuilder: (context, i) {
                      print(i);
                      if (i == 0) {
                        return Padding(
                            padding: EdgeInsets.only(
                                left:
                                    MediaQuery.of(context).size.height * 0.015,
                                right:
                                    MediaQuery.of(context).size.height * 0.015,
                                top: MediaQuery.of(context).size.width * 0.08,
                                bottom:
                                    MediaQuery.of(context).size.width * 0.15),
                            child: Material(
                                borderRadius: BorderRadius.circular(50.0),
                                color: Colors.white,
                                elevation: 8.0,
                                child: InkWell(
                                  onTap: () {
                                    setState(() {
                                      count++;
                                    });
                                  },
                                  splashColor: Colors.transparent,
                                  highlightColor: Colors.transparent,
                                  child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: <Widget>[
                                        Icon(
                                          Icons.add,
                                          size: 50.0,
                                        )
                                      ]),
                                )));
                      } else {
                        return Padding(
                            padding: EdgeInsets.only(
                                left:
                                    MediaQuery.of(context).size.height * 0.015,
                                right:
                                    MediaQuery.of(context).size.height * 0.015,
                                top: MediaQuery.of(context).size.width * 0.08,
                                bottom:
                                    MediaQuery.of(context).size.width * 0.15),
                            child: Material(
                              borderRadius: BorderRadius.circular(50.0),
                              color: Colors.white,
                              elevation: 8.0,
                            ));
                      }
                    }))));
  }
| 归档时间: | 
 | 
| 查看次数: | 7658 次 | 
| 最近记录: |