选择并更改瓷砖颤动的自定义高度后

选择并更改瓷砖颤动的自定义高度后

本文介绍了选择并更改瓷砖颤动的自定义高度后,更改ListTile的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 GridView ,但是现在我需要在此 GridView 中选择一些 ListTiles ,选择后,我想要改变背景颜色.

I've made a GridView, but now I have some ListTiles in this GridView that I need to be selected Upon selection, I want the background color to change.

我还面临另一个问题,我想自定义这些 ListTile 的高度,但这样做没有任何成功.我要附上图片,我得到的输出和我想要的输出.

And I am also facing one more issue, I want to custom the heights of these ListTile, but I am not getting any success to do so. I am gonna attach the picture the output that I am getting and the output that I want.

我得到的是什么

我想要的东西:

这是代码:

class _SelectLanguageNewState extends State<SelectLanguageNew> {


List<Results> listResponseData;
bool _color;

@override
void initState() {
// TODO: implement initState
super.initState();
getLang();
_color = false;

}

Future getLang() async {

await getLanguage().then((GetLanguageResponse getlanguage)
{
  if(getlanguage.isSuccess)
    {
      setState(() {
        listResponseData = getlanguage.responseData.listResults;
      });
    }




});
}


@override
Widget build(BuildContext context) {

var size = MediaQuery.of(context).size;

final double itemHeight = (size.height - kToolbarHeight - 24) / 2;
final double itemWidth = size.width / 2;


void _onTileClicked(int index){
  debugPrint("You tapped on item $index");
}



// Get grid tiles
List<Widget> _getTiles() {
  final List<Widget> tiles = <Widget>[];
  for (int i = 0; i < listResponseData.length; i++) {
    tiles.add(ListTileTheme(

      selectedColor: Colors.blue,
      child: new InkWell(
        child: new Card(
          elevation: 5.0,
          child: new Container(
            color: _color ? Colors.black : Colors.grey,
            alignment: Alignment.center,
            child: new Text(listResponseData[i].nativeText,
            style: TextStyle(color: Colors.white),),
          ),
        ),
        onTap: () {

          setState(() {
            _color = !_color;

          });

         /* showDialog(
            barrierDismissible: false,
            context: context,
            child: new CupertinoAlertDialog(

              content: new Container(
                  width: 40.0,
                  child: new CircularProgressIndicator()),
              actions: <Widget>[
                new FlatButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                    child: new Text("OK"))
              ],
            ),
          );*/
        },
      ),
    ));
  }
  return tiles;
}









return Scaffold(
  bottomNavigationBar: BottomAppBar(
    child: Container(
      height: 50.0,
      child: InkWell(
        onTap: ()=> print('pressed'),
        child: Center(
          child: Text('Next', style: TextStyle(color: Colors.white,

          ),

          ),
        ),
      ),
    ),
    color: Colors.blue,
  ),

  body: new Container(
    child: listResponseData == null || listResponseData.isEmpty ? new Container(
      child: new Text('Loading data......'),
    ) : new GridView.count(

      shrinkWrap: true,
      scrollDirection: Axis.vertical,
      childAspectRatio:1.0,
      crossAxisCount: MediaQuery.of(context).size.width <= 400.0 ? 3 : MediaQuery.of(context).size.width >= 1000.0 ? 5 : 2,

      // Create a grid with 2 columns. If you change the scrollDirection to
      // horizontal, this would produce 2 rows.

      crossAxisSpacing: 5.0,
      // Generate 100 Widgets that display their index in the List

      children: _getTiles() ,

     ),
   ),

  );
  }
 }

推荐答案

屏幕截图

完整的解决方案:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(scaffoldBackgroundColor: Colors.black, brightness: Brightness.dark),
      home: Scaffold(
        appBar: AppBar(title: Text("Choose Language")),
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _languageIndex = -1;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Stack(
        alignment: Alignment.bottomCenter,
        children: <Widget>[
          GridView.count(
            crossAxisCount: 2,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
            childAspectRatio: 2.4,
            children: <Widget>[
              GestureDetector(
                child: _buildWidget("English", 0),
                onTap: () => setState(() => _languageIndex = 0),
              ),
              GestureDetector(
                child: _buildWidget("Española", 1),
                onTap: () => setState(() => _languageIndex = 1),
              ),
              GestureDetector(
                child: _buildWidget("Française", 2),
                onTap: () => setState(() => _languageIndex = 2),
              ),
              GestureDetector(
                child: _buildWidget("Gaeilge", 3),
                onTap: () => setState(() => _languageIndex = 3),
              ),
              GestureDetector(
                child: _buildWidget("العربية", 4),
                onTap: () => setState(() => _languageIndex = 4),
              ),
              GestureDetector(
                child: _buildWidget("جغتای", 5),
                onTap: () => setState(() => _languageIndex = 5),
              ),
              GestureDetector(
                child: _buildWidget("ગુજરાતી", 6),
                onTap: () => setState(() => _languageIndex = 6),
              ),
              GestureDetector(
                child: _buildWidget("हिन्दी", 7),
                onTap: () => setState(() => _languageIndex = 7),
              ),
              GestureDetector(
                child: _buildWidget("日本語", 8),
                onTap: () => setState(() => _languageIndex = 8),
              ),
              GestureDetector(
                child: _buildWidget("коми", 9),
                onTap: () => setState(() => _languageIndex = 9),
              ),
              GestureDetector(
                child: _buildWidget("ਲਹਿੰਦੀ", 10),
                onTap: () => setState(() => _languageIndex = 10),
              ),
              GestureDetector(
                child: _buildWidget("Schwyzerdütsch", 11),
                onTap: () => setState(() => _languageIndex = 11),
              ),
            ],
          ),
          SizedBox(
            width: double.maxFinite,
            height: 44,
            child: RaisedButton(
              child: Text("SUBMIT"),
              onPressed: () {
                print("languageIndex = ${_languageIndex}");
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildWidget(String language, int index) {
    bool isSelected = _languageIndex == index;
    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(6),
        border: Border.all(color: isSelected ? Colors.blue[300].withOpacity(0.8) : Colors.grey[700]),
        color: Colors.grey[900],
      ),
      child: Text(
        language,
        style: TextStyle(fontSize: 16, color: isSelected ? Colors.blue[400] : null),
      ),
    );
  }
}

您可以使用 _languageIndex 来观看用户选择的语言.

You can watch which language user selected by using the _languageIndex.

这篇关于选择并更改瓷砖颤动的自定义高度后,更改ListTile的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:18