Created
August 30, 2021 13:28
-
-
Save sho-ito-1027/5afa28bbc330e373d700b0c306b3fa9a to your computer and use it in GitHub Desktop.
ListTileのtitle、subtitle、trailingを使う場合の高さ変更可能な模倣Widget
This file contains 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 & trailing & optional subtitle'), | |
), | |
body: ListView( | |
children: [ | |
ImitationListTile( | |
height: 64, | |
title: const Text('タイトル'), | |
trailing: const Icon(Icons.keyboard_arrow_right), | |
onTap: () { | |
// ImitationListTile処理 | |
}, | |
), | |
const Divider(height: 1), | |
ImitationListTile( | |
height: 100, | |
title: const Text('タイトル'), | |
subtitle: const Text('サブタイトル'), | |
trailing: IconButton( | |
icon: const Icon(Icons.more_vert), | |
onPressed: () { | |
// Button処理 | |
}, | |
), | |
onTap: () { | |
// ImitationListTile処理 | |
}, | |
), | |
const Divider(height: 1), | |
], | |
), | |
); | |
} | |
} | |
class ImitationListTile extends StatelessWidget { | |
const ImitationListTile( | |
{required this.title, required this.trailing, this.subtitle, this.height, this.onTap}); | |
final Text title; | |
final Text? subtitle; | |
final Widget trailing; | |
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: Row( | |
children: [ | |
Expanded( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: [ | |
title, | |
_subtitle, | |
], | |
), | |
), | |
const SizedBox(width: 16.0), | |
trailing, | |
], | |
), | |
), | |
), | |
onTap: onTap, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment