Flutter 自动注入功能:快速添加行为和语句
import 'package:flutter/material.dart';
class AutoInjectPage extends StatefulWidget { const AutoInjectPage({Key? key}) : super(key: key);
@override _AutoInjectPageState createState() => _AutoInjectPageState(); }
class _AutoInjectPageState extends State
@override void initState() { super.initState(); _filteredStringList = List.from(_stringList); }
void _onSearch(String keyword) { setState(() { _filteredStringList = _stringList.where((item) { return item['act']!.contains(keyword) || item['prompt']!.contains(keyword); }).toList(); }); }
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('自动注入'), actions: [ IconButton( icon: const Icon(Icons.search), onPressed: () { showSearch(context: context, delegate: _SearchBarDelegate(onSearch: _onSearch)); }, ), ], ), body: ListView.builder( itemCount: _filteredStringList.length, itemBuilder: (BuildContext context, int index) { String act = _filteredStringList[index]['act']!; String prompt = _filteredStringList[index]['prompt']!; return RadioListTile( title: Text('行为: $act'), subtitle: Text('语句: $prompt'), value: index, groupValue: _selectedIndex, onChanged: (int? value) { setState(() { _selectedIndex = value!; }); }, ); }, ), ); } }
class _SearchBarDelegate extends SearchDelegate
_SearchBarDelegate({required this.onSearch});
@override
List
@override Widget buildLeading(BuildContext context) { return IconButton( icon: const Icon(Icons.arrow_back), onPressed: () { close(context, ''); }, ); }
@override Widget buildResults(BuildContext context) { onSearch(query); return const SizedBox.shrink(); }
@override Widget buildSuggestions(BuildContext context) { return const SizedBox.shrink(); } }
原文地址: https://www.cveoy.top/t/topic/lQrQ 著作权归作者所有。请勿转载和采集!