简体   繁体   English

Flutter如何将项目存储在一页中

[英]How to store the items in one page in Flutter

SCENARIO设想

There are three pages, first page is Home Page which loads the image and name, second page is the Quantity Page once i select any of the image in the first page (homepage) it moves to second page(Quantity Page) which shows the details of the image, price and quantity and the third page is Cart Page which store the item in a listview builder once i click on the button in the second page(Quantity Page).共有三页,第一页是加载图像和名称的主页,第二页是数量页一次 i select 第一页(主页)中的任何图像移动到显示详细信息的第二页(数量页)图片,价格和数量的第三页是购物车页面,一旦我单击第二页(数量页面)中的按钮,它将项目存储在列表视图构建器中。

QUESTION问题

Now question is once i click on the button in the second page i can able to pass the variables (name,price etc) to third page but how do i store the item in third page because if i want to select another item in the first page(home page) then that previous selected item must be present in the third page(cart page).现在的问题是,一旦我单击第二页中的按钮,我就可以将变量(名称、价格等)传递到第三页,但是我如何将项目存储在第三页,因为如果我想 select 第一页中的另一个项目页面(主页),那么之前选择的项目必须出现在第三页(购物车页面)中。 Below is the dart code of the these three pages.下面是这三个页面的dart代码。

HomePage.dart首页.dart

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();}
class _HomeState extends State<Home> {
  var productList = [{
      "name": "Cake",
      "image": "assets/1.png",
      "price": "20.00",},
{
      "name": "Pasteries",
      "image": "assets/2.png",
      "price": "30.00",},];
  @override
  Widget build(BuildContext context) {
    return PlatformScaffold(
        body: ListView.builder(
            itemCount: productList.length,
            itemBuilder: (BuildContext context, int index) {
              return Products(
                product_image: productList[index]['image'],
                product_name: productList[index]['name'],
                product_price: productList[index]['price'],
              );}));}}
class Products extends StatelessWidget {
  final product_name;
  final product_image;
  final product_price;
  Products({this.product_name, this.product_image, this.product_price});
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 35.0, bottom: 15.0, left: 20.0, right: 20.0),
      child: GestureDetector(
        onTap: (){
          Navigator.of(context).push(MaterialPageRoute(builder: (context) => Quantities(
            productname: product_name,
            productprice: product_price,
            productimage: product_image,)));},
        child: Container(
          child: new FittedBox(
            child: Material(
                color: Colors.white,
                elevation: 15.0,
                borderRadius: BorderRadius.circular(15.0),
                shadowColor: Color(0x802196F3),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      width: 250,
                      height: 200,
                      child: ClipRRect(
                        borderRadius: new BorderRadius.circular(15.0),
                        child: new Image.asset(
                          product_image,
                          fit: BoxFit.cover,),),),
                    Padding(
                      padding: const EdgeInsets.only(top: 5.0,bottom: 5.0),
                      child: Text(product_name,style: TextStyle(color: Colors.blueGrey[700],
                          fontWeight: FontWeight.bold,fontSize: 18.0),),),],)),),),),);}}

Quantities.dart数量.dart

class Quantities extends StatefulWidget {
  var productprice;
  final productimage;
  final productname;
  Quantities({this.productprice, this.productimage,this.productname});
  @override
  _QuantitiesState createState() => _QuantitiesState(productprice,productimage,productname);}
class _QuantitiesState extends State<Quantities> {
  int counter = 1;
  var productprice;
  var finalprice;
  final productimage;
  final productname;
  _QuantitiesState(this.productprice, this.productimage,this.productname);
  void increment() {
    setState(() {
      counter++;
    finalprice=double.parse(productprice)*counter;});}
  void decrement() {
    setState(() {
      counter--;
      finalprice=double.parse(productprice)*counter;});}
  @override
  void initState() {
    super.initState();
    finalprice=double.parse(productprice);}
  @override
  Widget build(BuildContext context) {
    return PlatformScaffold(
      appBar: PlatformAppBar(
        backgroundColor: Colors.lightBlue[900],
        title: Text('Quantities'),),
      body: Padding(
        padding: const EdgeInsets.only(top: 10.0),
        child: ListView(
          children: <Widget>[
            new Container(
              height: 200.0,
              child: GridTile(
                child: Container(
                  color: Colors.white,
                  child: Image.asset(productimage),),),),
            Center(
              child: Text(
                "Price: "+finalprice.toString()+" SAR",
                style:
                TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),),),
            Padding(
              padding: const EdgeInsets.only(top: 10.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.only(right: 10.0),
                    child: PlatformButton(
                        onPressed: () {
                          decrement();
                          if (counter < 1) {
                            counter = 1;
                            finalprice=productprice;}},
                        child: Text(
                          '-',style: TextStyle(color: Colors.white, fontSize: 30.0),),
                        androidFlat: (_) =>
                            MaterialFlatButtonData(color: Colors.cyan),
                        ios: (_) => CupertinoButtonData(
                            color: Colors.cyan)),
                  ),Text(
                    '$counter',style:TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0),),
                  Padding(
                    padding: const EdgeInsets.only(left: 10.0),
                    child: PlatformButton(
                        onPressed: () {increment();},
                        child: Text(
                          '+',style: TextStyle(color: Colors.white, fontSize: 30.0),),
                        androidFlat: (_) =>
                            MaterialFlatButtonData(color: Colors.cyan),
                        ios: (_) => CupertinoButtonData(
                            color: Colors.cyan)),),],),),
            Padding(
              padding: const EdgeInsets.only(top: 20.0),
              child: Center(
                child: PlatformButton(
                    onPressed: () {
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) => Cart(
                        pImage: productimage,
                        pPrice: finalprice.toString(),)));},
                    child: Text('Add to Cart',style: TextStyle(color: Colors.white),),
                    androidFlat: (_) => MaterialFlatButtonData(
                        color: Colors.cyan),
                    ios: (_) => CupertinoButtonData(
                        color: Colors.cyan
                    )),),),],),),);}}

Cart.dart购物车.dart

class Cart extends StatefulWidget {
  final pImage;
  var pPrice;
  Cart({this.pImage, this.pPrice});
  @override
  _CartState createState() => _CartState(pImage,pPrice);}
class _CartState extends State<Cart> {
  final pImage;
  var pPrice;
  _CartState(this.pImage, this.pPrice);
  @override
  Widget build(BuildContext context) {
    return PlatformScaffold(
      appBar: PlatformAppBar(
        backgroundColor: Colors.lightBlue[900],
        automaticallyImplyLeading: false,
        title: Text('Cart'),),
      body: ListView(
        children: <Widget>[
          Card(
            child: Column(
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Column(
                      children: <Widget>[
                        Text(pPrice),],),
                    Container(
                      height: 100.0,
                      width: 50.0,
                      child: Image.asset(pImage),),],)],),),],),);}}

What you are looking for is a state management solution because you want to preserve the state (in this case the state is the list of currently selected products across screens) You can do that by creating a class that represents the state and in this class you save the items that should be shared across multiple screens Then you can use a state management solution like provider or bloc to help you build widgets that can listen to this state change and update accordingly. What you are looking for is a state management solution because you want to preserve the state (in this case the state is the list of currently selected products across screens) You can do that by creating a class that represents the state and in this class you保存应该跨多个屏幕共享的项目然后您可以使用 state 管理解决方案(如providerbloc )来帮助您构建可以监听此 state 更改和相应更新的小部件。 I recommend you to look for some tutorials like this one from Reso coder to learn more about state management in flutter.我建议您从 Reso coder 中查找类似这样的教程,以了解有关 flutter 中的 state 管理的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM