Created
September 5, 2019 06:39
-
-
Save 330132662/af6f19ebe835455018f97f2148453c24 to your computer and use it in GitHub Desktop.
学习flutter的第一天
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'; | |
import 'package:english_words/english_words.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final wordPair = new WordPair.random(); | |
return new MaterialApp( | |
title: 'Welcome to Flutter', | |
home: new RandomWords(), | |
theme: new ThemeData(primaryColor: Colors.white), | |
); | |
} | |
} | |
// #docregion RandomWordsState, RWS-class-only | |
class RandomWordsState extends State<RandomWords> { | |
final _saved = new Set<WordPair>(); | |
final _suggestions = <WordPair>[]; | |
final _biggerFont = const TextStyle(fontSize: 18.0); | |
Widget _buildSuggestions() { | |
return new ListView.builder( | |
padding: const EdgeInsets.all(16.0), | |
itemBuilder: (context, i) { | |
if (i.isOdd) return new Divider(); | |
final index = i ~/ 2; | |
if (index >= _suggestions.length) { | |
_suggestions.addAll(generateWordPairs().take(10)); | |
} | |
return _buildRow(_suggestions[index]); | |
}, | |
); | |
} | |
/*跳转新页面*/ | |
void _pushSaved() { | |
Navigator.of(context).push(new MaterialPageRoute(builder: (context) { | |
final tiles = _saved.map((pair) { | |
return new ListTile( | |
title: new Text( | |
pair.asPascalCase, | |
style: _biggerFont, | |
)); | |
}); | |
final divided = | |
ListTile.divideTiles(context: context, tiles: tiles).toList(); | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('saved suggestions'), | |
), | |
body: new ListView(children: divided), | |
); | |
})); | |
} | |
/** | |
* 跳转container | |
*/ | |
void _pushContainer() { | |
Navigator.of(context).push(new MaterialPageRoute(builder: (context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('container;练习'), | |
), | |
body: Center( | |
child: Container( | |
child: Text( | |
'慕课网是IT学习技能平台', | |
textAlign: TextAlign.right, | |
style: TextStyle( | |
fontSize: 25.0, | |
color: Color.fromARGB(255, 255, 150, 150), | |
decoration: TextDecoration.underline, | |
decorationStyle: TextDecorationStyle.solid), | |
), | |
alignment: Alignment.topLeft, | |
width: 500.0, | |
height: 400.0, | |
// color: Colors.lightBlue, | |
padding: const EdgeInsets.fromLTRB(10.0, 30.0, 0, 0), | |
margin: const EdgeInsets.all(40.0), | |
decoration: new BoxDecoration( | |
gradient: const LinearGradient(colors: [ | |
Colors.lightBlue, | |
Colors.lightGreen, | |
Colors.purple | |
])), | |
)), | |
); | |
})); | |
} | |
/** | |
* 图片练习 2019年9月5日11:05:43 | |
*/ | |
void _pushImage() { | |
Navigator.of(context).push(new MaterialPageRoute(builder: (context) { | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('container;练习'), | |
), | |
body: Center( | |
child: Container( | |
// width: 300.0, | |
height: 300.0, | |
color: Colors.lightGreen, | |
child: new Image.network( | |
'http://juhuiwangluo.com/files/images/banner05.jpg', | |
scale: 2.0, | |
fit: BoxFit.cover, | |
colorBlendMode: BlendMode.darken, | |
repeat: ImageRepeat.repeat, | |
), | |
)), | |
); | |
})); | |
} | |
// #enddocregion RWS-class-only | |
@override | |
Widget build(BuildContext context) { | |
// final wordPair = WordPair.random(); | |
// return Text(wordPair.asPascalCase); | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('start name generator'), | |
actions: <Widget>[ | |
new IconButton(icon: new Icon(Icons.list), onPressed: _pushImage), | |
new IconButton(icon: new Icon(Icons.list), onPressed: _pushSaved), | |
new IconButton( | |
icon: new Icon(Icons.list), onPressed: _pushContainer) | |
], | |
), | |
body: _buildSuggestions()); | |
} | |
// #docregion RWS-class-only | |
Widget _buildRow(WordPair pair) { | |
final alreadySaved = _saved.contains(pair); | |
return new ListTile( | |
title: new Text( | |
pair.asPascalCase, | |
style: _biggerFont, | |
), | |
trailing: new Icon( | |
alreadySaved ? Icons.favorite : Icons.favorite_border, | |
color: alreadySaved ? Colors.red : null, | |
), | |
onTap: () { | |
setState(() { | |
if (alreadySaved) { | |
_saved.remove(pair); | |
} else { | |
_saved.add(pair); | |
} | |
}); | |
}, | |
); | |
} | |
} | |
// #docregion RandomWords | |
class RandomWords extends StatefulWidget { | |
@override | |
RandomWordsState createState() => RandomWordsState(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment