当用户点击按钮然后添加项目按钮必须出现在 Flutter 中时,我如何更改所需的布局

How can I change required layout when user clicks on button then add item button has to appear in Flutter

提问人:VIKAS SHARMA 提问时间:10/7/2023 最后编辑:Mark RotteveelVIKAS SHARMA 更新时间:10/7/2023 访问量:36

问:

我是 Flutter 状态管理的新手。根据这个视频,我想要相同的功能。https://streamable.com/6zm375?src=player-page-share。当用户单击按钮时,必须显示添加 ttems 按钮,我们可以添加项目。

这是这个“ADD”小部件的代码。

Padding(
     padding: const EdgeInsets.only(right: 15, top: 50,),
     child: SizedBox(
     width: 102,
     height: 58,
     child: Container(
     padding: const EdgeInsets.all(5),
     decoration: BoxDecoration(
                                                      
     border: Border.all(color: Colors.white),
     color: Colors.white,
     borderRadius: BorderRadius.circular(6), 
    ),
    child: 
   const Center(
   child: Text('Add', style: TextStyle(fontSize: 23, fontWeight: FontWeight.w700, 
   color: Color(0XFFDF2C25)),)),
   ),
  //child: Icon(CupertinoIcon),
   ),
   ),
Flutter 购物车 提供者 状态管理

评论

0赞 Mark Rotteveel 10/7/2023
在书写问题的标题和正文时,请使用正常的大写字母。也就是说,将句子的第一个字母 I(第一人称单数)和专有名词大写,其他一切都应该是小写的。

答:

0赞 Abhishek Karad 10/7/2023 #1

你可以通过在 Flutter 中使用 ValueNotifier 来实现同样的目标。

https://www.youtube.com/watch?v=s-ZG-jS5QHQ

创建一个名为cartItems

final ValueNotifier<int> cartItems = ValueNotifier<int>(0);

每当单击“添加按钮”时,请使用cartItemscartItems.value=cartItems.value+1;

您可以使用 valueNotifier 的 ValueListenableBuilder 包装底部栏的计数小部件,每当值更改时,它都会更新计数 UI。cartItemscartItems

欲了解更多信息: https://medium.flutterdevs.com/exploring-valuelistenablebuilder-in-flutter-9a16fc4c4c5b

final ValueNotifier<int> cartItems = ValueNotifier<int>(0);

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Dummy"),
      ),
      body: Center(
        child: ValueListenableBuilder<int>(
          valueListenable: cartItems,
          builder: (context, value, child) => Text("$value"),
        ),
      ),
      floatingActionButton: IconButton(
        onPressed: () {
          cartItems.value = cartItems.value + 1;
        },
        icon: const Icon(Icons.add),
      ),
    );
  }
}