Created
August 30, 2021 13:23
-
-
Save aryzae/fd847cfe6b271b60d1239bf3b0441faf to your computer and use it in GitHub Desktop.
ListTileのtitle、subtitleを使う場合の高さ変更可能な模倣Widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp( | |
const MaterialApp( | |
home: MyHomePage(), | |
), | |
); | |
} | |
class MyHomePage extends StatelessWidget { | |
const MyHomePage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('title & optional subtitle'), | |
), | |
body: ListView( | |
children: [ | |
ImitationListTile( | |
height: 64, | |
title: const Text('タイトル'), | |
onTap: () { | |
// ImitationListTile処理 | |
}, | |
), | |
const Divider(height: 1), | |
ImitationListTile( | |
height: 100, | |
title: const Text('タイトル'), | |
subtitle: const Text('サブタイトル'), | |
onTap: () { | |
// ImitationListTile処理 | |
}, | |
), | |
const Divider(height: 1), | |
], | |
), | |
); | |
} | |
} | |
class ImitationListTile extends StatelessWidget { | |
const ImitationListTile({ | |
Key? key, | |
required this.title, | |
this.subtitle, | |
this.height, | |
this.onTap, | |
}) : super(key: key); | |
final Text title; | |
final Text? subtitle; | |
final double? height; | |
final VoidCallback? onTap; | |
@override | |
Widget build(BuildContext context) { | |
final _subtitle = subtitle ?? const SizedBox.shrink(); | |
final _height = height ?? 54.0; | |
return InkWell( | |
child: ConstrainedBox( | |
constraints: BoxConstraints(minHeight: _height), | |
child: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
title, | |
_subtitle, | |
], | |
), | |
), | |
), | |
onTap: onTap, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment