如何根据Flutter中CupertinoPicker的值在屏幕上更改值

时间:2019-05-21 09:30:41

标签: dart flutter

我试图基于由gridview呈现的列表在屏幕上显示一个值,然后在点击该值时,我显示一个CupertinoPicker,并根据cupertino选择器选择的数字,更改显示在屏幕上的数字。屏幕。这是我尝试过的,但是并没有给我结果:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Colors.red,
      ),
      title: "test",
      home: MyHome()
    );
  }


}
class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
    List<Map<String, dynamic>> mylist = [
    {"name": "one", "count": 0},
    {"name": "one", "count": 0}
  ];
  List<int> numbers = [1, 2, 3];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
          body: Container(
        height: 200,
        child: GridView.builder(
          itemCount: mylist.length,
          gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
          itemBuilder: (BuildContext context, int index) {
            return InkWell(
              child: Container(
                child: Text(mylist[index]["count"].toString()),
              ),
              onTap: ()  {

                   _showModalBottomSheet(mylist[index]["count"]);
              },
            );
          },
        ),
      ));
  } void _showModalBottomSheet(int number) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
              color: Colors.white,
              child: Container(
                height: 250,
                child: CupertinoPicker(
                    backgroundColor: Colors.white,
                    itemExtent: 70, //height of each item
                    looping: true,
                    magnification: 1,
                    onSelectedItemChanged: (int index) {
                      setState(() {
                         print(number.toString() + "before");
                        number = numbers[index];
                        print(number.toString() + "after");
                      });
                    },
                    children: numbers
                        .map((chosenValue) => Text(chosenValue.toString()))
                        .toList()),
              ));
        });
  }
}

0 个答案:

没有答案