Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void main() => runApp(
- MyApp()); // Main used arrow notation for calling one-line function or methods
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'NL',
- home: ScrollTab(),
- );
- }
- }
- class ScrollTab extends StatefulWidget {
- @override
- _ScrollTabState createState() => _ScrollTabState();
- }
- class _ScrollTabState extends State<ScrollTab>
- with SingleTickerProviderStateMixin {
- // Extending the stateless widget makes the app itself a widget
- TabController _tabController;
- ScrollController _scrollViewController;
- @override
- void initState() {
- super.initState();
- _scrollViewController = new ScrollController();
- _tabController = TabController(initialIndex: 0, vsync: this, length: 2);
- }
- @override
- void dispose() {
- _scrollViewController.dispose();
- _tabController.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return DefaultTabController(
- length: 2,
- child: new Scaffold(
- body: new NestedScrollView(
- headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
- return <Widget>[
- new SliverAppBar(
- title: Text("Application"),
- floating: true,
- pinned: true,
- snap:
- true, // <--- this is required if I want the application bar to show when I scroll up
- bottom: new TabBar(
- tabs: <Tab>[
- new Tab(text: "T"),
- new Tab(text: "B"),
- ], // <-- total of 2 tabs
- ),
- ),
- ];
- },
- body: new TabBarView(
- children: <Widget>[
- new RandomWords(),
- new RandomWords(),
- ], // <--- the array item is a ListView
- ),
- ),
- ),
- );
- // new Scaffold(
- // body: new NestedScrollView(
- // controller: _scrollViewController,
- // headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
- // return <Widget>[
- // new SliverAppBar(
- // title: new Text("NA"),
- // pinned: true,
- // floating: true,
- // forceElevated: innerBoxIsScrolled,
- // //snap: true,
- // bottom: new TabBar(
- // tabs: <Tab>[
- // new Tab(text: "T"),
- // new Tab(text: "B"),
- // ],
- // controller: _tabController,
- // ),
- // )
- // ];
- // },
- // body: new TabBarView(
- // children: <Widget>[
- // new RandomWords(),
- // new RandomWords(),
- // ],
- // controller: _tabController,
- // ),
- // ),
- // );
- //, );
- }
- }
- // Creating stateful widget needs two things
- // Stateful Widget which creates an instance of a state class.
- // So in essence its not the stateful widget which is immutable but the state
- // class which persists.
- class RandomWords extends StatefulWidget {
- @override
- _RandomWordsState createState() => _RandomWordsState();
- }
- class _RandomWordsState extends State<RandomWords> {
- final _suggestions = <WordPair>[];
- final _biggerFont = TextStyle(fontSize: 40.0);
- List<Note> _notes = List<Note>();
- Widget _buildSuggestions() {
- fetchNotes().then((value) {
- setState(() {
- _notes.addAll(value);
- });
- });
- return ListView.builder(
- itemCount: _notes.length,
- padding:
- EdgeInsets.only(top: 10.0, bottom: 10.0, left: 4.0, right: 4.0),
- itemBuilder: (context, i) {
- return Card(
- child: new InkWell(
- onTap: () {
- _navigateToPageDetails(context, _notes[i]);
- },
- child: Padding(
- padding: const EdgeInsets.only(
- top: 32.0, bottom: 32.0, left: 16.0, right: 16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Text(
- _notes[i].title,
- style:
- TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
- ),
- Text(
- _notes[i].text,
- style: TextStyle(color: Colors.grey.shade700),
- ),
- Image.network(_notes[i].image)
- // Image,
- ],
- ),
- ),
- ),
- );
- });
- }
- _navigateToPageDetails(BuildContext context, Note item) async {
- //Holds the results returned from PageDetails.
- final resultFromPageDetails = await Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => PageDetails(
- item: item,
- ),
- ),
- );
- Scaffold.of(context)
- ..removeCurrentSnackBar()
- ..showSnackBar(SnackBar(content: Text("$resultFromPageDetails")));
- }
- Widget _buildRow(WordPair pair) {
- return ListTile(
- title: Text(
- pair.asPascalCase,
- style: _biggerFont,
- ),
- );
- }
- Future<List<Note>> fetchNotes() async {
- var url =
- '...'
- var response = await http.get(url);
- var notes = List<Note>();
- if (response.statusCode == 200) {
- ...
- }
- }
- return notes;
- }
- @override
- Widget build(BuildContext context) {
- // final words = WordPair.random();
- // return Text(words.asPascalCase);
- // Now adding the infinite scroll list
- return Scaffold(
- body: _buildSuggestions(),
- );
- }
- }
- class PageDetails extends StatefulWidget {
- PageDetails({Key key, this.item}) : super(key: key);
- final Note item;
- @override
- _PageDetails createState() => new _PageDetails();
- }
- class _PageDetails extends State<PageDetails> {
- String description =
- "The Griffith Observatory is the most iconic building in Los Angeles, perched high in the Hollywood Hills, 1,134 feet above sea level.";
- bool isPlaying = false;
- FlutterTts _flutterTts;
- @override
- void initState() {
- super.initState();
- initializeTts();
- }
- @override
- void dispose() {
- super.dispose();
- _flutterTts.stop();
- }
- initializeTts() {
- _flutterTts = FlutterTts();
- if (PlatformUtil.myPlatform() == MyPlatform.ANDROID) {
- _flutterTts.ttsInitHandler(() {
- setTtsLanguage();
- });
- } else if (PlatformUtil.myPlatform() == MyPlatform.IOS) {
- setTtsLanguage();
- } else if (PlatformUtil.myPlatform() == MyPlatform.WEB) {
- //not-supported by plugin
- }
- _flutterTts.setStartHandler(() {
- setState(() {
- isPlaying = true;
- });
- });
- _flutterTts.setCompletionHandler(() {
- setState(() {
- isPlaying = false;
- });
- });
- _flutterTts.setErrorHandler((err) {
- setState(() {
- print("error occurred: " + err);
- isPlaying = false;
- });
- });
- }
- void setTtsLanguage() async {
- await _flutterTts.setLanguage("en-US");
- }
- void speechSettings1() {
- _flutterTts.setVoice("en-us-x-sfg#male_1-local");
- _flutterTts.setPitch(1.5);
- _flutterTts.setSpeechRate(.9);
- }
- void speechSettings2() {
- _flutterTts.setVoice("en-us-x-sfg#male_2-local");
- _flutterTts.setPitch(1);
- _flutterTts.setSpeechRate(0.5);
- }
- Future _speak(String text) async {
- if (text != null && text.isNotEmpty) {
- var result = await _flutterTts.speak(text);
- if (result == 1)
- setState(() {
- isPlaying = true;
- });
- }
- }
- Future _stop() async {
- var result = await _flutterTts.stop();
- if (result == 1)
- setState(() {
- isPlaying = false;
- });
- }
- Widget playButton(BuildContext context) {
- return Container(
- child: Stack(
- children: <Widget>[
- Container(
- padding:
- const EdgeInsets.symmetric(vertical: 5.0, horizontal: 16.0),
- margin: const EdgeInsets.only(
- top: 30, left: 30.0, right: 30.0, bottom: 20.0),
- child: FlatButton(
- onPressed: () {
- //fetch another image
- setState(() {
- //speechSettings1();
- //isPlaying ? _stop() : _speak("descriptions yes no");
- _speak(widget.item.text);
- });
- },
- child: isPlaying
- ? Icon(
- Icons.stop,
- size: 60,
- color: Colors.red,
- )
- : Icon(
- Icons.play_arrow,
- size: 60,
- color: Colors.green,
- ),
- ),
- ),
- ],
- ),
- );
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(widget.item.title),
- ),
- body: Column(
- children: [
- Text(
- widget.item.text,
- style: TextStyle(color: Colors.black),
- ),
- Image.network(widget.item.image),
- Text(
- "yolo",
- style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
- ),
- playButton(context),
- ],
- ),
- floatingActionButton: FloatingActionButton(
- child: Icon(Icons.favorite_border),
- onPressed: () {
- //Current item's name along with message is sent back to last screen
- Navigator.pop(context, '${widget.item.title} is marked as favorite.');
- },
- ),
- );
- }
- }
- class TTSPluginRecipe extends StatefulWidget {
- @override
- _TTSPluginRecipeState createState() => _TTSPluginRecipeState();
- }
- class _TTSPluginRecipeState extends State<TTSPluginRecipe> {
- String description =
- "The Griffith Observatory is the most iconic building in Los Angeles, perched high in the Hollywood Hills, 1,134 feet above sea level.";
- bool isPlaying = false;
- FlutterTts _flutterTts;
- @override
- void initState() {
- super.initState();
- initializeTts();
- }
- @override
- void dispose() {
- super.dispose();
- _flutterTts.stop();
- }
- initializeTts() {
- _flutterTts = FlutterTts();
- if (PlatformUtil.myPlatform() == MyPlatform.ANDROID) {
- _flutterTts.ttsInitHandler(() {
- setTtsLanguage();
- });
- } else if (PlatformUtil.myPlatform() == MyPlatform.IOS) {
- setTtsLanguage();
- } else if (PlatformUtil.myPlatform() == MyPlatform.WEB) {
- //not-supported by plugin
- }
- _flutterTts.setStartHandler(() {
- setState(() {
- isPlaying = true;
- });
- });
- _flutterTts.setCompletionHandler(() {
- setState(() {
- isPlaying = false;
- });
- });
- _flutterTts.setErrorHandler((err) {
- setState(() {
- print("error occurred: " + err);
- isPlaying = false;
- });
- });
- }
- void setTtsLanguage() async {
- await _flutterTts.setLanguage("en-US");
- }
- void speechSettings1() {
- _flutterTts.setVoice("en-us-x-sfg#male_1-local");
- _flutterTts.setPitch(1.5);
- _flutterTts.setSpeechRate(.9);
- }
- void speechSettings2() {
- _flutterTts.setVoice("en-us-x-sfg#male_2-local");
- _flutterTts.setPitch(1);
- _flutterTts.setSpeechRate(0.5);
- }
- Future _speak(String text) async {
- if (text != null && text.isNotEmpty) {
- var result = await _flutterTts.speak(text);
- if (result == 1)
- setState(() {
- isPlaying = true;
- });
- }
- }
- Future _stop() async {
- var result = await _flutterTts.stop();
- if (result == 1)
- setState(() {
- isPlaying = false;
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Stack(
- children: <Widget>[
- Container(
- height: 360,
- decoration: BoxDecoration(
- borderRadius: BorderRadius.only(
- bottomLeft: Radius.circular(50.0),
- bottomRight: Radius.circular(50.0)),
- gradient: LinearGradient(
- colors: [Colors.blue, Colors.blueAccent],
- begin: Alignment.topLeft,
- end: Alignment.bottomRight)),
- ),
- Container(
- margin: const EdgeInsets.only(top: 80),
- child: Column(
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: Center(
- child: Text(
- "Griffith Observatory",
- style: TextStyle(
- color: Colors.white,
- fontSize: 28,
- fontStyle: FontStyle.normal),
- ),
- ),
- ),
- SizedBox(height: 20.0),
- Expanded(
- child: Stack(
- children: <Widget>[
- Container(
- height: double.infinity,
- margin: const EdgeInsets.only(
- left: 30.0, right: 30.0, top: 10.0),
- child: ClipRRect(
- borderRadius: BorderRadius.circular(30.0),
- child: Container(
- height: MediaQuery.of(context).size.height / 1.25,
- width: MediaQuery.of(context).size.width / 1.25,
- child: Image.asset(
- "assets/images/griffith_observatory.jpg"),
- ),
- ),
- ),
- ],
- ),
- ),
- Padding(
- padding: EdgeInsets.all(8.0),
- child: Text(description),
- ),
- playButton(context),
- ],
- ),
- ),
- ],
- ),
- );
- }
- Widget playButton(BuildContext context) {
- return Container(
- child: Stack(
- children: <Widget>[
- Container(
- padding:
- const EdgeInsets.symmetric(vertical: 5.0, horizontal: 16.0),
- margin: const EdgeInsets.only(
- top: 30, left: 30.0, right: 30.0, bottom: 20.0),
- child: FlatButton(
- onPressed: () {
- //fetch another image
- setState(() {
- //speechSettings1();
- isPlaying ? _stop() : _speak(description);
- });
- },
- child: isPlaying
- ? Icon(
- Icons.stop,
- size: 60,
- color: Colors.red,
- )
- : Icon(
- Icons.play_arrow,
- size: 60,
- color: Colors.green,
- ),
- ),
- ),
- ],
- ),
- );
- }
- }
Add Comment
Please, Sign In to add comment