2017-08-08 11 views
0

私は以下のフラッターコードを持っていて、clickableのボタンをクリックできるようにしようとしています。フラッターギャラリーでサンプルコードを使用しましたが、入力クラスのフィールドをFlatButtonsのリストfinal List<FlatButtons> bvttonsに定義しようとしましたが、タイプではないFlatButtonsのエラーが発生します。クリック可能な文字列をボタンのように反応させる方法はありますか?あなたはListTileを使用しているExpansionTileをクリック可能にする

import 'package:flutter/material.dart'; 

void main() { 
    runApp(new Demo()); 
} 

class Demo extends StatelessWidget { 
    // This widget is the root of your application. 
    @override 
    Widget build(BuildContext context) { 
    return new MaterialApp(
     title: 'Demo', 
     theme: new ThemeData(
     primarySwatch: Colors.blue, 
    ), 
     home: new Home(title: 'Demo'), 
    ); 
    } 
} 

class Home extends StatefulWidget { 
    Home({Key key, this.title}) : super(key: key); 

    final String title; 

    @override 
    _HomeState createState() => new _HomeState(); 
} 

class Entry { 
    Entry(this.title, [this.children = const <Entry>[]]); 
    final String title; 
    final List<Entry> children; 
} 

final List<Entry> data = <Entry>[ 
    new Entry(
    'Expand', <Entry>[ 
     new Entry(
     'Clickable', 
    ), 
    ], 
)]; 

class EntryItem extends StatelessWidget { 
    const EntryItem(this.entry); 

    final Entry entry; 

    Widget _buildTiles(Entry root) { 
    if (root.children.isEmpty) 
     return new ListTile(title: new Text(root.title)); 
    return new ExpansionTile(
     key: new PageStorageKey<Entry>(root), 
     title: new Text(root.title), 
     children: root.children.map(_buildTiles).toList(), 
    ); 
    } 

    @override 
    Widget build(BuildContext context) { 
    return _buildTiles(entry); 
    } 
} 

class _HomeState extends State<Home> { 
    Widget _buildDrawer(BuildContext context) { 
    return new Drawer(
     child: new ListView(
     children: <Widget>[ 
      const DrawerHeader(
       child: const Center(child: const Text('Demo'))), 
      const ListTile(
      leading: const Icon(Icons.home), 
      title: const Text('Home'), 
      selected: false, 
     ), 
      const ListTile(
      leading: const Icon(Icons.assessment), 
      title: const Text('Results'), 
      enabled: false, 
     ), 
      new ListTile(
      leading: const Icon(Icons.cached), 
      title: const Text('Refresh All'), 
      enabled: false, 
     ), 
      const Divider(), 
      new ListTile(
      leading: const Icon(Icons.settings), 
      title: const Text('Settings'), 
      onTap: _handleShowSettings, 
     ), 
      new ListTile(
      leading: const Icon(Icons.feedback), 
      title: const Text('Feedback'), 
      enabled: false, 
     ), 
      new ListTile(
      leading: const Icon(Icons.help), 
      title: const Text('About'), 
      onTap: _handleShowAbout, 
     ), 
     ], 
    ), 
    ); 
    } 

    void _handleShowSettings() { 
    Navigator.popAndPushNamed(context, '/settings'); 
    } 

    void _handleShowAbout() { 
    showAboutDialog(context: context); 
    } 

    @override 
    Widget build(BuildContext context) { 
    return new Scaffold(
     appBar: new AppBar(
     title: new Text(widget.title), 
    ), 
     drawer: _buildDrawer(context), 
     body: new ListView.builder(
     itemBuilder: (BuildContext context, int index) => 
      new EntryItem(data[index]), 
     itemCount: data.length, 
    ), 
    ); 
    } 
} 

答えて

0

、それはコンストラクタパラメータonTapを持っています。あなたは_buildTilesを変更することができます。

Widget _buildTiles(Entry root) { 
    if (root.children.isEmpty) 
     return new ListTile(
     title: new Text(root.title), 
     onTap:()=>debugPrint("I was clicked"), 
     ); 
    return new ExpansionTile(
     key: new PageStorageKey<Entry>(root), 
     title: new Text(root.title), 
     children: root.children.map(_buildTiles).toList(), 
    ); 
    } 

またdocs

+0

おかげで、短いサンプルがあります!それはうまくいくようです。私は新しい画面に移動するためにそれを使用できますか?コンテキストを受け入れないようです – driftavalii

+0

ナビゲーションには何がありますか?明確にすることはできますか? – Mogol

+0

私は単純にonTapの呼び出しを 'secondPagenavi(){Navigator.of(context).pushNamed("/SecondPage ");}'と置き換えて別のページに移動することができます。インスタンスゲッター 'コンテキスト'なし – driftavalii

関連する問題