How to save a state from RadioListTile using Shared Preferences that will stay 'til I choose another RadioListTile? - Flutter

Viewed 64

I have a Row that contains a Text that shows a name based on RadioListTile in a btnSheetShow that I clicked.

Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                InkWell(
                                  onTap: () => btnSheetShow(memberController!.memberList),
                                  child: Text(
                                    merchantName,
                                    style: TextStyle(
                                        fontSize: 12,
                                        fontWeight: FontWeight.w500,
                                        color: Color(0xff31708F)),
                                  ),
                                ),
                                SizedBox(
                                  width: 5,
                                ),
                                valMember == -1
                                    ? Text('')
                                    : transactionLimit(limit: '0/2'),
                                Icon(
                                  Icons.expand_more,
                                  color: Color(0xff31708F),
                                ),
                              ],
                            ),

here's the btnSheetShow that contains RadioListTile(s)

Future btnSheetShow(List<SalesMemberList> memberList) {
    selectMerchant({required int index}) {
      setState(() =>
          merchantName = "${memberList[index].firstName} ${memberList[index].lastName}");
    }

    return showModalBottomSheet(
        isScrollControlled: false,
        barrierColor: Color(0xff000000).withOpacity(0.15),
        context: context,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20),
          ),
        ),
        // isDismissible: false,
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context, StateSetter setState) {
            return Container(
              height: 310,
              padding: EdgeInsets.fromLTRB(18, 17, 18, 20),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "Pilih Pedagang",
                        style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.w500,
                            color: Color(0xff1D6382)),
                      ),
                      Text(
                        "Lihat Semua",
                        style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.w400,
                            color: Color(0xff000000)),
                      ),
                    ],
                  ),
                  Container(
                    padding: EdgeInsets.only(top: 14),
                    height: 200,
                    child: ListView.builder(
                      itemCount: memberList.length,
                      itemBuilder: (context, index) => Padding(
                        padding: EdgeInsets.fromLTRB(0, 0, 15, 0),
                        child: RadioListTile<int>(
                          contentPadding: EdgeInsets.only(left: -25),
                          activeColor: Color(0xff799FB2),
                          dense: true,
                          value: memberList[index].memberId ?? 0,
                          title: Text(
                            "${memberList[index].firstName} ${memberList[index].lastName}",
                            style: TextStyle(
                                fontSize: 14,
                                color: Color(0xff000000),
                                fontWeight: FontWeight.w500),
                          ),
                          subtitle: Text(
                            // Ini adalah limit nilai transaksi yang bisa dilakukan oleh pedagang
                            "Limit: ${GlobalFunctions().rupiah(1000000)}/ ${GlobalFunctions().rupiah(3000000)}",
                            style: TextStyle(
                                fontSize: 10,
                                color: Color(0xff7F8A8E),
                                fontWeight: FontWeight.w500),
                          ),
                          secondary: transactionLimit(limit: '0/2'),
                          groupValue: valMember,
                          onChanged: (value) {
                            Provider.of<CartController>(context, listen: false).userId =
                                value ?? 0;
                            setState(() => valMember = value as int);
                            selectMerchant(index: index);
                          },
                          // selected: true,
                        ),
                      ),
                    ),
                  ),
                  Spacer(),
                  Center(
                    child: Container(
                      margin: EdgeInsets.only(top: 10),
                      width: MediaQuery.of(context).size.width / 1.4,
                      decoration: BoxDecoration(
                          color: Color(0xff007051),
                          borderRadius: BorderRadius.circular(4)),
                      height: 40,
                      child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                              primary: Color(0xff31708F),
                              textStyle: TextStyle(color: Colors.white)),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          child: Text(
                            "Pilih",
                            style: TextStyle(
                                fontSize: 18,
                                color: Colors.white,
                                fontWeight: FontWeight.w500),
                          )),
                    ),
                  ),
                ],
              ),
            );
          });
        });
  }

the code works just fine, it works according to my will. Here's some documentations. not picking any name the RadioListTile in a btnSheetShow the state after I picked a name here's the video that shows that I can't save the state

The question is "How do I save the state? Because when I leave to another page, the state turns back to "Pilih Pedagang", not the name that I picked earlier"

0 Answers
Related